To pick text files from previous month

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sanveg1988
Posts: 3
Joined: 23 Nov 2016 21:39

To pick text files from previous month

#1 Post by Sanveg1988 » 23 Nov 2016 21:47

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.

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

Re: To pick text files from previous month

#2 Post by Squashman » 24 Nov 2016 08:34

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?

Sanveg1988
Posts: 3
Joined: 23 Nov 2016 21:39

Re: To pick text files from previous month

#3 Post by Sanveg1988 » 24 Nov 2016 11:23

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.

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

Re: To pick text files from previous month

#4 Post by Squashman » 24 Nov 2016 16:45

Then that becomes region dependent. Need to know how your computer displays the date in the DIR command.

Sanveg1988
Posts: 3
Joined: 23 Nov 2016 21:39

Re: To pick text files from previous month

#5 Post by Sanveg1988 » 25 Nov 2016 22:00

It displays the date in MM/DD/YYYY format.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: To pick text files from previous month

#6 Post by pieh-ejdsch » 26 Nov 2016 13:42

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

Post Reply