I'm trying to save a variable to text file, but i'm failing:

 echo. && echo Where is your folder ?
 set /p userinputpath = Type input[Drive][Path]:
 echo "%userinputpath%" >> folder.txt   
 IF EXIST "%userinputpath%" (goto checkexe) REM goto check .exe
 echo Incorrect input & goto folder

Trying to save the userinputpath variable -> input looks like this: C:\Program Files (x86)\Subfolder\Subfolder

If i change the variable to string, it's working. What is the problem ?

  • Lose the spaces around = in the set /p statement - set /p userinputpath=Type input[Drive][Path]: – Michael Harvey May 20 at 21:35

I would use dobblequotes in the set /p, and also check if the input is a folder, by using dir /a-a with operator &&and || for to goto :label A or B

If exist can return True in cases where the user reports a file, to ensure that the input from a folder is passed.

For echo\ input>... command, you can also use "dobblequotes" to avoid any potential errors that may arise if the user enters something like &, |, or another, even if accidentally, and if you need save one at time, replace >>to >.

Obs.: See Conditional Execution linked below!

echo\ variable >> == strings output will be append to file
echo\ variable >  == overwrite string output to file (replace all content)

echo; & echo;Where is your folder?
set /p "userinputpath=Type input[Drive][Path]: "

dir /b/a-a "%userinputpath%" >nul 2>nul && (
     echo;"%userinputpath%">"%temp%\folder.txt" & goto :checkexe
   ) || set /p "'=Incorrect input!"<nul & set "userinputpath="
     timeout -1 & echo; & goto :folder


But, what would happen if the user (always this guy) just typed enter/retunr? Or, suddenly choose to use a variable for the input folder?

  • Like %temp%, %windir%, %userprofile%, %appdata%, etc...

In this supposed potential case, and with minimal changes to your code, I will try to:

@echo off

echo; & echo;Where is your folder?
set /p "userinputpath=Type input[Drive][Path]: " || goto :folder

<con: call dir/b/a-a "%userinputpath%\*" 2>nul >nul && (
      call echo;"%userinputpath%">"%temp%\folder.txt" & goto :checkexe
     ) || ( set /p "'=Incorrect input!"<nul && set "userinputpath=" <nul
      timeout -1 & goto :folder


If user only hit enter/return the command set /p "userinputpath=Type input[Drive][Path]: " will not be executed, instead, the command after operator || will take action in this case because set /p return non 0.

And, if the user enters any variable for the path/folder input, such as % cd%\Folder_X, %temp%\SubFolder_Y, etc..., the variables will be set correctly and saved in %userinputpath% using the call command.

To save the variable in your file, appending (>>) or overwriting (>), leave it to be done after checking that the input is a folder, thus saving the variable correctly provided in the input:

First check if path/input is a folder:

<con: call dir/b/a-a "%userinputpath%\*" 2>nul >nul && (

After that, save this folder to your file:

      call echo;"%userinputpath%">"%temp%\folder.txt" & goto :checkexe

| improve this answer | |
  • Awesome! Thank you sir. I dont know why did you get the down vote, but i'll give you an up :) – Georg May 21 at 7:35
  • @Georg Good to know that work for you! – It Wasn't Me May 21 at 7:40

Spaces mean something in some batch statements including set. Look:

C:\>set /p a = b?

C:\>echo %a%

C:\>echo %a %
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.