Question Locked

Copy specific folders and files

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

We have several hundred project folders each which the same subfolder structure. I am trying find a way to automate the task (with a script or whatever) of copying one of the subfolders (containing temperary data) to another drive and deleting the contents from the original.

Imagine the folder structure for each project is:

in
out
temp

so for example project1 folder looks like

project1
-- in
-- out
-- temp

and project2 is:

project2
-- in
-- out
-- temp

and so on...

I am trying to copy just the temp file to another drive where it would create the parent folder (project folder) and the temp folder, :-

project1
--temp

project2
--temp

then delete the contents of the original temp folder.

if that makes any sense

10 replies

by Member 25348569

The easiest way is with a couple of batch files:

I'm making the assumption that the only folders in the "project" drive are projects folders otherwise you'll have to be more selective with the filespec inside the braces for batch1.bat.

The first one-line batch1.bat

For /D %%i in (Path to source\*.*) Do call batch2.bat %%~ni

Batch2.bat to make the folder, copy and then delete. If you get hold of a copy of M$'s Robocopy (it's good!), you can do the copy and delete in one hit with its /MOVE option and log what's going on.

md "Path to target\%1\temp"
xcopy "path to source\%1\temp" "path to target\%1\temp" /E/C/Y
del "path to source\%1\temp\*.*" /Q

Neil :D

by wesley.chin

What OS is this for? I have tried creating batch files 1 and 2 and tested, but the result is not right. the source directory still has the contents and the contents do not appear in target directory.

by wesley.chin

I could not get XCOPY to work with the syntax suggested, though I did get it to work with other syntax. I wonder if it works if the source and target directories are external harddrives. Any ideas?

by Member 25348569

although I tested it on a very small scale - did exactly what you asked for. Running on XP Pro but it would work on anything from MSDOS 6 upwards. Possibly even CP/M if I used COPY and worked around the %%~ni.

The first batch file calls the second batch file passing the project folder name (without path) as the parameter. (%%~ni)

The second batch file makes the temp folder and the Project folder and then uses XCOPY to copy the files before deleting the source temp contents. The %1 variable will be expanded to the value of the folder name passed by Batch1.

XCOPY /E switch copies subfolders, /C carries on after errors and /Y suppresses prompting.

What do you get as an error message, what doesn't it do and what are YOU running it on?

Neil :)

by prcgroup

Thanks for your reply to my problem. It almost works ! it seems to copy only the files but does not copy or create the sub folders in the 'temp' folder even though the /e switch is there.

The source is our file server : Windows 200 Terminal.

The destination is XP Pro.

could you mind also a quick explanation of what the code is doing - thanks again , much appreciated.

by Member 25348569

Works OK for me as is. As you say, the /E switch is there for exactly that reason - to copy subdirectories - empty or otherwise. Try is with /S instead of /E (or try /S/E).

Also, add the /S (subdirectory switch) to the Del command to take out the files in subdirectories. It won't remove the directories, though and you'll have to use RD /S /Q for that.

Neil :)

Explanation in the post above.

Get a copy of Microsoft's ROBOCOPY and use that. It's MUCH better than XCOPY and you can use the /PURGE switch to delete as you copy.
http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

by prcgroup

Looks like the problem is that the variable does not pass folder name completely if it has spaces in it, example:

folder name: 00 a project

gets passed to batch2 as:

folder name: 00

I am going to have a look at robocopy as suggested but do you know how to get around this little problemo?

by Member 25348569

Spaces will be a problem. That now explains why it worked for me. I would guess that you've modified batch2 to display the parameter(s) to find this out.

One way is to add some quotes!

For /D %%i in (Path to source\*.*) Do call batch2.bat "%%~ni" to pass the complete folder name.

Unfortunately, you can then run into problems with needing to remove the quotes as the passed parameter represents only part of the final paths used in batch2. I can tell you how to do that but what is easier is not to add quotes to batch1 but to replace %1 in batch2 with %*. This will concatenate the variables back into the original string (up to nine!). If you enclose the complete string in batch2 in quotes - as I do in my original example - you should be OK. If you have more than nine spaces in your folder name, you are SERIOUSLY on your own... :)

Neil :)

by prcgroup

Thanks mate you have saved us a lot of work ! :-)

This discussion is closed to new replies.