# Solved: Batching looking for file on entire drive + xcopy



## TacticalSniper (Sep 21, 2009)

Hey all,

I am attempting to write a batch that would backup specific files on user's hard drive. Now, it is obviously easy to copy a specific folder and all data in it, but where I am having trouble is finding files with a specific extension and copying them into specific folders.

For example: the script should be using .mdb files on user's hard drive and then copy each file it discovers to N:\Bckp.

Any ideas? It would be greatly appreciated.


----------



## TheOutcaste (Aug 8, 2007)

Do you want to copy all *.mdb* files from anyplace on the drive into the _same_ folder, *N:\Bckp*?
You'd need to check for duplicate names, and would have no way to restore the file to the correct folder unless you also save the path info somehow.

If you want to copy all *.mdb* files to *N:\Bckp* preserving the folder structure, it's fairly easy.

*Xcopy C:\Users\*.mdb n:\Bckup /CDHIRS*
Add */O* or */X* if you need to copy NTFS info. /X will only work from and Elevated Prompt.

One problem with Xcopy is it first creates the Folder on the destination drive. If there are no files to copy in that folder, it then deletes the folder. Causes a lot of unnecessary writing, which should be avoided on flash drives. And takes a long time.
Robocopy doesn't create the folders, but will retry any folder it can't access. The default is 1 million retries 30 seconds apart. 347.22 days for each folder it doesn't have permission to access before moving on to the next What were they thinking?

Much faster to use a For loop and let Dir find the files, then use Copy, Robocopy or Xcopy to copy each file individually. I'd use Robocopy unless you have to run this on XP and can't install Robocopy.

Note that if N: is formatted FAT/FAT32, you can't copy file permissions and Ownership info. Robocopy will give a warning but will copy the file. Xcopy fails.

If you need to copy Audit info (Using /COPYALL for Robocopy, /X for Xcopy), this must be run from an Elevated Prompt on Vista/Win7
If you don't care about the NTFS info, you can also use Copy instead of Robocopy or Xcopy. Be sure to use the */B* switch though. Note that Copy won't copy zero byte files.

Dir will not search for just an extension, so that needs to be checked. For Example, *DIR *.mdb* will find this file:
*Donotcopythis.mdbfile.txt*
Change the *PushD* statement to the starting folder if you don't want to search the entire drive, say use *PushD C:\Users* to just search User and Public folders.

```
@Echo Off
SetLocal EnableDelayedExpansion
Set _Dest=N:\Bckp
Set _Ext=*.mdb
:: If the destination is FAT/FAT32 NTFS Permissions and ownership won't be copied
Set _Switches=/R:2 /W:5 /COPY:DATSO
PushD C:\
For /F "Tokens=* Delims=" %%I In ('Dir /B /S %_Ext%') Do (
:: Robocopy doesn't like trailing slashes on the paths, so these two lines remove them
Set _SrcPath=%%~pI
Set _SrcPath=!_SrcPath:~,-1!
If /I "*%%~xI"=="%_Ext%" Robocopy "%%~dI!_SrcPath!" "%_Dest%!_SrcPath!" %_Switches% "%%~nxI"
)
PopD
```
Wouldn't be to hard to modify the above to copy all files to the same folder, checking for duplicates and adding an incrementing number to the file name for duplicate files.


----------



## TacticalSniper (Sep 21, 2009)

Hey, mate, thanks a lot for the help.

I was toying around with it a bit, however, I am still wondering if it's possible to do this with _xcopy_? I have to run this script in a logon script on the domain and I wouldn't want users encountering compatibility issues.

In fact, I _have_ tried using xcopy, but it gives me _invalid number of parameters_ error.


----------



## TheOutcaste (Aug 8, 2007)

Xcopy needs the file name to be part of the source folder so I suspect that may be the cause of the error.
Give this a try, it should work. Change the _Root folder as needed, use C:\ to scan the entire drive.


```
@Echo Off
SetLocal
Set _Root=C:\Test Dir\Folder 1
Set _Dest=N:\Bckp
Set _Ext=*.mdb
:: If the destination is FAT/FAT32 Remove the O switch
:: To list the files being copied, remove the Q switch
Set _Switches=/CDHRQY
PushD "%_Root%"
For /F "Tokens=* Delims=" %%I In ('Dir /B /S %_Ext%') Do (
:: For no screen output add >Nul to the end of the next line
If /I "*%%~xI"=="%_Ext%" Xcopy "%%~dpI%%~nxI" "%_Dest%%%~pI" %_Switches%
)
PopD
```


----------



## TacticalSniper (Sep 21, 2009)

Hey, thanks a lot, mate! This works like a charm!


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------



## TacticalSniper (Sep 21, 2009)

^^ Thanks for the tip, mate


----------

