# [SOLVED] Create a batch script to change a text file



## flipmode62 (Feb 1, 2010)

Hello,

I will create a batch script for the first time.

I'd like to create a batch script to change a line in a text file every month. I looked for information on Internet before write this thread but I found anything. 

*I'd like to know what are commands lines to find a line in a text file and replace it by an other line? * I saw the command line *FIND* to search, but I didn't find the command line to change a line in a text file.

This is my text file:

```
<ConfigJob Name="Job1" JobID="413381" JobType="Command Script" Comments=" "> <...> <Parameters Script="configure terminal
service password-encryption
enable secret enable
exit
write memory" WriteToNVRAM="False" ConfigText="" ReportTitle="" Report="" Path="C:\Program Files\..."AllConfigs="FALSE"</Parameters><ImportOptions></ImportOptions><MappingColumns>
</MappingColumns><MappingRows></MappingRows></ConfigMgmtJob>
```
I want to replace the line *enable secret enable* by *enable secret passwordXX*, XX represents the month. I have to create an other file to put my new password every months and I use it to change the line in my text file.
Also, my batch script will change the password every months in my text file.

Thanks for your help.

Regards,

Flipmode


----------



## TheOutcaste (Mar 19, 2009)

*Re: Create a batch script to change a text file*

Because of the *<* and *>* symbols in the file, this would be difficult to read in, find one line, and change it in batch. VBScript would be much better.

With a short file like this you could just create a new file using the new password though.

You're sample file has unmatched quotes though, so some lines you would have to escape the *<* and *>* with a caret, others you won't. Line 5 in particular starts with a quote after *memory* but doesn't have a matching quote on the same line, so the *<* and *>* do not have to be escaped.
This would prompt for the password, create a temp file, then delete the original and rename the temp file.
You could also read the password from another file. The password cannot contain any of these symbols though: *<*, *>*, or *&*

```
@Echo Off
Set _TmpFile=tmp.txt
Set _OrgFile=Job.txt
Set /P _NewPass=Enter new password: 
>"%_TmpFile%" Echo.^<ConfigJob Name="Job1" JobID="413381" JobType="Command Script" Comments=" "^> ^<...^> ^<Parameters Script="configure terminal
>>"%_TmpFile%" Echo.service password-encryption
>>"%_TmpFile%" Echo.enable secret %_NewPass%
>>"%_TmpFile%" Echo.exit
>>"%_TmpFile%" Echo.write memory" WriteToNVRAM="False" ConfigText="" ReportTitle="" Report="" Path="C:\Program Files\..."AllConfigs="FALSE"</Parameters><ImportOptions></ImportOptions><MappingColumns>
>>"%_TmpFile%" Echo.^</MappingColumns^>^<MappingRows^>^</MappingRows^>^</ConfigMgmtJob^>
Del "%_OrgFile%"
Ren "%_TmpFile%" "%_OrgFile%"
```
Or use this VBScript. Save this file with a *.vbs* extension. You can double click it to run, or run from the command Prompt using *cscript <filename.vbs>* where *<filename.vbs>* is the name you saved the script as
The new password is the first line in the *newpassword.txt* file.

```
Const ForReading = 1
Const ForWriting = 2
strNewPassFile = "newpassword.txt"
strFileName = "Job.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strNewPassFile, ForReading)
strNewPass = objFile.ReadLine
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If LCase(Left(strLine,13)) = "enable secret" Then
        strLine = "enable secret " & strNewPass
    End If
    strContents = strContents & strLine & vbCrLf
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write(strContents)
objFile.Close
```


----------



## flipmode62 (Feb 1, 2010)

*Re: Create a batch script to change a text file*

Hello,

Thank you for your reply and thank you very much for your scripts.
I prefer the VBScript which allow to put the new password in a text file and to use it.
I scheduled a task to run this VBScript every months and it works perfectly 

Thanks for yor help.

Flipmode


----------



## TheOutcaste (Mar 19, 2009)

*Re: Create a batch script to change a text file*

You're Welcome!

If your issue has been resolved you can mark this thread Solved by using the Thread Tools at the Top Right of this thread (above the first post) :grin:

Jerry


----------

