代码之家  ›  专栏  ›  技术社区  ›  Ryan Leach

如果存在C:\directory\goto a else goto b问题windows XP批处理文件

  •  7
  • Ryan Leach  · 技术社区  · 14 年前

    无论何时我运行 code


    echo off  
    echo  
    echo (c) Ryan Leach 2010  
    echo Stockmaster Backup System for exclusive use of Riverland Paper Supplies  
    echo  
    echo Please ensure that all computers are out of stock master to the windows xp screen  
    echo and that the backup usb with the day of the week labeled on it is inserted  
    
    pause  
    
    IF EXIST D:\RPS_BACKUP\backups_to_zip\ goto zipexist else goto zipexistcontinue  
    :zipexist  
    IF EXIST d:\RPS_BACKUP\backups_old\ rd /s /q D:\RPS_BACKUP\backups_old  
    echo backup did not complete last time, backup will restart from zip-usb phase.  
    pause  
    call zip  
    goto tidyup  
    :zipexistcontinue  
    
    IF EXIST D:\RPS_BACKUP\backups_old\ goto oldexists else oldexistscontinue  
    :oldexists  
    IF EXIST d:\RPS_BACKUP\backup_temp\ rename D:\RPS_BACKUP\backups_temp backups_to_zip  
    rd /s /q D:\RPS_BACKUP\backups_old  
    echo backup did not complete last time, backup will restart at the zip to usb phase.  
    pause  
    call zip  
    goto tidyup  
    :oldexistscontinue  
    
    IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists else goto tempexistscontinue  
    :tempexists  
    IF EXIST D:\RPS_BACKUP\backups_old\ goto backupfailed else goto tempexistscontinue  
    :backupfailed  
    @rd /s /q D:\RPS_BACKUP\backups_temp  
    echo backup did not complete last time, backup will restart from start.  
    pause  
    :tempexistscontinue  
    
    md D:\RPS_BACKUPS\backups_temp  
    xcopy \\user1\c\* D:\RPS_BACKUP\backups_temp\user1\c /h /e /z /f /r /i /s /k  
    IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
    xcopy C:\* D:\RPS_BACKUP\backups_temp\user2\c /h /e /f /r /i /s /k  
    IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
    xcopy \\user3\c\* D:\RPS_BACKUP\backups_temp\user3\c /h /e /z /f /r /i /s /k  
    IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
    call sub  
    call zip  
    :tidyup  
    rename D:\RPS_BACKUP\backups_to_zip backups  
    pause  
    goto :eof  
    
    :ErrorHandler  
    echo xcopyerrorcode is ERRORLEVEL contact ryan  
    pause  
    
    6 回复  |  直到 8 年前
        1
  •  21
  •   Joey Gumbo    14 年前

    IF EXIST D:\RPS_BACKUP\backups_to_zip\ (goto zipexist) else goto zipexistcontinue
    

    在您的情况下,解析器永远不会看到 else 属于 if 因为 goto echo 而不是 转到 .

    另外,使用括号将允许您直接使用语句,而无需跳转(尽管我无法重写您的代码以实际使用结构化编程技术;也许它太早了,或者它本身不适合块结构(因为代码现在是这样)。

        2
  •  7
  •   Prutswonder    14 年前

    else 部分,尝试移除 其他的 并将命令放在新行上。这样地:

    IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists
    goto tempexistscontinue  
    
        3
  •  6
  •   Gabe Timothy Khouri    14 年前

    if /? ):

    The ELSE clause must occur on the same line as the command after the IF.  For
    example:
    
        IF EXIST filename. (
            del filename.
        ) ELSE (
            echo filename. missing.
        )
    
    The following would NOT work because the del command needs to be terminated
    by a newline:
    
        IF EXIST filename. del filename. ELSE echo filename. missing
    
    Nor would the following work, since the ELSE command must be on the same line
    as the end of the IF command:
    
        IF EXIST filename. del filename.
        ELSE echo filename. missing
    
        4
  •  2
  •   Carl Smotricz    14 年前

    如果我的理论是正确的,而你的其他理论却被忽视了,你最好还是这样做

    IF NOT EXIST file GOTO label
    

    第二,我隐约记得在测试目录是否存在时出现了某种bug。如果您可以测试该目录中是否存在一个文件,那么您的生活就会更轻松。如果没有可以确定的文件,可以尝试附加设备文件名(这在Win95、IIRC之前都是如此) NUL 你的目录名,例如。

    IF NOT EXIST C:\dir\NUL GOTO ...
    
        5
  •  1
  •   Ryan Leach    6 年前
    @echo off
    
    :START
    rmdir temporary
    cls
    IF EXIST "temporary\." (echo The temporary directory exists) else echo The temporary directory doesn't exist
    echo.
    dir temporary /A:D
    pause
    
    echo.
    echo.
    echo Note the directory is not found
    echo.
    echo Press any key to make a temporary directory, cls, and test again
    pause
    
    Mkdir temporary
    cls
    IF EXIST "temporary\." (echo The temporary directory exists) else echo The temporary directory doesn't exist
    echo.
    dir temporary /A:D
    pause
    echo.
    echo press any key to goto START and remove temporary directory 
    pause 
    
    goto START
    
        6
  •  0
  •   malenkiy_scot    12 年前

    要检查目录,不应使用以下内容:

    if exist c:\windows\
    

    要正常工作,请使用:

    if exist c:\windows\\.
    

    注意结尾的“.”。