Justin Thomas
Justin Thomas

Reputation: 71

SET variable not being read in .BAT file

I have this Windows batch file and I cannot get to work it properly.
Basically I have a folder in one location and I need to copy it to another folder, but to also have the new folder name renamed.

@Echo off
::
set 836147398 = @Taunus_A3
:: 
Echo Copying %836147398%
xcopy "C:\Users\arrona\Desktop\Update Mods\steamapps\workshop\content\107410\836147398" "C:\TCAFiles\Games\arma3exile\TCA.Mods\%836147398%" /s/h/e/k/f/c

So what I need is that the folder contents of 333310405 are copied to C:\TCAFiles\Games\arma3exile\TCA.Mods\, but as @Taunus_A3 folder name.

It will copy the folder over and all the contents, but it is not reading the variable. It will echo "Copying 836147398" not echoing "Copying @Taunus_A3"

Upvotes: 1

Views: 1448

Answers (2)

paxdiablo
paxdiablo

Reputation: 881093

Get rid of the spaces on either side of the = sign. As you can see from the second echo, you're actually creating an environment variable with an embedded space in the name (and value, for that matter):

c:\pax> set 42=

c:\pax> set 42 = 57
c:\pax> echo .%42%.
.%42%.
c:\pax> echo .%42 %.
. 57.

c:\pax> set 42=57
c:\pax> echo .%42%.
.57.

Once that's done, the substitutions work as needed, at least on the command line(a) (although your source area is different in the code and text, you probably want to sort that out as well):

c:\pax> set 836147398=@Taunus_A3

c:\pax> echo xcopy "C:\Users\arrona\Desktop\Update Mods\steamapps\workshop\content\107410\836147398" "C:\TCAFiles\Games\arma3exile\TCA.Mods\%836147398%" /s/h/e/k/f/c

xcopy "C:\Users\arrona\Desktop\Update Mods\steamapps\workshop\content\107410\836147398" "C:\TCAFiles\Games\arma3exile\TCA.Mods\@Taunus_A3" /s/h/e/k/f/c

(a) However, one thing you should be made aware of is that 836147398 is actually a particularly bad variable name, partially because it will, in a script, actually use parameter names (like %8) in preference to %8-something-or-other%, but also because it's not very mnemonic.

You would be far better off using something like dest_dir, something that actually makes sense to the casual reader.

Upvotes: 3

JosefZ
JosefZ

Reputation: 30103

  1. Get rid of spaces on either side of the = sign in set "varname=varvalue" command.
  2. Avoid naming variables with a leading number. Read Command Line arguments (Parameters):

Although set "836147398=@Taunus_A3" is a valid command, echo %836147398% would display

  • @Taunus_A3 in command prompt, but
  • 36147398 in a batch script, because %8 is evaluated to the 8th supplied parameter (supposedly an empty string in most cases) and orphan trailing % percent sign is ignored by design.

Solution: use a non-cipher prefix, e.g. _ underscore as follows:

set "_836147398=@Taunus_A3"
echo %_836147398%

Upvotes: 4

Related Questions