1

I am trying to create the interactive bat file which ask for the folder details it is able to take the userchoice but after that it is not able to set the folder path given by useri.e. it takes the path till Desktop only.Below is my code for the same:

@echo off
    set /p UserInfo= "Do you have abc software(y/n)?  "
    echo %UserInfo% // here it is working as expected
    IF %UserInfo%==y (
        echo "Reply is true-----"
        set /p Path= "Please enter path for abc directory?  "
        echo %Path% //but here it takes the path till the desktop only(C:\user\username\Desktop)
        CD  %Path%
        dir
        set /p Path1= "Please enter path1 directory path provided in package? "
        echo %Path1% //but here it takes the path till the desktop only(C:\user\username\Desktop)
        CD  %Path1%
        )
    IF %UserInfo%==n (
        echo "Reply is False**************"
        )
    pause

How to read the folder directive?

James Z
  • 12,209
  • 10
  • 24
  • 44
Pranav Shukla
  • 11
  • 2
  • 8
  • 1
    Use the `search` facility in the top bar to find references to `delayed expansion` - it's #1 FAQ. – Magoo Aug 28 '17 at 16:19
  • 1
    You need [delayed expansion](http://s64.com/nt/delayedexpansion.html) when setting and using a variable inside a (code block). –  Aug 28 '17 at 16:20
  • LotPings can you please provide the code for it? – Pranav Shukla Aug 28 '17 at 16:28
  • 1
    As a best practice you should avoid using `PATH` as a variable in your programs because `PATH` is already a system variable. – Squashman Aug 28 '17 at 17:08
  • Possible duplicate of [windows batch SET inside IF not working](https://stackoverflow.com/questions/9102422/windows-batch-set-inside-if-not-working) – indiv Aug 28 '17 at 17:18
  • 2
    another [possible duplicate[(https://stackoverflow.com/q/30282784/2152082). And DON'T use `path` as a variable name! – Stephan Aug 28 '17 at 18:15
  • @LotPings I guess you were in a hurry, and in your link, you typed `s64` instead of `ss64`. –  Aug 29 '17 at 02:36

1 Answers1

1

Hmm.. Please use the search bar as user Magoo, LotPings said.

Also, Stephan & Squashman mentioned, do not set a variable with the name path because there is a internal variable named path. If you rename it, other programs may not work properly.


What's DelayedExpansion?

When batch files are run, cmd process them line by line. The entire if statement get processed at once. That's why the variable are un-set.

Since we want cmd to process those variable at run-time, we will need to tell it to do so, by adding setlocal enableDelayedExpansion. This enables run-time variable expansion. To disable, just change enable to disable.

You may want to add it like so:

@echo off
setlocal enableDelayedExpansion
rem your code follows...

How To Make Variable Get Processed Run-Time?

Simply change %var% to !var!.

Please note that for loop metavariable %%n cannot be changed to !!n, since itself already implicated a delayed expansion.

Command-line arguments %n cannot be changed to !n. You may want to do this instead:

if "%var%"=="abc" (
    set variable=%1
    echo !variable!
)

SET /P Code Injection Cause Security Issue?!

If the input of %Userinput% is a==a format D:\ && echo, cmd sees:

if a==a
   do format D:\
   do echo ==y (

Which... formats your D drive. Adding quotes like if "%var%"=="abc" won't help since user can just escape the quote and execute the commands.

See here for more info.


SET /P Alternatives

You may want to consider CHOICE for single letter choice. It's command syntax is like so:

choice /c choices /n /cs /t timeout /d default_choice /m prompt
  • /n hides the list of options, letting /m to display it's own prompt
  • /cs == case-insensitive.

PATH Variable

Again mentioned above, PATH is a internal variable used by Windows and other programs. Mis-setting it may cause some Windows functions or programs to stop functioning properly.

Instead, use another variable name like programPath.