.bat to copy files based on current date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

.bat to copy files based on current date

#1 Post by daillest319 » 25 Jun 2012 08:40

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)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: .bat to copy files based on current date

#2 Post by dbenham » 25 Jun 2012 09:08

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

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: .bat to copy files based on current date

#3 Post by Squashman » 25 Jun 2012 09:10

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: .bat to copy files based on current date

#4 Post by dbenham » 25 Jun 2012 09:59

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.

:lol: 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
Last edited by dbenham on 25 Jun 2012 10:06, edited 1 time in total.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: .bat to copy files based on current date

#5 Post by Squashman » 25 Jun 2012 10:05

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: .bat to copy files based on current date

#6 Post by dbenham » 25 Jun 2012 10:08

I tried XCOPY with /D:6-25-2012, and it only copied if the file already existed.


Dave Benham

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: .bat to copy files based on current date

#7 Post by Fawers » 25 Jun 2012 13:39

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat to copy files based on current date

#8 Post by foxidrive » 26 Jun 2012 09:34

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

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: .bat to copy files based on current date

#9 Post by daillest319 » 28 Jun 2012 12:36

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat to copy files based on current date

#10 Post by foxidrive » 29 Jun 2012 01:41

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

Post Reply