# Batch - Copy files with a same filename within a folder structure



## hannes2424 (May 6, 2010)

Hey folks!

I searched through the web but I could not find anything helping me.
I'm trying to write a batch file in order to copy JUST the files within a folder structure to another folder.
All the files within the given folder structure do have the same name, but I don't want the batch script to overwrite the file that has been copied into the destination directory so far, instead it should in a way rename the files, so that in the end I do not have only one file called "filename.dat" but all of the files out of the source directory called "filename01.dat", "filename02.dat"... or something similar...

This is what I have got so far:


```
@echo off

Set "sourceDir=%CD%

:: copy files
For /F "Delims=" %%! in ('Dir "%sourceDir%\" /b /s /a-d 2^>nul') do (
   @xcopy "%%!" "%sourceDir%\%z%" /i /y /h /f /c
   
)
pause
```
I am able to get the single files within the folder structure, but I don't really know how to manage the overwriting problem. (The first file is copied and is then overwritten because of the next file which has the exact same filename)

Hope you can help me!

Greetings, hannes2424


----------



## TheOutcaste (Mar 19, 2009)

Welcome to TSF!

Never use an exclamation point as a variable, it's a special symbol like *%*, used with Delayed Expansion.

Are you wanting to copy all the files from the Source tree and have them end up in the same folder, or do you need to replicate the folder tree as well? 
Do you want the resulting names to sort in numeric order? If you don't add leading zeros to the number you add, they won't sort in order in a COmmand Prompt, and in some versions of Windows, depending on settings
For example, without leading zeros, a DIR will show this:
File1
File10
File19
File2
File25
File3

Copying the files to a subfolder of the source folder will only work if you only look at the top folder. you can't copy the tree that way.
Assuming there is only one file in the source, and you add a number to rename the file, this is what will happen:
First run:
Copy %sourceDir%\File1.txt to %sourceDir%\z\File1.txt
2nd run
Copy %sourceDir%\File1.txt to %sourceDir%\z\File1-1.txt
Copy %sourceDir%\z\File1.txt to %sourceDir%\z\File1-2.txt
Third run
Copy %sourceDir%\File1.txt to %sourceDir%\z\File1-3.txt
Copy %sourceDir%\z\File1.txt to %sourceDir%\z\File1-4.txt
Copy %sourceDir%\z\File1-1.txt to %sourceDir%\z\File1-1-1.txt
Copy %sourceDir%\z\File1-2.txt to %sourceDir%\z\File1-2-1.txt

Your destination can not be a subfolder of the source if you use the */S* switch.

So the main question is:
Do you want the files to all end up in the same folder, or do you need to replicate the folder tree as well?


----------



## Squashman (Apr 14, 2005)

You never define the %z% variable.

You are using the /H switch with Xcopy but it technically will never copy a hidden file because your DIR command in the for loop never lists any hidden files.


----------



## Squashman (Apr 14, 2005)

Squashman said:


> You are using the /H switch with Xcopy but it technically will never copy a hidden file because your DIR command in the for loop never lists any hidden files.


I was wrong about this. I always assumed dir /a-d wouldn't show hidden files but it does.
Glad I know that now.


----------

