1

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"

zx485
  • 28,498
  • 28
  • 50
  • 59
  • 4
    An environmental variable name cannot begin with a number. – Squashman Jan 18 '17 at 02:26
  • 2
    ... and should not end with a space. `set var = value` will set the variable named `%var %` (with a space after) with a value `" value"` (with a space before) – phuclv Jan 18 '17 at 10:21

2 Answers2

4
  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%
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • ok i will have to do these all manually then, every folder name will always be a number set, steamcmd automatically creates that foldername based on which addon it downloads, no way to change that. So the only way I can see this working doing that would be to specify the first pathname as is instead of using the variable. 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 – Justin Thomas Jan 18 '17 at 02:46
  • 3
    The variable name can't start with a number. That doesn't mean the folder name can't start with a number. – Harry Johnston Jan 18 '17 at 02:56
  • ok it seems to be working now, how can I get rid of this prompt Copying @Taunus_A3 Does C:\TCAFiles\Games\arma3exile\TCA.Mods\@Taunus_A3 specify a file name or directory name on the target (F = file, D = directory)? – Justin Thomas Jan 18 '17 at 02:58
  • 1
    @JustinThomas read [xcopy file, rename, suppress “Does xxx specify a file name…” message](http://stackoverflow.com/q/3018289/3439404) thread. – JosefZ Jan 18 '17 at 03:05
  • 1
    `"C:\TCAFiles\Games\arma3exile\TCA.Mods\%_836147398%\"` adding a trailing \ backslash specifies **directory** as well as `/I` switch does. @JustinThomas – JosefZ Jan 18 '17 at 03:21
3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • originally it was without spaces, i tried with and without and with " " – Justin Thomas Jan 18 '17 at 01:55
  • Doing it with a space won't affect the variable without a space. It may well be you have an old variable lying around. In any case, as per the execution snippet above, doing it without spaces seems to work fine. – paxdiablo Jan 18 '17 at 02:05
  • This would making having the set variables redundant. Is it not possible to use a set variable within an xcopy command? or is something in my windows not allowing it? its on windows server 2008 r2. – Justin Thomas Jan 18 '17 at 02:37
  • As others already stated, variable names must cannot begin with numerals in [tag:batch-files], because `%4` in `%42%` is expanded to the fourth command line argument, so your answer solves only one of two problems... – aschipfl Jan 18 '17 at 09:57
  • @aschipfl, it solves the exact problem as stated in the question. If I could have written an answer that would solve *every* IT-related problem, believe me, I would have. But I think SO has size limits on answers :-) In any case, I've answered that aspect as well in that the variable name chosen is, shall we say, sub-optimal (only partially because of the parameter interference problem). – paxdiablo Jan 18 '17 at 10:12
  • The question is tagged [tag:batch-file] for which your answer does not really help much, that is what I was trying to say... JosefZ attends to both issues although his [answer](http://stackoverflow.com/a/41710435) is shorter than yours... ;-) – aschipfl Jan 18 '17 at 10:19
  • S'Ok, @aschipfl, if it's better, vote it up. This is a meritocracy after all and I have pretty thick skin :-) – paxdiablo Jan 18 '17 at 10:21