copy files to a different folder by date stamp
Moderator: DosItHelp
copy files to a different folder by date stamp
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.
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.
Re: copy files to a different folder by date stamp
Please provide examples of your file names and what your source and destination folder structure looks like.
Re: copy files to a different folder by date stamp
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.
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.
Re: copy files to a different folder by date stamp
Is it fair to say that the latest log file is the only one that doesn't have to be moved?
Re: copy files to a different folder by date stamp
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.
Re: copy files to a different folder by date stamp
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.
For your confusion Squashman: it is the created date or modified date, not the file names with date and time in it.
Thanks.
Re: copy files to a different folder by date stamp
You can use the Forfiles command to move or copy all the files form the previous day.
Re: copy files to a different folder by date stamp
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
)
)
Re: copy files to a different folder by date stamp
Thank you so much Foxidrive. It works perfectly. I would like to learn about the batch scripting. Where do I start? Thanks.
Re: copy files to a different folder by date stamp
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.
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
)
)
Re: copy files to a different folder by date stamp
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.
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.
Re: copy files to a different folder by date stamp
I got it already. Thanks, Foxidrive.
Re: copy files to a different folder by date stamp
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.
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.
Re: copy files to a different folder by date stamp
Open up a CMD prompt and read the HELP for the FORFILES command.
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.
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.
Re: copy files to a different folder by date stamp
This is what I have, but it still does not work. Thanks.
MOD EDIT: PLEASE USE CODE TAGS!!!!
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!!!!