william123456
william123456

Reputation: 65

rename & replace characters windows batch script

x

Not success in rename the file in windows batch script. please kindly advise. appreciate the help

set "source=C:\Users\Processed"

    for /f "delims=" %%A in ('findstr /s /m /l /c:"949010" "%source%\*"') 
        do (
        set "fname=%%A"
            setlocal enabledelayedexpansion
            set "fname=!fname:{=!"
            ren %%A "!fname!"
            endlocal &
        copy /y "%source%\%fname%" "C:\Users\949010" )

Upvotes: 0

Views: 106

Answers (2)

william123456
william123456

Reputation: 65

set "source=C:\Users\Processed"

for /f "delims=" %%A in ('findstr /s /m /l /c:"949010" "%source%\*"') do (
    set "fname=%%~nxA"
    setlocal enabledelayedexpansion
    set "fname=!fname:{=!"
    ren "%%A" "!fname!"
    copy /y "%%A" "C:\Users\949010"
    endlocal
)

Upvotes: 0

Aacini
Aacini

Reputation: 67216

You have 5 small errors in your code when I reviewed it! BTW it is a bad idea to modify the original code with no advice of what you did in the question itself...

  • The fname variable must contain just the name and extension of the file, so ~nx modifier must be used in %%A variable.
  • The do clause must be placed in the same line of the for command.
  • In the ren command both names should be enclosed in quotes (not an error in this case).
  • The & sign is used to separate two commands in the same line. It should be a command after the &.
  • All references to a variable that changes inside for loop must be enclosed in exclamation-marks.
  • The endlocal command must be placed after the last variable with exclamation-marks.

I also modified the justification of the code in the standard way.

This is the corrected code:

set "source=C:\Users\Processed"

for /f "delims=" %%A in ('findstr /s /m /l /c:"949010" "%source%\*"') do (
    set "fname=%%~nxA"
    setlocal enabledelayedexpansion
    set "fname=!fname:{=!"
    ren "%%A" "!fname!"
    copy /y "%source%\!fname!" "C:\Users\949010"
    endlocal
)

Upvotes: 2

Related Questions