Page 1 of 1
.bat to copy files based on current date
Posted: 25 Jun 2012 08:40
by daillest319
Im trying to have my batch script to copy a .txt file from one folder and copy it to another folder only if the test.txt file date modified is the same as the system date...can this be done in batch?
i dont have xcopy so it would be best for me to use it. How would i go about using xcopy to do the following.
so copy test.txt from C:\Practice to C:\Test only if it has today date (modified date)
Re: .bat to copy files based on current date
Posted: 25 Jun 2012 09:08
by dbenham
Code: Select all
forfiles /p "sourceFolder" /m "a.txt" /d 0 /c "cmd /c copy @path ""destinationFlder"""
I put quotes around the destination in case it contains spaces or special characters. Quote literals within the command must be doubled.
Dave Benham
Re: .bat to copy files based on current date
Posted: 25 Jun 2012 09:10
by Squashman
daillest319 wrote:i dont have xcopy so it would be best for me to use it. How would i go about using xcopy to do the following
Kind of contradicts itself.
Re: .bat to copy files based on current date
Posted: 25 Jun 2012 09:59
by dbenham
Squashman wrote:daillest319 wrote:i dont have xcopy so it would be best for me to use it. How would i go about using xcopy to do the following
Kind of contradicts itself.

I missed that bit of nonsense in the original question.
I don't think XCOPY has an option that will work. The /D option is close, but it will only copy if the file already exists in the destination.
Edit: I thought ROBOCOPY with /MAXAGE:0 would work, but apparently it doesn't.
Dave Benham
Re: .bat to copy files based on current date
Posted: 25 Jun 2012 10:05
by Squashman
I agree with you on Robocopy.
Not sure about xcopy. The help says if you don't specify a date then it only copies if the file is newer han the destination. So I am wondering if you do specify a date will it still copy regardless of what is in the destination.
Can't verify this. On vacation this week and only have my phone.
Re: .bat to copy files based on current date
Posted: 25 Jun 2012 10:08
by dbenham
I tried XCOPY with /D:6-25-2012, and it only copied if the file already existed.
Dave Benham
Re: .bat to copy files based on current date
Posted: 25 Jun 2012 13:39
by Fawers
But what if a flag .txt file was created to be used with xcopy? I mean, the file is going to be replaced anyway.
Code: Select all
@echo off
if not exist C:\Test\test.txt (type nul>C:\Test\test.txt)
::remaining code
Re: .bat to copy files based on current date
Posted: 26 Jun 2012 09:34
by foxidrive
daillest319 wrote:Im trying to have my batch script to copy a .txt file from one folder and copy it to another folder only if the test.txt file date modified is the same as the system date...can this be done in batch?
so copy test.txt from C:\Practice to C:\Test only if it has today date (modified date)
I think this should work. if your %date% variable is like this "Wed 26/06/2012" then change the tokens=2 in both lines. The date format should not matter.
Code: Select all
@echo off
pushd "c:\practice"
for %%a in (test.txt) do (
for /f "tokens=1" %%b in ("%%~ta") do (
for /f "tokens=1" %%c in ("%date%") do (
if "%%b" EQU "%%c" copy /b /y "%%a" "c:\test"
)
)
)
popd
Re: .bat to copy files based on current date
Posted: 28 Jun 2012 12:36
by daillest319
foxidrive
i tried your code but its not copying the file. the date for the date modified column look like the following....12/12/2012 1:53:pm
Re: .bat to copy files based on current date
Posted: 29 Jun 2012 01:41
by foxidrive
I tested it, it is working when the date format is right.
Try this version with some echo statements and copy/paste what you see on the screen please.
Code: Select all
@echo off
pushd "c:\practice"
for %%a in (test.txt) do (
for /f "tokens=1" %%b in ("%%~ta") do (
for /f "tokens=1" %%c in ("%date%") do (
if "%%b" EQU "%%c" copy /b /y "%%a" "c:\test"
echo if "%%b" EQU "%%c" copy /b /y "%%a" "c:\test"
)
)
)
popd
pause