Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

need some assitance with xcopy command

Posted on 2013-12-18
Last Modified: 2013-12-19
I want to create a  bat file that will copy files from one directory to another but I ran into a question I dont know the answer to. I need to move a single file from directory A to directory B everyday. A new file is created in directory A every day (the directory will continually build 1 file a day) but when the file is moved to directory B it is moved by an automated process to another directory. So on day two we now have 2 files in directory A and zero files in directory B but I only want to copy the new file from directory A not both files because the first file came across on day one and was processed. How can I set it up so it will not copy the same file twice even though the file doesnt exist in directory B any longer?
I would like to use a simple xcopy bat file for this if possible and set it up as a scheduled task..
Question by:bankadmin
  • 3
  • 2
  • 2
LVL 29

Accepted Solution

serialband earned 250 total points
ID: 39728108
Assuming you have no other files created in the same day in folder A, you can use robocopy.exe to do that with the /maxage option set to 1 day

robocopy "c:\dir\dir A" "c:\dir\dir B" /maxage:1

robocopy.exe comes included in Windows 7 and newer.  Prior to Win 2008, you had to download and install, or extract, the Windows Resource Kit to get it.
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 39728224
Otherwise you can get the newest file to copy using a for loop against dir listing of the files in date order, the first file is then the newest, that is copied and the process stopped.

As it stands that blindly copies the latest file though, unlike robocopy which will specify the age.

You can also just use xcopy /d with todays date

xcopy /d:mm-dd-yy  will copy the files after today.  If your date format is mm-dd-yyyy already you can just use xcopy /d:%date% but otherwise best to get the date in a reliable way, e.g. my article here--> http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

@echo off
Set Dir=c:\somedir
Set FType=*.xxx
Set Dest=D:\somedir
for /f "tokens=*" %%a in ('dir /b /o-d /a-d %Dir%\%FType%') do (
  echo copying %%~a which is from %%~ta
  copy /y "%%~a" "%Dest%"
  goto next
echo Completed


Author Comment

ID: 39729385
serial: It is a 2008 server box that I want to run it from. When I switch the .bat from xcopy to robocopy.exe I get a error on the first param. this is an example of it.
robocopy.exe "F:\dir\dir 1\dir 2\*.*"
Do you know why this is getting errors?
There are spaces in the directory names so I quoted the path
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.


Author Comment

ID: 39729503
THanks for the help I will split the points. We had some confusion with what was happening and as it turns out the files can be removed from from director A so a simple move command will work for us.. Again thanks for the help
LVL 43

Expert Comment

by:Steve Knight
ID: 39729505
It sounds like you are including the files too, with robocopy you specify directories, not file specs, and then the file spec afterwards:

robocopy "C:\dir1 \sub dir" "c:\dest dir\sub dir" *.*

If you check the error level when copying note that Upto 7 is "good", 8 onwards generally not.  There is list of the error codes around if wanted - the codes below 8 mean such things as "file copied", "extra file in destination" etc.


Author Comment

ID: 39729508
I've requested that this question be closed as follows:

Accepted answer: 250 points for serialband's comment #a39728108
Assisted answer: 250 points for dragon-it's comment #a39728224
Assisted answer: 0 points for bankadmin's comment #a39729503

for the following reason:

THe original question became a none issue
LVL 29

Expert Comment

ID: 39729677
dragon-it beat me to it.  Glad you solved your problem.

As dragon-it mentioned, robocopy.exe only takes directories as it parameters.  It's mainly used as a "poor man's" backup tool, since it can mirror entire directories and keep time stamps, ownership and permissions settings.  xcopy doesn't.  I very rarely use xcopy anymore.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question