Computing.Net > Forums > Programming > batch file to edit/copy local file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

batch file to edit/copy local file

Reply to Message Icon

Name: cgamsjager
Date: March 18, 2009 at 13:13:23 Pacific
OS: XP professional
CPU/Ram: 3.25gb
Subcategory: Batch
Comment:

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...



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 18, 2009 at 14:04:27 Pacific
Reply:

"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


0

Response Number 2
Name: cgamsjager
Date: March 19, 2009 at 05:57:21 Pacific
Reply:

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...


0

Response Number 3
Name: cgamsjager
Date: March 19, 2009 at 06:07:35 Pacific
Reply:

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...


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Write the following MS-DO... set result of FINDSTR in ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file to edit/copy local file

Batch script to search/copy/replace www.computing.net/answers/programming/batch-script-to-searchcopyreplace/16663.html

Batch File Help www.computing.net/answers/programming/batch-file-help/9077.html

batch file to find and copy www.computing.net/answers/programming/batch-file-to-find-and-copy/9236.html