Articles

batch file to edit/copy local file

March 18, 2009 at 13:13:23
Specs: XP professional, 3.25gb

Hello I am trying to create a batch file that will "add" a line to my hosts file so that our server can resolve.
Since it will be for more then just me I want to make sure that the batch file creates a backup if their original hosts file then copies and adds a line to a new one.
Is this possible? If so can anyone help me?
-Copies existing hosts file renames with.bkp as a backup
-adds / creates new hosts file with contents of original + one added line to resolve server
Currently I have this
:::1st part tells NT what date format I want:::  
@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy% 

RENAME "C:\WINDOWS\system32\drivers\etc\hosts" hosts.bkp_%date%

MKDIR "C:\WINDOWS\system32\drivers\etc\hosts"
echo   
echo DONE Backing up CACHE and creating a new empty CACHE!
echo   
@pause

which only renames existing file with .bkp and a time stamp then it creates a "Folder" rather then the actual hosts file.
or would it simply be easier to remote in and manually edit the file?

hello hello hello, is there anybody in there...


See More: batch file to edit/copy local file

Report •


#1
March 18, 2009 at 14:04:27

"hello hello hello, is there anybody in there..."

just nod if you can hear me

You don't need to REN; you need to COPY.

And you don't need to MD a directory named HOSTS. If you do you can't have a FILE called HOSTS.

=============================================
@echo off & setLocal EnableDelayedExpansion

pushd "C:\WINDOWS\system32\drivers\etc\hosts"
copy HOSTS HOSTS.bkp
>> HOSTS echo 192.168.1.11 somedomain.com #PRE


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
March 19, 2009 at 05:57:21

I have made some changes to my file it now reads

@echo off

MKDIR "C:\_bkup\HostsBkup"

COPY "C:\WINDOWS\system32\drivers\etc\hosts" C:\_bkup\HostsBkup

echo   
echo DONE Backing up and Editing Hosts
echo

and I need to learn how to add lines to the hosts file after my copy

hello hello hello, is there anybody in there...


Report •

#3
March 19, 2009 at 06:07:35

sorry about the late reply I left work before I rcvd that yesterday thank you for your help. I will test these changes and let you know how they turned out.

hello hello hello, is there anybody in there...


Report •

Related Solutions


Ask Question