Robocopy in Batch File

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Robocopy in Batch File

Post by jstevens »

I'm having a challenge with Robocopy and passing the code to a new line. Not all file extensons are being copied.

Code: Select all

:This does not work
robocopy C:\ D:\CopiedFiles\C.Drive^
	     /XO /S *.bat^
	            "*.csc"^
                               "*.csv"^  
                               "*.doc"^
                              "*.xls"

:This works
robocopy C:\ D:\CopiedFiles\C.Drive^
	     /XO /S *.bat *.csc *.csv *.doc *.xls

 
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Robocopy in Batch File

Post by John Gray »

Try sticking echo in front of each Robocopy, and see what each the resulting line is parsed to...
Then you will see that it objects to "*.doc". I think you have a line length problem because of all the preceding spaces.

Code: Select all

:: This works when the preceding blank spaces are reduced...
ECHO robocopy C:\ D:\CopiedFiles\C.Drive^
        /XO /S *.bat^
        "*.csc"^
        "*.csv"^
        "*.doc"^
        "*.xls"
Why not do something like:
set parms=/XO /S *.bat "*.csc" "*.csv" "*.doc"^ "*.xls"
and thence
robocopy C:\ D:\CopiedFiles\C.Drive %parms%
I've no idea why you want each individual parameter on a new line
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Robocopy in Batch File

Post by jstevens »

John Gray wrote:Try sticking echo in front of each Robocopy, and see what each the resulting line is parsed to...
set parms=/XO /S *.bat "*.csc" "*.csv" "*.doc"^ "*.xls"
and thence
robocopy C:\ D:\CopiedFiles\C.Drive %parms%
I've no idea why you want each individual parameter on a new line



I have a long list of file types to include and I thought it would be easier to list them down then across.

Regards,
John
Regards,
John

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Robocopy in Batch File

Post by jstevens »

John Gray,

On a similar note, is it possible to exclude certain folders and subfolders such as "C:Windows"?

Example: Excluded C:\Windoes but includes subfolders
/XD C:\Windows

My intent is to exclude subfolders as well.

Regards,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Robocopy in Batch File

Post by John Gray »

jstevens wrote:
John Gray wrote:I've no idea why you want each individual parameter on a new line
I have a long list of file types to include and I thought it would be easier to list them down then across.
If so, the best place to put them is at the left-hand-side of each line!
jstevens wrote:On a similar note, is it possible to exclude certain folders and subfolders such as "C:Windows"?
Example: Excluded C:\Windows but includes subfolders
/XD C:\Windows
Unless you can prove otherwise, /XD c:\something includes all the subdirectories of c:\something, as well as itself.

Here's part of a BATch file I use to copy from one external hard drive (containing backups of a data drive, not C:\) to another:

Code: Select all

:: create the name of the log file from today's date (whose format is dd/mm/yyyy, UK only)
set log=%~dpn0_%date:~6,4%%date:~3,2%%date:~0,2%.txt
set source=E:\
set target=F:\
set parms=/copyall /s /np /r:0 /w:0 /fft /nfl /ndl
set parms=%parms% /xd Recycler "System Volume Information"
set parms=%parms% /xd "Local Data" "Kaspersky Lab"
set parms=%parms% /xd cache bases recent prefetch datacoll
set parms=%parms% /xf hiberfil.sys pagefile.sys
robocopy %source% %target% %parms% >> %log%
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Robocopy in Batch File

Post by jstevens »

John Gray wrote: Unless you can prove otherwise, /XD c:\something includes all the subdirectories of c:\something, as well as itself.
I was able to use your suggestion and sucessfully copy the files/folders and etc.. My challenge with excluding the subfolders relates to the blank spaces. After reviewing your code I was able to put 2+2 together.

And no I did not get five (just in case Chris G reads this-he may be busy shoveling snow) :hello:

Regards,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Robocopy in Batch File

Post by John Gray »

Excellent!
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Robocopy in Batch File

Post by jstevens »

John Gray,

Is it possible to copy a file that is in use?

I am receiving the following message relative to the "personal.xls" file: "The process cannot access the file because it is being used by another process?

Other than closing Excel is there a workaround?

Thanks,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Robocopy in Batch File

Post by John Gray »

The only way (that I'm aware of) that Windows (and thus RoboCopy) can reliably copy an open file is by means of Volume Shadow Copy (VSS). Otherwise you have to close the file, I'm afraid.
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...