copy files to a different folder by date stamp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

copy files to a different folder by date stamp

#1 Post by loc3loan » 16 Jan 2014 08:05

Hello all,

I need some help to write a batch file that would copy all the file with the same date stamp to a different folder. Also, the folder name is matched with the file date stamps. I am running a program that will generate 99 logs file, and it will keep over-write the old file once it reaches to log files #99. I would like to move those file with a same date stamp to a different folder before it gets over-write. Thanks.

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

Re: copy files to a different folder by date stamp

#2 Post by Squashman » 16 Jan 2014 08:58

Please provide examples of your file names and what your source and destination folder structure looks like.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#3 Post by loc3loan » 16 Jan 2014 10:15

For example:

All the logs would be in d:\logs folder, and the filenames start with mmaster.lg1, .lg2, .....lg99 with a different date stamp. I would like to move the files with previous date stamp to a different folder. For instance, all the files with date stamps as 1/15/2014 would move to the folder d:\logs\20140115 folder that the batch file will create the folder. Thanks.

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

Re: copy files to a different folder by date stamp

#4 Post by foxidrive » 16 Jan 2014 10:27

Is it fair to say that the latest log file is the only one that doesn't have to be moved?

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

Re: copy files to a different folder by date stamp

#5 Post by Squashman » 16 Jan 2014 10:37

When you say DATE STAMP you really mean the File Attributes Last Modified or Created Date. Usually when someone says date or time stamp that makes me think the File name has the date and time in it as well.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#6 Post by loc3loan » 16 Jan 2014 10:43

For your question foxidrive: Not just the lastest log file, but all the logs files that get create on the current date will not be move. All the files that got create from previous date will be move to a different folder.

For your confusion Squashman: it is the created date or modified date, not the file names with date and time in it.

Thanks.

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

Re: copy files to a different folder by date stamp

#7 Post by Squashman » 16 Jan 2014 11:00

You can use the Forfiles command to move or copy all the files form the previous day.

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

Re: copy files to a different folder by date stamp

#8 Post by foxidrive » 16 Jan 2014 15:47

Squashman wrote:You can use the Forfiles command to move or copy all the files form the previous day.


Or indeed any that are 1 day or older.

This should work but the folder date stamp format depends on your regional settings. Here it creates YYYYMMDD but you may need "%c%%a%%b%" in your region, in the bottom two commands.


Code: Select all

@echo off
if "%~1"=="" (
for /f %%a in ('"FORFILES /P %homedrive%\ /M pagefile.sys /c "cmd /c echo @fdate" "') do set "d=%%a"
FORFILES /M *.lg* /d -1 /c "cmd /c call 0x22%~f00x22 @fdate @file" 2>nul
goto :EOF
)

:: this is processed with the date and filename info from above.
:: it is skipped if the filedate is the same as today

if "%d%"=="%~1" goto :EOF
for /f "tokens=1-3 delims=/-." %%a in ("%~1") do set A=00%%a&set B=00%%b&set c=%%c
set a=%a:~-2%
set b=%b:~-2%
echo c=%c% b=%b% a=%a% - %1 %2
      md "%c%%b%%a%" 2>nul
      move "%~2" "%c%%b%%a%" >nul
   )
)

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#9 Post by loc3loan » 17 Jan 2014 08:10

Thank you so much Foxidrive. It works perfectly. I would like to learn about the batch scripting. Where do I start? Thanks.

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

Re: copy files to a different folder by date stamp

#10 Post by foxidrive » 17 Jan 2014 08:31

This particular script is convoluted and recursive - not a good script to learn from.

It uses forfiles to get todays date in the same format as the rest of the forfiles date variables,
and then uses forfiles a second time to call the same batch file, to process the date
and to create the folder and move each file.

It occurs to me now that the first forfiles command is not needed, along with the date compare,
because the second forfiles will never pass a file with todays date due to the /d -1 switch.

This should work the same.

Code: Select all

@echo off
if "%~1"=="" (
FORFILES /M *.lg* /d -1 /c "cmd /c call 0x22%~f00x22 @fdate @file" 2>nul
goto :EOF
)

:: this processes each file older than 1 day with the date and filename info, from the forfiles command above

for /f "tokens=1-3 delims=/-." %%a in ("%~1") do set A=00%%a&set B=00%%b&set c=%%c
set a=%a:~-2%
set b=%b:~-2%
echo c=%c% b=%b% a=%a% - %1 %2
      md "%c%%b%%a%" 2>nul
      move "%~2" "%c%%b%%a%" >nul
   )
)

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#11 Post by loc3loan » 17 Jan 2014 08:55

Hi Foxidrive,

If I want to specify that path for those file, what do I need to change? I don't want to put the batch file in the same directory for those logs file. Thanks.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#12 Post by loc3loan » 17 Jan 2014 09:35

I got it already. Thanks, Foxidrive.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#13 Post by loc3loan » 07 Nov 2014 08:15

Hello all,

I got the above script to run. However, I never delete the folder, therefore, it filled up my hard drive quick. I would like to use the same script above, but delete the folder that older than 30 days. Therefore, everytime the script runs, it will create a new folder and delete the folder that was created 31 days ago. Thanks for your help.

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

Re: copy files to a different folder by date stamp

#14 Post by Squashman » 07 Nov 2014 08:26

Open up a CMD prompt and read the HELP for the FORFILES command.

Code: Select all

C:\>forfiles /?

The help has lots of good examples that you should be able to piece together to accomplish this task.
Look at @isdir and the /D options.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: copy files to a different folder by date stamp

#15 Post by loc3loan » 07 Nov 2014 08:47

This is what I have, but it still does not work. Thanks.

Code: Select all

@echo off
if "%~1"=="" (
FORFILES /p f:\logs /M *.lg* /d -1 /c "cmd /c call 0x22%~f00x22 @fdate @file" 2>nul
goto :EOF
)

:: this processes each file older than 1 day with the date and filename info, from the forfiles command above

for /f "tokens=1-3 delims=/-." %%a in ("%~1") do set A=00%%a&set B=00%%b&set c=%%c
set a=%a:~-2%
set b=%b:~-2%
echo c=%c% b=%b% a=%a% - %1 %2
      md f:\"%c%%b%%a%" 2>nul
      move "%~2" f:\"%c%%b%%a%" >nul
     FORFILES /P "f:\logs"  /D -30 /c “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @PATH” >nul
  )
)


MOD EDIT: PLEASE USE CODE TAGS!!!!

Post Reply