batch file - .bat renaming script error adding suffixes - Stack Overflow

I'm trying to get the last 5 characters off of each file in one folder I have (source directory),

I'm trying to get the last 5 characters off of each file in one folder I have (source directory), and then do the same thing with all the files in a different folder (target directory) but then add the 5 characters onto each folder in the target directory. Each file in the source directory corresponds with a file in the target directory, but I've got the ordering working. The only thing I can't get to work is getting each target directory to get its own suffix. For example, the first suffix is 00000, and it will try and rename all files 00000, instead of 00000, then 00200, etc.

@echo off
setlocal enabledelayedexpansion

REM Set directories
set "SOURCE_DIR=C:\Users\user\Downloads\105Images\105Images"
set "TARGET_DIR=C:\Users\user\Downloads\105_Images\105_Images"

REM File extensions
set "SRC_EXT=jpg"
set "TGT_EXT=csv"

REM Temporary storage for suffixes
set "TEMP_FILE=%TEMP%\suffixes.txt"
if exist "%TEMP_FILE%" del "%TEMP_FILE%"

echo Extracting suffixes from SOURCE_DIR...

REM Step 1: Extract last 5 characters from SOURCE_DIR filenames and save to a temp file
set "index=0"
for /f "delims=" %%f in ('dir /b /on "%SOURCE_DIR%\*.%SRC_EXT%"') do (
    set "filename=%%~nf"
    set "suffix=!filename:~-5!" 
    echo !suffix!>>"%TEMP_FILE%"
    echo [!index!] Stored suffix: !suffix! from file: %%f
    set /a index+=1
)

REM Read suffixes into memory properly
set "index=0"
for /f "tokens=*" %%s in (%TEMP_FILE%) do (
    set "suffixes[!index!]=%%s"
    echo Loaded suffix [!index!]: !suffixes[!index!]!
    set /a index+=1
)

REM Clean up temp file
del "%TEMP_FILE%"

REM Reset index
set "index=0"

echo Renaming files in TARGET_DIR...

REM Step 2: Loop through TARGET_DIR and rename files correctly
for /f "delims=" %%g in ('dir /b /on "%TARGET_DIR%\*.%TGT_EXT%"') do (
    set "target_filename=%%~ng"

    REM Get correct suffix from the suffixes array
    set "suffix=!suffixes[%index%]!"

    REM If suffix is found, rename the file
    if defined suffix (
        REM Construct new filename
        set "newname=!target_filename:~0,-4!!suffix!.%TGT_EXT%"

        REM Ensure unique file before renaming
        if exist "%TARGET_DIR%\!newname!" (
            echo ERROR: Duplicate file exists: "!newname!". Skipping...
        ) else (
            echo Renaming "%TARGET_DIR%\%%g" to "!newname!"
            ren "%TARGET_DIR%\%%g" "!newname!"
        )
    ) else (
        echo ERROR: No suffix found for "%TARGET_DIR%\%%g" (Index: !index!)
    )

    set /a index+=1
)

echo Done! Renamed all files in %TARGET_DIR%.
pause
exit /b %ERRORLEVEL%

I'm trying to get the last 5 characters off of each file in one folder I have (source directory), and then do the same thing with all the files in a different folder (target directory) but then add the 5 characters onto each folder in the target directory. Each file in the source directory corresponds with a file in the target directory, but I've got the ordering working. The only thing I can't get to work is getting each target directory to get its own suffix. For example, the first suffix is 00000, and it will try and rename all files 00000, instead of 00000, then 00200, etc.

@echo off
setlocal enabledelayedexpansion

REM Set directories
set "SOURCE_DIR=C:\Users\user\Downloads\105Images\105Images"
set "TARGET_DIR=C:\Users\user\Downloads\105_Images\105_Images"

REM File extensions
set "SRC_EXT=jpg"
set "TGT_EXT=csv"

REM Temporary storage for suffixes
set "TEMP_FILE=%TEMP%\suffixes.txt"
if exist "%TEMP_FILE%" del "%TEMP_FILE%"

echo Extracting suffixes from SOURCE_DIR...

REM Step 1: Extract last 5 characters from SOURCE_DIR filenames and save to a temp file
set "index=0"
for /f "delims=" %%f in ('dir /b /on "%SOURCE_DIR%\*.%SRC_EXT%"') do (
    set "filename=%%~nf"
    set "suffix=!filename:~-5!" 
    echo !suffix!>>"%TEMP_FILE%"
    echo [!index!] Stored suffix: !suffix! from file: %%f
    set /a index+=1
)

REM Read suffixes into memory properly
set "index=0"
for /f "tokens=*" %%s in (%TEMP_FILE%) do (
    set "suffixes[!index!]=%%s"
    echo Loaded suffix [!index!]: !suffixes[!index!]!
    set /a index+=1
)

REM Clean up temp file
del "%TEMP_FILE%"

REM Reset index
set "index=0"

echo Renaming files in TARGET_DIR...

REM Step 2: Loop through TARGET_DIR and rename files correctly
for /f "delims=" %%g in ('dir /b /on "%TARGET_DIR%\*.%TGT_EXT%"') do (
    set "target_filename=%%~ng"

    REM Get correct suffix from the suffixes array
    set "suffix=!suffixes[%index%]!"

    REM If suffix is found, rename the file
    if defined suffix (
        REM Construct new filename
        set "newname=!target_filename:~0,-4!!suffix!.%TGT_EXT%"

        REM Ensure unique file before renaming
        if exist "%TARGET_DIR%\!newname!" (
            echo ERROR: Duplicate file exists: "!newname!". Skipping...
        ) else (
            echo Renaming "%TARGET_DIR%\%%g" to "!newname!"
            ren "%TARGET_DIR%\%%g" "!newname!"
        )
    ) else (
        echo ERROR: No suffix found for "%TARGET_DIR%\%%g" (Index: !index!)
    )

    set /a index+=1
)

echo Done! Renamed all files in %TARGET_DIR%.
pause
exit /b %ERRORLEVEL%
Share asked Mar 11 at 7:00 titus loftintitus loftin 1 1
  • t would aid understanding to an potential responders if you could provide us with the initial 'test' directory contents, the results you are getting, and those expected. As for an initial viewing of your code, my advice is that you need to change echo !suffix!>>"%TEMP_FILE%", on line 23, to, >>"%TEMP_FILE%" echo !suffix!. Otherwise !suffix! will first be seen as a redirection handle number, and not the number string you intended to write. – Compo Commented Mar 11 at 13:05
Add a comment  | 

1 Answer 1

Reset to default 0
 set "suffix=!suffixes[%index%]!"

%index% will be the value of index at the time of parsing the for ... %%g ... loop.

Conceptually, you need set "suffix=!suffixes[!index!]!"

but that won't work.

You could try

CALL set "suffix=%%suffixes[!index!]%%"

or

for /f "tokens=2delims==" %%y in ('set index') do set "suffix=!suffixes[%%y]!"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744810414a4595024.html

相关推荐

  • batch file - .bat renaming script error adding suffixes - Stack Overflow

    I'm trying to get the last 5 characters off of each file in one folder I have (source directory),

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信