-1

I'm trying to create a .bat file that will copy all files that end with "XX" in three separate folders. I want it to put them all in one folder but add an incremental number to the end of the names (that way there aren't duplicate file names). After reading answers to my original question, I have written it again. But it still isn't copying everything because it gets duplicate filenames.

:: The setlocal line should put before the for loop:
setlocal EnableDelayedExpansion

set _a=0

for /D %%D in ("R:\SQL QUERIES\Mark\text append testing\*") do (

::Same as set /a _a=_a+1
SET /A _a+=1
echo !_a!

xcopy /Y /S "%%~D\*XX*" "R:\SQL QUERIES\Mark\conversion_scripts"

ren "R:\SQL QUERIES\Mark\conversion_scripts\*XX*" *!_a!
)
pause
Mark
  • 55
  • 1
  • 1
  • 9
  • The first line should read `Set "_a=0"`, the `SET` in your loop should read `Set /A _a +=1`, your `echo` and `ren` commands should be expanded upon running, using either delayed expansion, or a pseudo `Call` statement, e.g. `Call Echo(%%_a%%`, _although I'm sure this line isn't really required_. – Compo Feb 14 '19 at 17:37
  • Duplicates here: [Defining and using a variable in batch file](//stackoverflow.com/q/10552812), [Variables are not behaving as expected](//stackoverflow.com/q/30282784). – double-beep Feb 14 '19 at 18:17
  • Possible duplicate of [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – double-beep Feb 14 '19 at 18:18
  • What do you mean by »end with "XX"«? the base name (like `some_nameXX.txt`) or the extension (`some_name.XX`)? – aschipfl Feb 14 '19 at 19:19
  • The base name. The funny thing is they don't have an extension though. They are just type=file. I open them in Notebook but they don't have a pre-defined app to run in. – Mark Feb 15 '19 at 15:05
  • It's messing up on the rename part. The closest I can get it to working is to do: ren "R:\SQL QUERIES\Mark\conversion_scripts\*XX" *XX!_a! However, this adds an extra X to the end of the file. Which is not all that bad, but it is unnecessary. – Mark Feb 15 '19 at 17:16

1 Answers1

0

This works:

:: The setlocal line should put before the for loop:
setlocal EnableDelayedExpansion

set _a=0

for /D %%D in ("R:\SQL QUERIES\Mark\text append testing\*") do (

::Same as set /a _a=_a+1
SET /A _a+=1
echo !_a!

xcopy /Y /S "%%~D\*XX*" "R:\SQL QUERIES\Mark\conversion_scripts"

ren "R:\SQL QUERIES\Mark\conversion_scripts\*XX" *X!_a!
)
Mark
  • 55
  • 1
  • 1
  • 9
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Makyen Feb 15 '19 at 23:36