I obtained a batch script from Google AI for performing string replacement on the content of a list of text files in Windows. This is what I got--with a little tweaking, first. (Just testing at this time--so updating is commented out.)
:: @echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: Define the target directory and file pattern
set targetDir="C:\tcVISION\Config\File Store"
set filePattern="*.txt"
:: Define the string to find and the string to replace it with
set searchString="LIBRAT"
set replaceString="LIBRAQ"
:: Loop through each file matching the pattern in the target directory
for /d %%f in ("%targetDir%\%filePattern%") do (
echo Processing file: "%%f"
set tempFile="%%~nf.tmp"
:: Process each line of the current file
(for /f "delims=" %%a in ('type "%%f"') do (
set srcline="%%a"
:: Replace the string in the current line
set srcline="!srcline:%searchString%=%replaceString%!"
echo !srcline!
)) > "%targetDir%\%tempFile%"
:: Delete the original file and rename the temporary file
:: del "%%f"
:: ren "%targetDir%\%tempFile%" "%%~nf%%~xf"
)
ENDLOCAL
echo Conversion complete.
pause
When I run it, I get the following output and error message.
C:\Users\dlclark\Desktop>"convert file store.bat"
C:\Users\dlclark\Desktop>SETLOCAL ENABLEDELAYEDEXPANSION
C:\Users\dlclark\Desktop>set targetDir="C:\tcVISION\Config\File Store"
C:\Users\dlclark\Desktop>set filePattern="*.txt"
C:\Users\dlclark\Desktop>set searchString="LIBRAT"
C:\Users\dlclark\Desktop>set replaceString="LIBRAQ"
do was unexpected at this time.
C:\Users\dlclark\Desktop> (for /f "delims=" %a in ('type "%f"') do (
C:\Users\dlclark\Desktop>
I've tried a number of things but am unable to figure out what it is complaining about. I admit that I am no expert in Windows Batch scripts. But I did look up the FOR statement and read about the syntaxes supported.
What am I missing, please?
UPDATE: OK, the error was in using the AI-generated :: syntax for script comments. That creates broken labels that can interfere in block () code. So, here is the corrected and working script.
@echo off
SetLocal EnableDelayedExpansion
rem -- Define the target directory and file pattern
set "targetDir=C:\tcVISION\Config\File Store"
set "filePattern=*.txt"
rem -- Define the string to find and the string to replace it with
set "searchString=LIBRAT"
set "replaceString=LIBRAQ"
rem -- Loop through each file matching the pattern in the target directory
for %%f in ("%targetDir%\%filePattern%") do (
echo Processing file: "%%f"
set "tempFile=%%~nf.tmp"
rem -- Process each line of the current file
(for /f "delims=" %%a in ('type "%%f"') do (
set "srcline=%%a"
rem -- Replace the string in the current line
set "srcline=!srcline:%searchString%=%replaceString%!"
echo !srcline!
)) > "%targetDir%\!tempFile!"
rem -- Delete the original file and rename the temporary file
del "%%f"
ren "%targetDir%\!tempFile!" "%%~nxf"
)
EndLocal
echo Conversion complete.
pause
::-style comments within a loop - useremthere instead as::-comments are LABELS which break the loop.for/fwill ignore empty lines and those starting;. The standard way to ovecome this is to usefor/f "tokens=1*delims=?" %%b in ('type filename^|findstr /n "."') do if "%%c" equ "" (echo/) else (echo %%c) where?=:` if you usefindstr /nor]if you usefind /n /v ""Findstr will number the matching lines withlinenum:andfindwith those options will match all lines NOT being "", prefixing each non-match with[linenum]. Which you choose depends on whether or not you have lines starting:or].