.bat to copy files based on current date
Moderator: DosItHelp
-
- Posts: 27
- Joined: 31 Jan 2012 14:45
.bat to copy files based on current date
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)
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
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
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
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 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
Last edited by dbenham on 25 Jun 2012 10:06, edited 1 time in total.
Re: .bat to copy files based on current date
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.
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
I tried XCOPY with /D:6-25-2012, and it only copied if the file already existed.
Dave Benham
Dave Benham
Re: .bat to copy files based on current date
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
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
-
- Posts: 27
- Joined: 31 Jan 2012 14:45
Re: .bat to copy files based on current date
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
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
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.
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