0

I can't seem to find how to do this simple task, and this is probably answered.

How does one add a string to a variable? such as set "FileNameAndPCName = Locker\ and add %COMPUTERNAME%" to the variable so it would become like, "Locker/Bill"?

Code:

@echo off
set FileNameAndPCName="Locker\" + "%COMPUTERNAME%"
echo %FileNameAndPCName%
pause
7
  • remove spaces around =. They're significant in the set command Commented May 19, 2019 at 4:21
  • 1
    Possible duplicate of Batch File Set Variable not working Commented May 19, 2019 at 4:22
  • Sadly its not (I think), if I share you the entire code are you able to see what the problem is? Commented May 19, 2019 at 4:53
  • of course no one can help you if you don't show a minimal reproducible example Commented May 19, 2019 at 5:00
  • no. questions should be self-contained. No one will click on an external link just to read the code Commented May 19, 2019 at 5:28

1 Answer 1

1

Operations in batch is already string manipulation, therefore you don't need and can't use + to concatenate strings. Just type the whole string like this

set "FileNameAndPCName=Locker\%COMPUTERNAME%"

set FileNameAndPCName="Locker\" + "%COMPUTERNAME%" will set the variable %FileNameAndPCName% to "Locker\" + "%COMPUTERNAME%" literally (after substituting %COMPUTERNAME%. Quotes are not special in the set command. And it's also recommended to use the set "var=value" variant instead of set var=value to avoid unexpected trailing spaces at the end

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.