1

So, I'm trying to write a batch file that will take my input, slot it into a template that I've created, and put that into a text file.

set Output="%USERPROFILE%\desktop"

set /p VarOne=Example

echo ** %VarOne% ** > %Output%\%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt

This works fine, but if I do it like this, I get an access denied error:

set Output="%USERPROFILE%\desktop"
set FNAME="%Output%\%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt"

set /p VarOne=Example

echo ** %VarOne% ** > %FNAME%

I'm pretty inexperienced so, forgive me if its obvious.

2
  • No error on my system - Windows 10 - what version are you running? Commented Apr 30, 2018 at 21:20
  • 1
    Don't assign double quotes to your variables. Only use the double quotes when you are using the variable. Remove the quotes from the output and FNAME variables. Then your last line should be echo ** %VarOne% ** > "%FNAME%" Commented Apr 30, 2018 at 21:25

1 Answer 1

3

Don't assign double quotes to your variables but do use them to quote the SET statement as a best practice to protect special characters.

set "Output=%USERPROFILE%\desktop"
set "FNAME=%Output%\%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt"

set /p VarOne=Example

echo ** %VarOne% ** >"%FNAME%"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.