Skip to content

Latest commit

Β 

History

History
executable file
Β·
135 lines (99 loc) Β· 2.76 KB

File metadata and controls

executable file
Β·
135 lines (99 loc) Β· 2.76 KB

πŸ” SHIFT Command and SET /P for User Input

  • Batch scripting can do more than static automation β€” with tools like SHIFT and SET /P, you can process dynamic input and build interactive scripts.

    πŸ”„ The SHIFT Command

    • The SHIFT command shifts all command-line arguments one position to the left.

      It enables batch scripts to handle more than %9 arguments β€” critical for loops and bulk operations.

      βœ… Syntax

      SHIFT [/n]
      • /n β€” Optional, specifies the starting argument to shift from (rarely used in practice)

      πŸ§ͺ Basic Example: Print All Arguments

      @ECHO OFF
      :loop
      IF "%1"=="" GOTO end
      ECHO Arg: %1
      SHIFT
      GOTO loop
      :end

      πŸ“Œ Run it like this:

      showargs.bat one two three four five six seven eight nine ten eleven

      βœ… Output:

      Arg: one
      Arg: two
      ...
      Arg: eleven
      

      🧠 Use Case: Processing a List of Files

      @ECHO OFF
      :process
      IF "%1"=="" GOTO done
      ECHO Processing file: %1
      :: Place your logic here
      SHIFT
      GOTO process
      :done
      ECHO All files processed.

🧾 SET /P – Accepting User Input

  • SET /P allows your batch script to prompt the user for input β€” like a simple interactive prompt.

    βœ… Syntax

    SET /P variable=Prompt text

    πŸ”Ή Simple Example

    @ECHO OFF
    SET /P name=Enter your name: 
    ECHO Hello, %name%!

    πŸ–₯️ Output:

    Enter your name: Alice
    Hello, Alice!
    

    πŸ›‘οΈ With Validation

    @ECHO OFF
    :ask
    SET /P age=Enter your age: 
    IF "%age%"=="" (
        ECHO Age is required!
        GOTO ask
    )
    ECHO Your age is: %age%

    πŸ§ͺ Password Entry Simulation (not secure)

    SET /P pass=Enter password: 
    IF "%pass%"=="secret" (
        ECHO Access granted.
    ) ELSE (
        ECHO Access denied.
    )

    🧠 Combining SHIFT and SET /P

    You can combine both to create dynamic + interactive scripts:

    @ECHO OFF
    SET /P confirm=Start processing arguments? (Y/N): 
    IF /I NOT "%confirm%"=="Y" EXIT /B
    
    :loop
    IF "%1"=="" GOTO end
    ECHO Processing %1
    SHIFT
    GOTO loop
    :end