Copy file based on Date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
melidrie
Posts: 2
Joined: 07 Jun 2017 03:50

Copy file based on Date

#1 Post by melidrie » 07 Jun 2017 04:09

Hello,

I am new to this forum and i need help. :oops:
What i want is to copy a file(log.txt) in folder A to Folder B based on the date that is given in the command.

Eg. If i want a file from 2 january 2017 i typ in the command: 02-01-2017. Then de bat Will look in the folder on that date with the name and copy it to the other folder.

i have something like this but its not working OK (see code below)

Thank you so much :D

@echo off
:start
SET /P Date=insert:


SET _CmdResult=NONE
SET sourceDir=C:\mogri\1
SET targetDir=C:\mogri\2



forfiles /p %sourceDir% /D %Date% -m test*.txt /c "cmd /c copy %sourceDir%\test*.txt %targetDir%\testpaste.txt"

Echo.
Echo.
Echo -- File moved! --
Echo.
Echo.
pause

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy file based on Date

#2 Post by aGerman » 07 Jun 2017 13:27

I think robocopy should work here.
untested:

Code: Select all

@echo off &setlocal
set "sourceDir=C:\mogri\1"
set "targetDir=C:\mogri\2"

set /p "maxage=Enter the date [format YYYYMMDD]: "

set /a "minage=maxage + 1"
robocopy "%sourceDir%" "%targetDir%" "test*.txt" /minage:%minage% /maxage:%maxage%

Enter the date as shown in the prompt. E.g. 20170102

Steffen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Copy file based on Date

#3 Post by Compo » 08 Jun 2017 12:54

Your commands would look like this, note I changed the variable name because %DATE% is already a built-in variable name.

Code: Select all

SET/P "askDate=insert: "
FORFILES /P "%sourceDir%" /D "%askDate%" /M "test*.txt" /C "Cmd /C Copy @file 0x22%targetDir%\testpaste.txt0x22"
Just be aware that the command will work on every test*.txt file modified on or since the insert date; more than one matching file could cause you overwrite issues. 0x22 is hexadecimal code for a double quote character, although not needed with the names you've used above, I've left them there as good practice.

melidrie
Posts: 2
Joined: 07 Jun 2017 03:50

Re: Copy file based on Date

#4 Post by melidrie » 13 Jun 2017 07:05

It's almost ok.

What i want to do is, copy the file "test*.txt" if the Date is 02/06/2017. thats wat i insert into askDate.

I dont newer then or older then.

With a VBS code i got it working but i need to use it with bat


SET/P "askDate=insert: "

SET sourceDir=C:\mogri\1
SET targetDir=C:\mogri\2

Robocopy %sourceDir% %targetDir% test*.txt /MAXAGE:%askDate%




Echo.
Echo.
Echo -- File moved! --
Echo.
Echo.
pause

Post Reply