I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
views:
16319answers:
12I'm not aware of anything built-in, but you could make your own program that does that in about a minute using any programming language.
I don't think there's a way to do it with any built-in commands. I would suggest you download something like Gnuwin32 or UnxUtils and use the sed
command:
sed -c s/FOO/BAR/g filename
If you are on Windows version that supports .Net 2.0, I would replace your shell. PowerShell gives you the full power of .Net from the command line. There are many commandlets built in as well. The example below will solve your question. I'm using the full names of the commands, there are shorter aliases, but this gives you something to Google for.
Get-Content test.txt | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt
The following link has an example of search and replace using a pure batch file BatchFindAndReplace.
It uses a combination of FOR, FIND and CALL SET.
This is one thing that batch scripting just does not do well.
The script morechilli linked to will work for some files, but unfortunately it will choke on ones which contain characters such as pipes and ampersands.
VBScript is a better built-in tool for this task. See this article for an example: http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx
Take a look at http://stackoverflow.com/questions/127318/ which asked for a sed equivalent under Windows, should apply to this question as well. Executive summary:
- It can be done in batch file, but it's not pretty
- Lots of available third party executables that will do it for you, if you have the luxury of installing or just copying over an exe
- Can be done with VBScript or similar if you need something able to run on a Windows box without modification etc.
May be a little bit late, but I am frequently looking for similar stuff, since I don't want to get through the pain of getting software approved.
However, you usually use the FOR statement in various forms. Someone created a useful batch file that does a search and replace. Have a look here. It is important to understand the limitations of the batch file provided. For this reason I don't copy the source code in this answer.
Just used FART ("F ind A nd R eplace T ext" command line utility):
excellent little freeware for text replacement within a large set of files.
The setup files are here.
fart.exe -p -r -c -- C:\tools\perl-5.8.9\* @@APP_DIR@@ C:\tools
will preview the replacements to recursively do in the files of this Perl distribution.
Only problem: the FART website icon isn't exactly tasteful, refined nor elegant ;)
Here's a solution that I found worked on Win XP. In my running batch file, I included the following:
set value=new_value
:: Setup initial configuration
:: I use && as the delimiter in the file because it should not exist, thereby giving me the whole line
::
echo --> Setting configuration and properties.
for /f "tokens=* delims=&&" %%a in (config\config.txt) do (
call replace.bat "%%a" _KEY_ %value% config\temp.txt
)
del config\config.txt
rename config\temp.txt config.txt
The replace.bat file is as below. I did not find a way to include that function within the same batch file, because the %%a variable always seems to give the last value in the for loop.
replace.bat:
@echo off
:: This ensures the parameters are resolved prior to the internal variable
::
SetLocal EnableDelayedExpansion
:: Replaces Key Variables
::
:: Parameters:
:: %1 = Line to search for replacement
:: %2 = Key to replace
:: %3 = Value to replace key with
:: %4 = File in which to write the replacement
::
:: Read in line without the surrounding double quotes (use ~)
::
set line=%~1
:: Write line to specified file, replacing key (%2) with value (%3)
::
echo !line:%2=%3! >> %4
:: Restore delayed expansion
::
EndLocal
Enjoy!
-Chad
Power shell command works like a charm ( test.txt | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt )
Create file replace.vbs:
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
To use this revised script (which we’ll call replace.vbs) just type a command similar to this from the command prompt:
cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James "