返回开始如果没有输入输入(蝙蝠文件)(#2)

时间:2011-07-21 16:34:28

标签: batch-file if-statement

这是一个后续问题: Go Back to Start If no input is entered (Bat File) 现在换另一个最后的Touch,我的文件很完美:) 这是相关的代码部分:

:: Delete variable %F%
SET "F="
set /p F=Folder or a File Trget:
attrib +s +h +r %F%

如果我设置非输入,该文件将所有相关文件放在同一文件夹中,并使用相同的扩展名(如attrib +s +h +r *.bat),(在本例中为bat)进入系统文件

我为糟糕的措辞道歉

这个脚本的完整过程(它不是onlly的所有脚本)

:Hide
@echo off
cls
Color 0c

ECHO.
ECHO.
:: Delete variable %F%
SET "F="
set /p F=Folder or a File Trget:
attrib +s +h +r %F%


@echo off
CLS
ECHO.
ECHO.
ECHO   ######################################################
ECHO   #                                                    #
ECHO   # 1 - Set Other System Attribute To a Folder or file #
ECHO   # 2 - Remove System Attribute From a Folder or file  #
Echo   # 3 - Exit                                           #
ECHO   #                                                    #
ECHO   ######################################################
ECHO.
ECHO.

:: Delete variable %A%
SET "A="
SET /P A=Set Your Choice And Press Enter: 
ECHO Loading .........

IF "%A%"=="1" GOTO Hide
IF "%A%"=="2" GOTO Show
IF "%A%"=="3" GOTO Exit

GOTO Hide

1 个答案:

答案 0 :(得分:1)

  1. 总是把文件名作为引用,因为它可以有空格。
  2. 从文件名中删除引号。更多:设置/?

    set F=%F:"=%
    
    1. 确保输入不为空。命令'if not“%F%”==“”' - 检查输入是否为空。命令'if EXIST“%F%”'检查该文件是否存在。

      :: Delete variable %F%
      SET "F="
      set /p F=Folder or a File Target:
      set F=%F:"=%
      
      if not "%F%"=="" if EXIST "%F%" attrib +s +h +r "%F%"
      
    2. 如果您有太多无法在没有输入的情况下运行的命令,您可以在文件末尾添加过程并调用它

      if not "%F%"=="" if EXIST "%F%" call :HideChecked
      
      :: There are your code. Remember, that call :Label always returns back & keeps batch execution. Read more: call /?
      :: So there your code ends. And new procedure starts.
      
      :: Prevent crazy parsing
      exit /b
      
      :HideChecked
          attrib +s +h +r %F%
      :: exit /b == return back
      exit /b
      
  3. 总是对用户说,他的错误在哪里

    if not "%F%"=="" if EXIST "%F%" call :HideChecked
    
    if "%F%"=="" (
        echo You must choose something
        REM in 'IF ()' your can use only REM for comments.
        REM >nul - means command 'pause' will print nothing (except errors)
        pause>nul
        goto Hide
    )
    if NOT EXIST "%F%" (
        echo No such file. File list:
        REM dir /? for help
        dir /P/B
        pause>nul
        goto Hide
    )
    
  4. 在文件开头添加'goto Input'。在每个过程(:Hide:Show:Exit)之后添加'goto Input'以防止疯狂解析。

    :: At start of file
    goto Input
    
    :: There can be some code
    
    :Show
        :: And there
    goto Input
    
    :Hide
        @echo off
        cls
        Color 0c
    
        ECHO.
        ECHO.
        :: Delete variable %F%
        SET "F="
        set /p F=Folder or a File Target:
            set F=%F:"=%
    
        if not "%F%"=="" if EXIST "%F%" call :HideChecked
        if "%F%"=="" (
            echo You must choose something
            REM in 'IF ()' your can use only REM for comments.
            REM >nul - means command 'pause' will print nothing (except errors)
            pause>nul
            goto Hide
        )
        if NOT EXIST "%F%" (
            echo No such file. File list:
            REM dir /? for help
            dir /P/B
            pause>nul
            goto Hide
        )
    
    goto Input
    
    :Input
                        @echo off
        CLS
        ECHO.
        ECHO.
        ECHO   ######################################################
        ECHO   #                                                    #
        ECHO   # 1 - Set Other System Attribute To a Folder or file #
        ECHO   # 2 - Remove System Attribute From a Folder or file  #
        Echo   # 3 - Exit                                           #
        ECHO   #                                                    #
        ECHO   ######################################################
        ECHO.
        ECHO.
    
        :: Delete variable %A%
        SET "A="
        SET /P A=Set Your Choice And Press Enter: 
    
        IF "%A%"=="1" GOTO Hide
        IF "%A%"=="2" GOTO Show
        IF "%A%"=="3" GOTO Exit
    
        echo You must choose something
        pause>nul
    
        GOTO Input
    
    :: Prevent crazy parsing
    exit /b
    
    :HideChecked
        attrib +s +h +r "%F%"
    :: exit /b == return back
    exit /b