如果存在于For循环内

问题描述 投票:0回答:2

我在修改了这段代码以接受多个参数后,在使其工作时遇到了麻烦。我在第二个for循环处得到"(was unexpected at this time."。(第23行)。

有没有可能在for循环里面有这些if组?根据我所看到的,这是可能的。

取代if exist组,我可以用if NOT EXPRESS来代替。我可以直接使用IF NOT EXIST,但我不知道如何让它跳过当前项的循环中的其余代码,而继续下一项的循环。我无法找到如何做到这一点,只找到了如何完全退出循环,我不想这样做。

@echo off
:variables
set script_dir="E:\Plex Scripts\DVR"
set script1=MCEBuddyScriptv2.bat
set method2=GPU
set script2=ScanMedia%method2%.ps1
set extension=mkv
set logfile=post_processing_log.txt
set history=history.txt
:change_directory
cd /d %script_dir%
:method
setlocal enabledelayedexpansion
:counter
set argCount=0
for %%x in (%*) do (
    set /A argCount+=1
    set "argVec[!argCount!]=%%~x"
)
::How many videos we will attempt to process
echo Number of files to process: %argCount%
:loop_start
for /L %%i in (1,1,%argCount%) do (
    set fullpath=!argVec[%%i]!
    ::get new filepath (with new extension)
    REM :names
    for %%f in (!fullpath!) do set itemname=%%~nf
    for %%f in (!fullpath!) do set newpath=%%~dpnf.%extension%
    ::if logfile doesn't exist then create it
    REM :log_init
    if not exist %logfile% echo This log file is a detailed history of the DVR Post Process scipt. >> %logfile%
    if not exist %history% echo This is a history of the items processed with the DVR Post Process script. >> %history%
    ::check if input file exist
    REM :check
    if exist ( !fullpath! ) (
        REM :script1
        set state=Processing started
        echo %date% - %time% - !state! for !itemname! with %script1% >> %logfile%
        call %script1% !fullpath!
        set state=Processing finished
        echo %date% - %time% - !state! for !itemname! with %script1% >> %logfile%
        REM :script2
        set state=Processing started
        echo %date% - %time% - !state! for !itemname! with %script2% >> %logfile%
        powershell.exe -NoProfile -ExecutionPolicy Bypass -File %script2% -Path "!newpath!" -LimitCPU -AutoRepair -RemoveOriginal -RemoveRepaired -IAcceptResponsibility
        set state=Processing finished
        echo %date% - %time% - !state! for !itemname! with %script2% >> %logfile%
        REM :log_final
        echo %date% - %time% - Plex post processing script finished for !itemname!. The file will be moved and added to the library. >> %logfile%
        echo. >> %logfile%
        REM :history_final
        echo %date% - %time% - !itemname! >> %history%
    ) else (
        echo %date% - %time% - !itemname! does not exist. Error code 1. >> %logfile%
    )
)
:end
exit /b 0

编辑:把 "if exist ( !!!fullpath!)... "中的括号去掉就可以了。

我不知道为什么最初的时候,在放入for循环里面之前,这还能用。总之,这里是最终的代码。

@echo off

:variables
set script_dir="E:\Plex Scripts\DVR"
set script1=MCEBuddyScriptv2.bat
set method2=GPU
set script2=ScanMedia%method2%.ps1
set extension=mkv
set logfile=post_processing_log.txt
set history=history.txt

:change_directory
cd /d %script_dir%

:method
setlocal enabledelayedexpansion

:counter
set argCount=0
for %%x in (%*) do (
    set /A argCount+=1
    set "argVec[!argCount!]=%%~x"
)
::How many videos we will attempt to process
echo Number of files to process: %argCount%

:loop_start
for /L %%i in (1,1,%argCount%) do (
    set fullpath="!argVec[%%i]!"
    echo !fullpath!

    ::get new filepath (with new extension)
    :names
    for %%f in (!fullpath!) do set itemname=%%~nf
    echo !itemname!
    for %%f in (!fullpath!) do set newpath=%%~dpnf.%extension%
    echo !newpath!

    ::if logfile doesn't exist then create it
    :log_init
    if not exist %logfile% echo This log file is a detailed history of the DVR Post Process scipt. >> %logfile%
    if not exist %history% echo This is a history of the items processed with the DVR Post Process script. >> %history%

    ::check if input file exist
    :check
    if exist !fullpath! (
        :script1
        set state=Processing started
        echo %date% - %time% - !state! for !itemname! with %script1% >> %logfile%
        call %script1% !fullpath!
        set state=Processing finished
        echo %date% - %time% - !state! for !itemname! with %script1% >> %logfile%

        :script2
        set state=Processing started
        echo %date% - %time% - !state! for !itemname! with %script2% >> %logfile%
        powershell.exe -NoProfile -ExecutionPolicy Bypass -File %script2% -Path "!newpath!" -LimitCPU -AutoRepair -RemoveOriginal -RemoveRepaired -IAcceptResponsibility
        set state=Processing finished
        echo %date% - %time% - !state! for !itemname! with %script2% >> %logfile%

        :log_final
        echo %date% - %time% - Plex post processing script finished for !itemname!. The file will be moved and added to the library. >> %logfile%
        echo. >> %logfile%

        :history_final
        echo %date% - %time% - !itemname! >> %history%
    ) else (
        echo %date% - %time% - !itemname! does not exist. Error code 1. >> %logfile%
    )
)
:end
exit /b 0
for-loop batch-file if-statement nested
2个回答
0
投票
if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

没有括号的 filename 令牌。使用

if exist !fullpath! (

甚至

if exist "!fullpath!" (
© www.soinside.com 2019 - 2024. All rights reserved.