CMD重命名文件,如果有一定的前缀,如果所有文件都有(或没有)前缀,则有效,如果不同,则会失败

时间:2015-09-20 19:23:04

标签: file loops batch-file cmd file-rename

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

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

  1. XX.somegame.ELF用于从USB
  2. 加载
  3. PP.somegame.ELf用于从PS2s HDD上的每个游戏分区加载
  4. __.somegame.ELF用于从PS2s HDD上隐藏的每个游戏分区加载
  5. somegame.ELF用于从PS2s HDD上的__.POPS分区加载
  6. 此脚本应重命名要以相应格式使用的文件。

    如果所有.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脚本.....

1 个答案:

答案 0 :(得分:0)

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

但我认为问题是你实际上只测试了第一个文件。第一个for循环实际上从不循环。它只检查它获得的第一个文件并跳转到相应的“动作”循环。

您希望对每个文件进行测试并采取相应措施。你只需要一个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!"

    )
)