How to edit files using batch script?
Ever wondered how to edit multiple files with a single script? Well I did, and I am sure everyone who is reading this post must have. Imagine there are hundreds of files (for e.g., signatures of all the employees) and you were asked to edit (eg., name of the company) all the signatures. It takes a great amount of time if we manually edit them individually. Well, the answer for our problem is a single batch script that can take care of our problem with just one click. Here’s an example:
1.Open Notepad, Copy and paste the below script :
REM — Prepare the Command Processor —
::BatchSubstitude – parses a File line by line and replaces a substring”
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] – string to be replaced
:: NewStr [in] – string to replace with
:: File [in] – file to be parsed
if “%*”==”” findstr “^::” “%~f0″&GOTO:EOF
for /f “tokens=1,* delims=]” %%A in (‘”type %3|find /n /v “””‘) do (
if defined line (
call set “line=echo.%%line:%~1=%~2%%”
for /f “delims=” %%X in (‘”echo.”%%line%%””‘) do %%~X
) ELSE echo.
2. Save the file as “xyz.bat”
The above script will parses a file line by line and will replaces a desired substring.
3. Open cmd prompt >> type the following command:
c:\xyz.bat “text-to-replace” text-to-replace-with c:\FileToEdit.txt > c:\Edited_file.txt
xyz.bat is the batch script we have created in Step 1; FileToEdit.txt is the file you would like to edit; and Edited_File.txt is an edited file.
The command will edit the file (FileToEdit.txt) and output into a new file (Edited_File.txt).
This script can also edit an htlml file. I haven’t tried the script on other file formats (e.g., .aspx, .rtf, etc…). If anyone did, then please let me know if it works.
The only drawback of this script is, it cannot recognize the spaces in a string. I wasn’t able to figure out how to edit a complete sentence. Instead I used a lengthy but effective way to edit 2 or more strings in a file. Try the following script:
c:\xyz.bat “First-string-to-replace” string-to-replace-with c:\FileToEdit.txt > c:\Edited_temp.txt
“The above command will replace the first substring and output into ‘Edited_temp.txt’ file.”
c:\xyz.bat “Second-string-to-replace” string-to-replace-with c:\Edited_temp.txt > c:\Edited_final_file.txt
“The above command will replace the second substring from ‘Edited_temp.txt’ file and output into a new file i.e., ‘Edited_final_file.txt’ ”
“It will delete the ‘Edited_temp.txt’ file”
Do let me know, if anyone figured out how to replace a complete sentence using a single command line.
You can furhter manipulate the script accordingly to add any conditional statements etc… Hope this article helps.
Tip of the day: Test all your batch scripts in a Virtual machine. To know how to create a virtual machine, please visit http://technet.microsoft.com/en-us/library/cc708299(WS.10).aspx
Rule of the day: Do you understand the value of consistency?
Author: Syed Dilawer Mehdi – Software Developer/Network Administrator
6 Comments »