Page 1 of 1
To pick text files from previous month
Posted: 23 Nov 2016 21:47
by Sanveg1988
I am having one requirement as below :
I want to create a batch job which will pickup only last months text file from folder. To pick all files from 1st Day of previous month to last day of previous month which are created in previous month.
Lets say my batch job is running on 8th of every month, so when it will run on 8th of month, it should pickup all text files from 1st Oct to 31st Oct.
Ex : If batch job runs on 8th Dec 2016, it should pick up all files if Nov month from folder based on the files created in Nov.
Any help would be greatly appreciated.
Re: To pick text files from previous month
Posted: 24 Nov 2016 08:34
by Squashman
You have not specified what date you are referring to.
1) Is there a date in the file name?
2) Do we use the file systems create date?
3) Do we use the file systems modified date?
Re: To pick text files from previous month
Posted: 24 Nov 2016 11:23
by Sanveg1988
We will refer only creation date of file. There is no Date mentioned in text files. We will pick up files based on creation date only i.e files created in last month.
Re: To pick text files from previous month
Posted: 24 Nov 2016 16:45
by Squashman
Then that becomes region dependent. Need to know how your computer displays the date in the DIR command.
Re: To pick text files from previous month
Posted: 25 Nov 2016 22:00
by Sanveg1988
It displays the date in MM/DD/YYYY format.
Re: To pick text files from previous month
Posted: 26 Nov 2016 13:42
by pieh-ejdsch
with this batch you can specify from which month a backup should occur.
at least one month must be given.
the annual backup is as possible as one of all three months.
or whatever they would like to set.
Code: Select all
@echo off
setlocal
:: rem Starttime
@robocopy /L |findstr /rc:"[0-9]:[0-9]"
:: Settings Month
rem - how many months ago should the backup be begun?
set /a AgoMonth=1
rem - how many months have to go in the backup
set /a MonthGoBackup=1
:Timestamp
rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L /njh "\|" .^|find "123" ') do (
set "TS=%%T%%U%%V-%%W%%X%%Y"
set "TSDATE=%%T%%U%%V"
set /a YY=%%T , MM=100%%U %%100 , TT=100%%V %%100
)
:: end Timestamp
:calculateMonthRC /maxage /minage
set /a M.all =YY *12 +MM -1 -AgoMonth
set /a BU.begin =M.all /12 *100 +M.all %%12 +1
set /a BU.end =(M.all +MonthGoBackup) /12 *100 +(M.all +MonthGoBackup) %%12 +1
set /a n.BU.end =(M.all -1 +MonthGoBackup) /12 *100 +(M.all -1 +MonthGoBackup) %%12 +1
if %n.BU.end% == %BU.begin% (set "s.BU.end=") else set "s.BU.end=- %n.BU.end%"
set /a RCdateMax =BU.begin *100 +1
set /a RCdateMin =BU.end *100 +1
echo you want to backup: %BU.begin% %s.BU.end%
:: Settings for Backup please
robocopy /L /S /maxage:%RCdateMax% /minage:%RCdateMin% . "D:\.. Backupfolder ..\\" *.txt /ndL /fp /Xj /nc /ns /np /TS /R:0
:: end
pause
Phil