-2

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
5
  • 2
    [without looking further] : Do NOT use ::-style comments within a loop - use rem there instead as ::-comments are LABELS which break the loop. Commented Nov 3 at 19:12
  • 1
    However, the AI fails at the rest as well. The complete code can't work as expected, as the set/quote syntax is used the wrong way. Commented Nov 3 at 20:50
  • @Magoo thanks, it came from the AI that way. I fixed it and, with a few more adjustments, I have it working now. Commented Nov 3 at 21:56
  • @Magoo or whomever. I notice that the working script strips out blank lines in the converted text files. Is there a way to preserve those blank lines? Commented Nov 3 at 22:39
  • for/f will ignore empty lines and those starting ;. The standard way to ovecome this is to use for/f "tokens=1*delims=?" %%b in ('type filename^|findstr /n "."') do if "%%c" equ "" (echo/) else (echo %%c) where ?=:` if you use findstr /n or ] if you use find /n /v "" Findstr will number the matching lines with linenum: and find with 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 ]. Commented Nov 4 at 11:44

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.