Automate XCOPY help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
phpofficial
Posts: 3
Joined: 09 Apr 2012 04:31

Automate XCOPY help

#1 Post by phpofficial » 09 Apr 2012 04:41

Hello Everyone,

I have created this batch command. Here i am copying the whole files and folders from a particular drive to newly created folder '_OLD' in the same drive. I want xcopy to start copying the folder and should wait until the first process is completed i.e. when it starts copying the files and folders of drive D:\ then it should not start the copying process for next drive until the process of drive D:\ is completed. I have used the command line /W but I have to do it manually i need a way to automate this. I hope someone has solution for this. Thank you in advance.

@echo off

md D:\_OLD
md E:\_OLD
md F:\_OLD
md G:\_OLD
xcopy "D:\*.*" D:\_OLD /Y /S /E /R /H /W
xcopy "E:\*.*" E:\_OLD /Y /S /E /R /H /W
xcopy "F:\*.*" F:\_OLD /Y /S /E /R /H /W
xcopy "G:\*.*" G:\_OLD /Y /S /E /R /H /W

cls
exit

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Automate XCOPY help

#2 Post by abc0502 » 09 Apr 2012 05:15

Try This:

Code: Select all

@echo off
cls
color 0c
For /D %%a IN (.) DO (
::==============================
   Echo Copying Files In D:\_OLD
   md md D:\_OLD
   xcopy "D:\*.*" "D:\_OLD" /Y /E /R /H /Q

   Echo Copying Files In E:\_OLD
   md E:\_OLD
   xcopy "E:\*.*" "E:\_OLD" /Y /E /R /H /Q

   Echo Copying Files In F:\_OLD
   md F:\_OLD
   xcopy "F:\*.*" "F:\_OLD" /Y /E /R /H /Q

   Echo Copying Files In G:\_OLD
   md G:\_OLD
   xcopy "G:\*.*" "G:\_OLD" /Y /E /R /H /Q
::==============================
   color 0a
   Echo Copying Process Finished
   pause >nul
)

It will start one copy process at atime and when done start the next the copy process will not be shown to see the files while copying remove "/Q" from the end of every xommand line

you can add more tasks after the last xcopy command as you like

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Automate XCOPY help

#3 Post by foxidrive » 09 Apr 2012 05:51

This task is just made for a batch file and a loop! except you will get a recursive copy error I think. You can't copy a whole drive to a folder *on the same drive* using xcopy, without filtering out the target folder.

This is untested but it replicates the original batch file (with some extra switches):

Code: Select all

@echo off
for %%a in (d e f g) do (
md "%%a:\_OLD" 2>nul
xcopy "%%a:\*.*" "%%a:\_OLD\" /S /H /E /K /F /C /R /Y
)
cls
exit


If you really want to backup the whole drive to itself then reply and we can devise something. Why are you doing so anyway? It's a poor backup because if the drive fails then both copies are gone.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Automate XCOPY help

#4 Post by dbenham » 09 Apr 2012 06:02

The simple solution is to simply romove the /W option. You do not need the /W option to wait for the prior command to finish before beginning the next xcopy. The batch file will always wait for the prior command to finish.

The XCOPY /W option is intended to be used when the script is copying files to removable media and you want to give the user time to insert the media.


Dave Benham

phpofficial
Posts: 3
Joined: 09 Apr 2012 04:31

Re: Automate XCOPY help

#5 Post by phpofficial » 11 Apr 2012 02:50

Thank you all for your suggestions.

@foxidrive - Your code worked. This is not aimed to create a backup, it will be deleted eventually.


I am new to this so forgive me if i sound naive. Could it be possible instead of hard coding the drive names (d e f g), batch command automatically detects the number of drives and perform the copy process EXCEPT C:\?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Automate XCOPY help

#6 Post by Squashman » 11 Apr 2012 05:57

Well you could in theory put all possible letters into the FOR LOOP and then before the xcopy you could just do and IF EXIST.

Code: Select all

@echo off
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
     IF EXIST %%a:\ (
          md "%%a:\_OLD" 2>nul
          xcopy "%%a:\*.*" "%%a:\_OLD\" /S /H /E /K /F /C /R /Y
     )
)
cls
exit


But I thought there was a way to pull the mounted devices from the registry or from another command. Memory is failing me this morning.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Automate XCOPY help

#7 Post by Squashman » 11 Apr 2012 06:19

The registry does have a mounted devices key but it shows all mounted devices including any USB sticks that you previously mounted in the system. So in theory you will still have to check if they exist.

Another option is fsutil.
fsutil fsinfo drives
This shows all drive letters including any network drives. Which you probably don't want. I am assuming you just want local drives.

phpofficial
Posts: 3
Joined: 09 Apr 2012 04:31

Re: Automate XCOPY help

#8 Post by phpofficial » 11 Apr 2012 23:23

Thank you Squashman.

Could you also suggest how do I copy files or folders from a same location using Administrator privileges but the issue is user name between the path changes.

My PC is also being accessed by other users, I am the administrator and want to copy the logs from 'Document and Settings' of each users

Here are the Paths for example

C:\Documents and Settings\user1\Application Data\Pandion\Profiles
C:\Documents and Settings\user2\Application Data\Pandion\Profiles
C:\Documents and Settings\user3\Application Data\Pandion\Profiles

How do I write xcopy batch command so that it copy logs inside Profiles folder of each user and paste all into a master folder that I have specified?

Thank you in advance.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Automate XCOPY help

#9 Post by abc0502 » 12 Apr 2012 03:15

"C:\Documents and Settings\user1" u can replace this with %userprofile%
this will always take u to the user profile folder

in windows xp u can replace the "C:\Documents and Settings\user3\Application Data\"
with %appdata% this will always take u to the application data folderand then complete the address like that %appdata%\Pandion\Profiles

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Automate XCOPY help

#10 Post by foxidrive » 12 Apr 2012 08:24

This is untested: If the folder exists for each user it should create a user folder and copy those *.log files over

Code: Select all

@echo off
for /f "delims=" %%a in ('dir "C:\Documents and Settings" /ad /b') do (
if exist "C:\Documents and Settings\%%a\Application Data\Pandion\Profiles\" (
md "z:\target folder\%%a" 2>nul
copy "C:\Documents and Settings\%%a\Application Data\Pandion\Profiles\*.log" "z:\target folder\%%a"
)
)

Post Reply