Saved Bookmarks
| 1. |
Solve : .bat file making search and replace in text file? |
|
Answer» Does anyone know how to write a .bat that looks for a string in a text file and replaces it with something else? SET /?THANKS! I have tried this but it does not run as I expected. I have the file test.txt containing the single word "Charlie". the bat script looks as follows: set var= for /f "tokens=*" %%A in ('type test.txt') do set myVar=%%A echo %myvar% set str1=Charlie set str2=Bob %myVar:str1=str2% echo %myvar% _______________________________________ ______________________ Now when I run this it returns/echos: C:\Transfer>set var= C:\Transfer>for /F "tokens=*" %A in ('type test.txt') do set myVar=%A C:\Transfer>set myVar=Charlie C:\Transfer>echo Charlie Charlie C:\Transfer>set str1=Charlie C:\Transfer>set str2=Bob C:\Transfer>Charlie 'Charlie' is not recognized as an internal or external command, operable program or batch file. C:\Transfer>echo Charlie Charlie _______________________________________ ___ so the %myVar:str1=str2% try to execute the content of str1?????? What did I miss???The sentence %myVar:str1=str2% return a new variable where it's been replaced the word "str1" for "str2". You would need: set myVar=%myVar:str1=str2% But in 'myVar' there isn't the word "str1". I have tried to put: set myVar=%myVar:%str1%=%str2%% but it seems that it doesn't admit variables within variables. If you run a subshell cmd /v: on, the next work: Code: [Select]@echo off for /f "tokens=*" %%A in (test.txt) do set myVar=%%A echo %myvar% set str1=Charlie set str2=Bob set myvar=!myVar:%str1%=%str2%! echo [ %myvar% ]Batch coding was never meant to do much more than run programs sequentially. Using batch as a programming language is like using a SPOON to dig the Panama Canal. Yes, it can be done, but it's not the best tool for the job. Code: [Select]Const ForReading = 1 Const ForWriting = 2 Const FileIn = "c:\test.txt" Const FileOut = "c:\test.txt" '<== Change to prevent overwrite of original file Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(FileIn, ForReading) strText = objFile.ReadAll objFile.Close strNewText = Replace(strText, "Charlie ", "Bob ") Set objFile = objFSO.OpenTextFile(FileOut, ForWriting) objFile.WriteLine strNewText objFile.Close Save the script with a vbs extension and run from the command line as: cscript scriptname.vbs. As WRITTEN, the script will overwrite the original file. You can modify this by changing the FileOut parameter. Just my 2¢ worth. 8-)Thanks Carlos! You are a Star!!! That works briliant! This really helped me out. Ah Yes. I have THOUGHT of looking in to vbs just have not had time yet. I might just do that in a hurry now though. Thanks.Since this is DOS FORUM here is a possible DOS solution ... At http://dostips.cmdtips.com/DtCodeBatchFiles.php you can find a section about [highlight]String Substitution[/highlight]. It shows a batch file that replaces text in a text file. The main task is being done by a few lines of code similar to this: Code: [Select]SETLOCAL ENABLEEXTENSIONS SETLOCAL ENABLEDELAYEDEXPANSION REM -- Parse each line REM -- Trick, add line numbers using find command, to preserve empty lines set src=<source file> set str1=<old string> set str2=<new string> for /f "tokens=1,* delims=]" %%a in ('"find /n /v "@#$PreserveEmptyLines$#@" "%src%""') do ( if "%%b"=="" ( echo. ) else ( set line=%%b call echo.!line:%str1%=%str2%! ) ) Due to the nature of the FOR command and Delayed Expansion, some restrictions for the content of the text file apply. Hope this information is useful for somebody I just figured out that find /v "" filename.txt returns all lines of a file, so the funny @#$PreserveEmptyLines$#@ is not needed. The for command can be written like this: for /f "tokens=1,* delims=]" %%a in ('"find /n /v "" "%src%""') do (... |
|