Syntax error in batch script if not exist command is used [closed]

℡╲_俬逩灬. 提交于 2021-02-11 12:29:25

问题


I'm getting a syntax error in the below script, Can someone help me? I'm a novice in batch scripting.

Error log: 

The syntax of the command is incorrect.

E:\Apps\Console\BatchFile>IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-Trade.file"

Code:

ECHO OFF
SETLOCAL EnableDelayedExpansion


SET "Stat=A"
SET FileDestinationLocation=%1
echo %Stat% -here
ECHO:
IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-Trade.file"
( 
SET "Stat=B"
echo %Stat% -here
ECHO Missing Daily Cycle-Trade File 
)
IF NOT EXIST "!FileDestinationLocation!IDH-DailyCycle-Security.file"    
( 
SET "Stat=B" 
ECHO Missing Daily Cycle-Security File
)
IF NOT EXIST "!FileDestinationLocation!IDH-DailyCycle-Issuer.file"      
( 
SET "Stat=B"
ECHO Missing Daily Cycle-Issuer File
)
IF NOT EXIST     "!FileDestinationLocation!IDH-DailyCycle-IssuerUC.file"    
( 
SET "Stat=B"
ECHO Missing Daily Cycle-IssueUC File
)


if %Stat% EQU "A"
ECHO All files available for Daily Cycle


DEL /q "!FileDestinationLocation!*.*"
IF ERRORLEVEL 1 GOTO CNT
: CNT
ECHO "Process Completed"

回答1:


The main issue with your code, as already alluded to, was that your opening parentheses must be on the same line as their preceding code, and separated by at least one space character, i.e. IF [condition] (

However, there were a few other inconsistencies, which I've tried to address in the rewrite below.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "FileDestinationLocation=%~1"
IF NOT DEFINED FileDestinationLocation (
    EXIT /B 1
) ELSE (
    IF "%FileDestinationLocation:~-1%" == "\" (
        SET "FileDestinationLocation=%FileDestinationLocation:~,-1%"
    )
)
IF NOT EXIST "%FileDestinationLocation%\" (
    ECHO Input directory does not exist
    EXIT /B 1
)

SETLOCAL ENABLEDELAYEDEXPANSION
    SET "Stat=A"
    ECHO %Stat% -here
    ECHO=
    IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-Trade.file" (
        SET "Stat=B"
        ECHO !Stat! -here
        ECHO Missing Daily Cycle-Trade File
    )
    IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-Security.file" (
        SET "Stat=B"
        ECHO Missing Daily Cycle-Security File
    )
    IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-Issuer.file" (
        SET "Stat=B"
        ECHO Missing Daily Cycle-Issuer File
    )
    IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-IssuerUC.file" (
        SET "Stat=B"
        ECHO Missing Daily Cycle-IssueUC File
    )
    IF "%Stat%" EQU "A" ECHO All files available for Daily Cycle
    DEL /A /F /Q "!FileDestinationLocation!\*.*" 2> NUL
    ECHO "Process Completed"
ENDLOCAL

GOTO :EOF

I have not enabled delayed exapansion at the beginning of the script, to ensure that input arguments which have ! characters are still passed without modification as the intended directory name.

I have also incorporated some code to ensure that input arguments, with, or without, trailing backslashes are treated equally. (You had some instances of !FileDestinationLocation!IDH-DailyCycle-… and some of !FileDestinationLocation!\IDH-DailyCycle-…)

The script is closed if the input directory was not provided or does not exist.

I have indented the script for readability, and also improved the DEL command options to allow for the deletion regardless of the files' attributes, (subject to permissions).




回答2:


The opening parentheses needs to be on the same line as the IF keyword.

Like this:

ECHO OFF
SETLOCAL EnableDelayedExpansion


SET "Stat=A"
SET FileDestinationLocation=%1
echo %Stat% -here
ECHO:

IF NOT EXIST "!FileDestinationLocation!\IDH-DailyCycle-Trade.file" ( 
SET "Stat=B"
echo !Stat! -here
ECHO Missing Daily Cycle-Trade File 
)

IF NOT EXIST "!FileDestinationLocation!IDH-DailyCycle-Security.file" ( 
SET "Stat=B" 
ECHO Missing Daily Cycle-Security File
)

IF NOT EXIST "!FileDestinationLocation!IDH-DailyCycle-Issuer.file" ( 
SET "Stat=B"
ECHO Missing Daily Cycle-Issuer File
)

IF NOT EXIST     "!FileDestinationLocation!IDH-DailyCycle-IssuerUC.file" ( 
SET "Stat=B"
ECHO Missing Daily Cycle-IssueUC File
)


if "%Stat%" EQU "A" (
ECHO All files available for Daily Cycle


DEL /q "!FileDestinationLocation!*.*"


IF ERRORLEVEL 1 GOTO CNT
)

: CNT
ECHO "Process Completed"


来源:https://stackoverflow.com/questions/65774000/syntax-error-in-batch-script-if-not-exist-command-is-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!