Robocopy in Batch File

jstevens
GoldLounger
Posts: 2640
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: 5534
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

A car crashed into a barrier at speed; nobody was injured, but a front wheel became detached, and slowly rolled down the road.
Driver [sings]: "You picked a fine time to leave me, Loose Wheel"​​

jstevens
GoldLounger
Posts: 2640
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: 2640
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: 5534
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

A car crashed into a barrier at speed; nobody was injured, but a front wheel became detached, and slowly rolled down the road.
Driver [sings]: "You picked a fine time to leave me, Loose Wheel"​​

jstevens
GoldLounger
Posts: 2640
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: 5534
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Robocopy in Batch File

Post by John Gray »

Excellent!
John Gray

A car crashed into a barrier at speed; nobody was injured, but a front wheel became detached, and slowly rolled down the road.
Driver [sings]: "You picked a fine time to leave me, Loose Wheel"​​

jstevens
GoldLounger
Posts: 2640
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: 5534
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

A car crashed into a barrier at speed; nobody was injured, but a front wheel became detached, and slowly rolled down the road.
Driver [sings]: "You picked a fine time to leave me, Loose Wheel"​​

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16415
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Robocopy in Batch File

Post by ChrisGreaves »

jstevens wrote:
04 Dec 2010, 18:07
And no I did not get five (just in case Chris G reads this-he may be busy shoveling snow) :hello:
I saw this! (just now).
Hi John! :hello: :stupidme:
Cheers, Chris
Most of my hair had already fallen out by the time I learned that mousse is spelled with two esses

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

Re: Robocopy in Batch File

Post by jstevens »

Chris,

You must have had a blizzard or two or the kid down the block is really slow shoveling. 14 years seems excessive.

:grin:
Regards,
John

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16415
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Robocopy in Batch File

Post by ChrisGreaves »

jstevens wrote:
21 Jun 2024, 01:55
You must have had a blizzard or two or the kid down the block is really slow shoveling. 14 years seems excessive.
Yabbut: that's in metric. In imperial, depending on the scale you choose, it's only 8.75 or -3.77. Now you may be wondering what a negative historic date is. Well it must mean a date in the future. David-across-the-street tells me that he can remember snow falling one July, here in Bonavista, so there's hope yet.
Cheers, Chris
Most of my hair had already fallen out by the time I learned that mousse is spelled with two esses

OneSecondStreet
Lounger
Posts: 47
Joined: 08 Jun 2024, 06:10

Re: Robocopy in Batch File

Post by OneSecondStreet »

Well, while we're at it...
jstevens wrote:
04 Dec 2010, 18:07
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)
2+2 can actually equal 5 for very large values of 2...