开发者

Go Back to Start If no input is entered (Bat File) (#2)

开发者 https://www.devze.com 2023-03-21 20:41 出处:网络
This is 开发者_运维百科a to follow-up question: Go Back to Start If no input is entered (Bat File)

This is 开发者_运维百科a to follow-up question: Go Back to Start If no input is entered (Bat File) Now for another final Touch and my file is perfect :) Here's the relevant code part:

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

IF I Set Non Input, the file makes all the relevant files in the same folder and with the same extension (as in attrib +s +h +r *.bat) ,(in this case, bat) in to system files

I apologize for the bad wording

The complete procedure of this script (it's not the all script onlly on part of it)

: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. Always take filename it quotes becouse it can have spaces.
  2. Delete quotes from filename. More: set /?

    set F=%F:"=%
    
    1. Make sure the input is not empty. Command 'if not "%F%"==""' - checks that input isn't empty. Command 'if EXIST "%F%"' checks that file exist.

      :: 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 you have too many commands that can't be runned without input you can add procedure in end of file and call it

      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. Always say to user, where is his error

    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. In start of file add 'goto Input'. After each procedure (:Hide :Show :Exit) add 'goto Input' to prevent crazy parsing.

    :: 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
    
0

精彩评论

暂无评论...
验证码 换一张
取 消