Batch File Syntax

djnes

Fully [H]
Joined
Mar 24, 2000
Messages
19,560
I'm trying to write a batch file that sites on our H drive network drive, that will rename the users local hosts file to hosts.old and copy down a new one. I'm getting a path error, so I'm thinking I did something wrong, maybe because of the spaces, or possibly because the hosts file has no extension? I'm horrible at writing batch files, but here's my syntax:

ren C:\Windows\system32\drivers\etc\hosts C:\Windows\system32\drivers\etc\hosts.old

copy H:\Remote Host File\hosts C:\Windows\system32\drivers\etc\hosts

For the sake of this posting, we'll assume I typed the paths in correctly. I want to be able to have my remote users go to our H drive, and run this batch file to replace their file.
 
1. Use %windir% instead of C:\windows when possible. If you ever *do* happen to have a computer where the windows path isn't c:\windows, the script should still work

2. You may need to either shorten your path or use quotes.

In the end, one of these should work:
Code:
ren %windir%\system32\drivers\etc\hosts %windir%\system32\drivers\etc\hosts.old
copy H:\Remote~1\hosts %windir%\system32\drivers\etc\hosts

Code:
ren %windir%\system32\drivers\etc\hosts %windir%\system32\drivers\etc\hosts.old
copy "H:\Remote Host File\hosts" %windir%\system32\drivers\etc\hosts

Notice the path change on the H drive in the first example and the quotes used in the second example (in addition to the %windir% changes)
 
Don't you use a path on your target. So in your example it should be.

ren C:\Windows\system32\drivers\etc\hosts hosts.old

This is because you are not doing a move or copy.

You only need quotes if you have spaces, which you do not in this case.
 
Don't you use a path on your target. So in your example it should be.

ren C:\Windows\system32\drivers\etc\hosts hosts.old

This is because you are not doing a move or copy.

Although that is true, he said he's getting a path error. Trying it myself, it tells me there's a syntax error.

You only need quotes if you have spaces, which you do not in this case.

I don't know about you, but I see 2 spaces in his copy command (H:\Remote Hosts File).
 
Since you correctly covered the copy line I was only referring to the rename line. Hence my line about not doing a copy or move (since the copy is the next line).

I should have been a bit more clear.

Thanks.
 
I'm going to give this a shot today, to see how it works. Thanks for the replies.
 
You could also add a period at the end of the original hosts file you are attempting to rename. This will avoid any confusion between hosts directory (if there is one) and hosts file.

ren %windir%\system32\drivers\etc\hosts. %windir%\system32\drivers\etc\hosts.old
copy "H:\Remote Host File\hosts" %windir%\system32\drivers\etc\*.*
 
Back
Top