# Batch loop copying



## flyg (Feb 20, 2009)

Hi,

Trying to write a batch file to copy the my documents folder to another location.

One problem is this is on a domain enviroment so I wont know the names of the profile directories when running the batch file.

I came up with this :

@echo off
c:
cd\"documents and settings"
for /f %%D in ('dir/b/ad') do (
cd %%D
cd "my documents"
xcopy *.* /s /y /r /c c:\test
cd \"documents and settings"
)

It goes into each profiles\my documents directory and does an xcopy to c:\test (xcopy as some people may have created sub folders) but the problem is it seems to repeat forever.

I had to add the cd \"documents and settings" at the end as it didnt loop with that.

I know i've probably done it all wrong (not good with for on batch files). Could anyone help/point me in the right direction?

thanks


----------



## Squashman (Apr 14, 2005)

you may want to look at using 

FOR /R instead of FOR /F


----------



## Squashman (Apr 14, 2005)

From my Buddy TheOutcaste over on the TechGuy forums.


> On this one For /D would be better than /R, as /R will grab all the subfolders as well. Here he just needs the profile names.
> The shared documents folder in AllUsers is actually named just Documents, not My Documents, so we need to allow for that; either skip it to avoid a Not Found error, or copy it.
> The destination folder needs to be specified; dumping them all into C:\Test will lump every one's files together.
> 
> ...


----------



## flyg (Feb 20, 2009)

thanks a lot! Almost there - the batch file above with echo removed to carry out the copying coems up with

Does c:\test\user2\documents specify a file name or a directory name on the target?

Manually pressing D works fine and it goes through all the directories but it asks everytime - any idea how to supress this prompt?


----------



## Squashman (Apr 14, 2005)

http://www.csulb.edu/~murdock/xcopy.html
Read about half way down on that page.


----------



## TheOutcaste (Mar 19, 2009)

flyg said:


> Manually pressing D works fine and it goes through all the directories but it asks everytime - any idea how to supress this prompt?


If renaming to mcopy doesn't work, just add the /I switch. This forces it to assume it's a directory everytime.
*Xcopy /CIRSY*

Jerry


----------

