Go Back   Tech Support Forum > Microsoft Support > Windows 7 , Windows Vista Support

User Tag List

[SOLVED] Xcopy problem in batch file

This is a discussion on [SOLVED] Xcopy problem in batch file within the Windows 7 , Windows Vista Support forums, part of the Tech Support Forum category. I am stumped with a batch file problem. I can accomplish what I want with COPY command, but it won't


Closed Thread
 
Thread Tools Search this Thread
Old 05-03-2010, 09:50 PM   #1
Registered Member
 
Join Date: May 2010
Posts: 2
OS: Windows 7 ultimate



I am stumped with a batch file problem. I can accomplish what I want with COPY command, but it won't work when I replace it with XCOPY (which is what I really need).

I am trying to back up files AND insert the DATE into the filename.
The following line works perfectly:
copy c:\atemp\*.* c:\atemp\back*-%date:~4,2%-%date:~7,2%-%date:~10,4%.*

However, when I try to run the exact same line using XCOPY, it doesn't insert the date.

My question is, whats the difference between COPY and XCOPY regarding how it interprets the %DATE% variable?
FFAA is offline  
Sponsored Links
Advertisement
 
Old 05-04-2010, 12:56 AM   #2
Administrator
Manager, Microsoft Support
Acting Manager, Security
BSOD Kernel Dump Expert
Microsoft Windows Insider MVP
 
jcgriff2's Avatar

Microsoft Most Valuable Professional
 
Join Date: Sep 2007
Location: New Jersey Shore
Posts: 34,338
OS: Windows 10, 8.1 + Windbg :)



Hi -

Since the backup directory is within the directory itself, XCOPY would give off an error = "Cannot perform a cyclic copy". Let XCOPY create a new directory in c:\ for the backup files.

Try this code for XCOPY - copy into *.bat file -
Code:

set x=%date:~4,2%-%date:~7,2%-%date:~10,4%
if not exist c:\atemp_backup\%x% then do (
	xcopy c:\atemp c:\atemp_backup\%x% /i /s /y
)  
Then look in c:\atemp_backup. You'll find a folder with today's date on it and complete backup of c:\atemp.

You must run the batch script at an elevated admin level - RIGHT-click on the batch file, select "Run as Administrator".

Regards. . .

jcgriff2

.
jcgriff2 is offline  
Old 05-04-2010, 08:52 AM   #3
Registered Member
 
Join Date: May 2010
Posts: 2
OS: Windows 7 ultimate



Thanks. This works. It is different from the approach I was attempting, but it will work.

Can you explain why the code works for COPY but not for XCOPY?
FFAA is offline  
Sponsored Links
Advertisement
 
Old 05-05-2010, 05:44 AM   #4
Administrator
Manager, Microsoft Support
Acting Manager, Security
BSOD Kernel Dump Expert
Microsoft Windows Insider MVP
 
jcgriff2's Avatar

Microsoft Most Valuable Professional
 
Join Date: Sep 2007
Location: New Jersey Shore
Posts: 34,338
OS: Windows 10, 8.1 + Windbg :)



Hi -

Glad that code worked out for you.

"COPY" copies files only; XCOPY copies directories (folders) and files.

The code you provided for the COPY command simply copies only files (not sub-directories and files found in those sub-directories) found in c:\atemp, renames them with the date and copies them to c:\atemp\backup. You would end up with a ton of files in c:\atemp\backup

With XCOPY, if the destination back-up directory (folder) is a sub-directory of the source directory, it gives off the "Cannot perform a cyclic copy" error. If it ran, it would cause back-up sub-directories to be created within the back-up sub-directories and would not end until all hard drive space used up.

I hope that gives you a better understanding.

Regards. . .

jcgriff2

.
jcgriff2 is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2
Powered by vBadvanced CMPS v3.2.3


All times are GMT -7. The time now is 10:16 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.1.0 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
Copyright 2001 - 2018, Tech Support Forum

Windows 10 - Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts