如果有特定前缀,CMD 重命名文件,如果所有文件都有(或没有)前缀,则工作,如果不同,则即使有一个也会失败

CMD rename files if has certain prefix, works if ALL files have (or don't have) prefix, fails if even one if different

我正在为在改装的 PlayStation 2 上使用 POPStarter 的人制作一个 .BAT 脚本。

使用它需要 .ELF 文件具有四种格式之一(取决于游戏的加载方式。)

  1. XX.somegame.ELF 用于从 USB 加载
  2. PP.somegame.ELf 用于从 PS2 硬盘上的每个游戏分区加载
  3. __.somegame.ELF 用于从 PS2 硬盘上每个游戏的隐藏分区加载
  4. somegame.ELF 用于从 PS2 硬盘上的 __.POPS 分区加载

此脚本应重命名要以相应格式使用的文件。

我有我的代码,如果所有 .ELF 文件都有一个前缀(这三个文件中的哪一个无关紧要)或者如果它们都缺少前缀,那么命令运行完美,但是即使有一个不同的文件(都有前缀,一个没有,或者都没有前缀,一个有)所有文件都被视为不同的文件。

我的代码是:

@echo off
setlocal ENABLEDELAYEDEXPANSION
Mode con: cols=115 lines=40
color 17
set "logfile=ELF_Test_Log.txt"
Set "Replace1=XX"
Set "Replace2=__"
Set "Replace3=PP"
SET "_prefix=%1:~0,3%"
goto start
:1
set load=
set/a loadnum=0
if Not Exist "*.ELF" goto Loading1b
:Loading1a
set load=%load%[0]
cls
echo.
echo Processing... Please Wait...
echo ------------------------------------------------------------
echo %load% 
echo ------------------------------------------------------------
ping localhost -n 1 >nul
set/a loadnum=%loadnum% +1
if %loadnum%==20 goto done1
goto Loading1a
:done1
cls
echo.
echo Processing Complete!
echo ------------------------------------------------------------
echo [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] 
echo ------------------------------------------------------------
echo.
echo.
pause
goto menu
:Loading1b
cls
echo %date% - %time%  No ELF files >> "%logfile%" 2>&1
echo.
echo.
echo There are currently NO .ELF files to Rename
echo.
echo.
echo.
pause
goto menu

:2
set load=
set/a loadnum=0
if Not Exist "*.ELF" goto Loading2b
:Loading2a
set load=%load%[0]
cls
echo.
echo Processing... Please Wait...
echo ------------------------------------------------------------
echo %load% 
echo ------------------------------------------------------------
ping localhost -n 1 >nul
set/a loadnum=%loadnum% +1
if %loadnum%==20 goto done2
goto Loading2a
:done2
cls
echo.
echo Processing Complete!
echo ------------------------------------------------------------
echo [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] 
echo ------------------------------------------------------------
echo.
echo.
pause
goto menu
:Loading2b
cls
echo %date% - %time%  No ELF files >> "%logfile%" 2>&1
echo.
echo.
echo There are currently NO .ELF files to Rename
echo.
echo.
echo.
pause
goto menu

:3
set load=
set/a loadnum=0
if Not Exist "*.ELF" goto Loading3b
:Loading3a
set load=%load%[0]
cls
echo.
echo Processing... Please Wait...
echo ------------------------------------------------------------
echo %load% 
echo ------------------------------------------------------------
ping localhost -n 1 >nul
set/a loadnum=%loadnum% +1
if %loadnum%==20 goto done3
goto Loading3a
:done3
cls
echo.
echo Processing Complete!
echo ------------------------------------------------------------
echo [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] 
echo ------------------------------------------------------------
echo.
echo.
pause
goto menu
:Loading3b
echo %date% - %time%  No ELF files >> "%logfile%" 2>&1
cls
echo.
echo.
echo There are currently NO .ELF files to Rename
echo.
echo.
echo.
pause
goto menu

:4
set load=
set/a loadnum=0
if Not Exist "*.ELF" goto Loading4b
:Loading4a
set load=%load%[0]
cls
echo.
echo Processing... Please Wait...
echo ------------------------------------------------------------
echo %load% 
echo ------------------------------------------------------------
ping localhost -n 1 >nul
set/a loadnum=%loadnum% +1
if %loadnum%==20 goto done4
goto Loading4a
:done4
cls
echo.
echo Processing Complete!
echo ------------------------------------------------------------
echo [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] 
echo ------------------------------------------------------------
echo.
echo.
pause
goto menu
:Loading4b
echo %date% - %time%  No ELF files >> "%logfile%" 2>&1
cls
echo.
echo.
echo There are currently NO .ELF files to Rename
echo.
echo.
echo.
pause
goto menu
:start
echo %date% - %time% Start Program >> "%logfile%" 2>&1
echo Welcome to renaming Utility
pause
goto menu
:menu
echo %date% - %time% Menu Loaded >> "%logfile%" 2>&1
cls
echo [1] USB formated       [2] Hidden Partition(individual)
echo [3] OLD HDD Method     [4] NEW HDD Method
echo.
echo [X] Exit
CHOICE /C 1234X
IF ERRORLEVEL 5 goto exit
IF ERRORLEVEL 4 goto loading4
IF ERRORLEVEL 3 goto loading3
IF ERRORLEVEL 2 goto loading2
IF ERRORLEVEL 1 goto loading1
:loading1
echo %date% - %time% - Checking If File has Prefix >> "%logfile%" 2>&1
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%~na
set F=!F:~3!
if %%a == %Replace1%.!F!%%~xa goto 1b else
if %%a == %Replace2%.!F!%%~xa goto 1b else
if %%a == %Replace3%.!F!%%~xa goto 1b else goto 1a
)
:1a
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
echo %date% - %time% - File %%a has no Prefix, Adding %Replace1%. >> "%logfile%" 2>&1
ren "%%a" "%Replace1%.%%a" >> "%logfile%" 2>&1
)
pause
goto 1
:1b
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%a
set F=!F:~3!
echo %date% - %time% - File %%a has a prefix, renaming to %Replace1%.!F! >> "%logfile%" 2>&1
ren "%%~a" "%Replace1%.!F!" >> "%logfile%" 2>&1
)
goto 1
:loading2
echo %date% - %time% Checking If File has Prefix >> "%logfile%" 2>&1
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%~na
set F=!F:~3!
if %%a == %Replace1%.!F!%%~xa goto 2b else
if %%a == %Replace2%.!F!%%~xa goto 2b else
if %%a == %Replace3%.!F!%%~xa goto 2b else goto 2a
)
:2a
for /f "delims=" %%a in ('dir /a-d /b *.elf') do (
set F=%%a
echo %date% - %time% - File %%a has no Prefix, Adding %Replace2%. >> "%logfile%" 2>&1
ren "%%a" "%Replace2%.%%a" >> "%logfile%" 2>&1
goto 2
:2b
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%a
set F=!F:~3!
echo %date% - %time% - File %%a has a prefix, renaming to %Replace2%.!F! >> "%logfile%" 2>&1
ren "%%~a" "%Replace2%.!F!" >> "%logfile%" 2>&1
)
goto 2
:loading3
echo %date% - %time% - Checking If File has Prefix >> "%logfile%" 2>&1
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%~na
set F=!F:~3!
if %%a == %Replace1%.!F!%%~xa goto 3b else
if %%a == %Replace2%.!F!%%~xa goto 3b else
if %%a == %Replace3%.!F!%%~xa goto 3b else goto 3a
)
:3a
For /f "delims=" %%a in ('dir /a-d /b *.elf') do (
set F=%%a
echo %date% - %time% - File %%a has no Prefix, Adding %Replace3%. >> "%logfile%" 2>&1
ren "%%a" "%Replace3%.%%a" >> "%logfile%" 2>&1
goto 3
:3b
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%a
set F=!F:~3!
echo %date% - %time% - File %%a has a prefix, renaming to %Replace3%.!F! >> "%logfile%" 2>&1
ren "%%~a" "%Replace3%.!F!" >> "%logfile%" 2>&1
)
goto 3
:loading4
echo %date% - %time% - Checking If File has Prefix >> "%logfile%" 2>&1
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%~na
set F=!F:~3!
if %%a == %Replace1%.!F!%%~xa goto 4b else
if %%a == %Replace2%.!F!%%~xa goto 4b else
if %%a == %Replace3%.!F!%%~xa goto 4b else goto 4a
)
:4a
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (echo %date% - %time% - File %%a has no Prefix, Should Work with NEW HDD install type >> "%logfile%" 2>&1)
pause
goto menu
:4b
For /f "delims=" %%a in ('dir /a-d /b *.elf') Do (
set F=%%a
set F=!F:~3!
echo %date% - %time% - File %%a has a Prefix, Now removing it >> "%logfile%" 2>&1
ren "%%a" "!F!" >> "%logfile%" 2>&1
)
goto 4
:exit
cls
echo %date% - %time% End Program >> "%logfile%" 2>&1
pause
exit

