1.

Solve : Simple Batch Script not working.. Can anyone help??

Answer»

I have created a simple batch script that acts as a "hosts file switcher", but it doesn't seem to be working. The code is posted below.. Can anyone please tell me why this isn't working? This is very frustrating!

Thanks in ADVANCE!

Code: [Select]IF EXIST %windir%\system32\drivers\etc\hosts_restricted (
ren %windir%\system32\drivers\etc\hosts hosts_normal &
ren %windir%\system32\drivers\etc\hosts_restricted hosts
)
ELSE (
ren %windir%\system32\drivers\etc\hosts hosts_restricted &
ren %windir%\system32\drivers\etc\hosts_normal hosts
)

when you say doesn't work, what doesn't work? the files did not rename? or you encountered other errors?
I noticed there's a "&", what is it for? i don't think you need it.Hi ghostdog,

Thanks for your reply!

When I say "it doesn't work", I mean that when I execute the batch file, I get the command prompt window flash for 2 SECONDS, and it disappears. Afterward, when I check the relevant directory, none of the files have been RENAMED as they should have been.

I am trying to execute 2 commands if the if statement evaluates to "true". I thought the "&" was needed to say: execute this line & also execute this line after it.

Is this not the right syntax for it?

Thanks!

The & is not needed. By default the file will go through commands in order. If the two commands are in () then there really isn't a need for it. The & is used for MULTIPLE commands on the same line. You can try it on the command prompt if you want -- the one I use the most is the command "cls & dir". well, you can put a pause at the end of your batch, the see what the error message is.Probably the punctuation. I seem to recall that the else must be on the same logical line as the if. Also REMOVE the ampersands; they're used incorrectly in this context.

Code: [Select]IF EXIST %windir%\system32\drivers\etc\hosts_restricted (
ren %windir%\system32\drivers\etc\hosts hosts_normal
ren %windir%\system32\drivers\etc\hosts_restricted hosts
) ELSE (
ren %windir%\system32\drivers\etc\hosts hosts_restricted
ren %windir%\system32\drivers\etc\hosts_normal hosts
)

8-)



Discussion

No Comment Found