windowsfor-loopbatch-file

DO Error during String Replacement using Windows Batch Script


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

Solution

  • For anyone interested, the following is the corrected and working script with an enhancement to preserve blank lines in the converted output file.

    @echo off
    SetLocal EnableDelayedExpansion
    
    rem -- Define the target directory and file pattern
    set "targetDir=C:\tcVISION\Config\File Store"
    set "filePattern=*.*"
    
    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 -- Redirect converted content to a temp file
        > "%targetDir%\!tempFile!" (
            rem -- Process each line of the current file; retaining blank lines by
            rem --   adding a line number and colon to the beginning of each line.
            for /f "delims=" %%a in ('findstr /n "^" "%%f"') do (
                set "srcline=%%a"
                rem -- Replace the string in the current line
                set "srcline=!srcline:%searchString%=%replaceString%!"
                rem -- remove line number and colon
                set "srcline=!srcline:*:=!"
                rem -- preserve blank lines
                echo(!srcline!
            )
        )
    
        rem -- Delete the original file and rename the temporary file
        del "%%f"
        ren "%targetDir%\!tempFile!" "%%~nxf"
    )
    
    EndLocal
    
    echo Conversion complete.
    pause