:loading部分出现了四次(对应4个期望的结果)

使用两个文件,当它们具有相同的结构时,这是读出的日志

Sun 09/20/2015 - 14:19:03.56 Start Program 
Sun 09/20/2015 - 14:19:05.08 Menu Loaded 
Checking If File has Prefix 
File C_C[CD1].elf has no Prefix, Adding XX. 
File C_C[CD2].elf has no Prefix, Adding XX. 
Sun 09/20/2015 - 14:19:07.84 Menu Loaded 
Sun 09/20/2015 - 14:19:12.62 End Program

这是当所有 somegame.ELF 而只有一个 XX.somegome.ELF:

时的日志输出
Sun 09/20/2015 - 14:28:17.38 Start Program 
Sun 09/20/2015 - 14:28:18.83 Menu Loaded 
Checking If File has Prefix 
File C_C[CD1].elf has a prefix, renaming to XX.[CD1].elf 
File XX.C_C[CD2].elf has a prefix, renaming to XX.C_C[CD2].elf 
Sun 09/20/2015 - 14:28:21.02 Menu Loaded 
Sun 09/20/2015 - 14:28:22.37 End Program

我该如何补救这最后遗漏的部分?

编辑 - 添加了完整的 BAT 脚本 .....

你并没有真正告诉我们你的脚本应该做什么。

但我认为问题在于您实际上只测试了第一个文件。第一个 for 循环实际上从不循环。它只是检查它获得的第一个文件并跳转到相应的 "action" 循环。

您想测试每个文件并据此采取行动。为此,您只需要一个 for 循环:

for /f "delims=" %%a in ('dir /a-d /b *.elf') do (

    set F=%%~na
    set F=!F:~3!
    set ADD=1
    if %%a == %Replace1%.!F!%%~xa set ADD=0
    if %%a == %Replace2%.!F!%%~xa set ADD=0
    if %%a == %Replace3%.!F!%%~xa set ADD=0

    if !ADD! == 1 (

        echo File %%a has no Prefix, Adding %Replace1%. 
        ren "%%a" "%Replace1%.%%a"
        pause

    ) else (

        set F=%%a
        set F=!F:~3!
        echo File %%a has a prefix, renaming to %Replace1%.!F! 
        ren "%%~a" "%Replace1%.!F!"

    )
)