# batch script for moving files on time basis



## vvijayagiri (May 15, 2010)

Hello,

I need write a batch script for finding files got stuck in temp folder,if user find 15min old files with the current system time, then those files should move into trash folder.Can anyone help on this, I'm new to coding and could use some help.


----------



## TheOutcaste (Mar 19, 2009)

If they have the current system time, then they can't be 15 minutes old. How will you determine what is a "15 minute old" file?
If the file is in use, it can't be moved or deleted. If the time stamp on the file is the current time then it's likely the file is in use.


----------



## vvijayagiri (May 15, 2010)

Hi,
Thanks for quick response.
My goal is to delete the .txt files which are in a specific folder with a specific name. Assuming that sas-mail.txt file is there in the temp folder and it was created 15 mins ago when compared with the current system date and time.
Now i want to delete those(sas-mail.txt only) text fiiles which should not have the current system time but with the same name.


----------



## TheOutcaste (Mar 19, 2009)

I'm still confused by this, you've used files, plural, meaning more than one file, in two places:


vvijayagiri said:


> My goal is to delete the .txt files which are in a specific folder with a specific name. Assuming that sas-mail.txt file is there in the temp folder and it was created 15 mins ago when compared with the current system date and time.
> Now i want to delete those(sas-mail.txt only) text fiiles which should not have the current system time but with the same name.


You can't have more than one file with the same name in the same folder.
So, is this one file in one folder?
multiple files with the same name in _different_ folders
Or multiple files _containing_ *sas-mail* in their name in one folder, or in multiple folders?

This file will delete the specified file from the specified folder when it is X minutes old. Win 7 will limit it to 1667 minutes, I don't know how long you can specify in XP or Vista.

```
@Echo Off
:: Enter Path to the folder, the file name, and the age in minutes
:: when the file should be deleted. 
Set _TFolder=C:\Test Dir
Set _FName=sas-mail.txt
Set _Delay=15
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
CD /D "%_TFolder%"
Call :_SetupVariables
Goto _CheckFN
:_Loop
Call :%_tWait% 60
:_CheckFN
If Not Exist "%_FName%" Goto _Loop
:: Get date and time for the file
Set _Tdt=
Set _Ttm=
Set _Tap=
For /F "Tokens=1-3 Skip=4 Delims= " %%I In ('Dir /A-D "%_FName%"') Do (
  Set _Tdt=%%I
  Set _Ttm=%%J
  Set _Tap=%%K
  Goto _CheckDate
)
:_CheckDate
Echo.%_Tap%|Findstr /I /R /C:"[AP]M">Nul&&Set _Ttm=%_Ttm% %_Tap%
Call :_Getdate %_Tdt%
Call :_GetTime %_Ttm%
Call :_JDate %_fdate:~0,4% %_fdate:~4,2% %_fdate:~6,2%
Set _FileDate=%_Jdate%
Set _FileTime=%_mmmm%
:_CheckNow
:: Get current date and time
Call :_Getdate
Call :_GetTime
Call :_JDate %_fdate:~0,4% %_fdate:~4,2% %_fdate:~6,2%
Set /A _TimeDif=(_Jdate-_FileDate)*1440+_mmmm-_FileTime
:: If file not old enough to delete, set wait time to the remaining time
If %_TimeDif% GEQ %_Delay% Del /A "%_FName%" & Goto _CheckFN
Set /A _WTime=(_Delay-_TimeDif)*60
Call :%_tWait% %_WTime%
Goto _CheckNow
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::: 
:_SetupVariables
:: Get Regional Settings to use in date routines
Set _iDate=
Set _sDate=
Set _iTime=
Set _sTime=
Set _tWait=_Time0
:: Check if sleep or timeout commands availible
For %%I In (sleep.exe) Do If Not "%%~$PATH:I"=="" Set _tWait=_Time1
For %%I In (timeout.exe) Do If Not "%%~$PATH:I"=="" Set _tWait=_Time2
:: Default Delimiter of TAB and Space are used
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v iTime') Do Set _iTime=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v sTime') Do Set _sTime=%%B
Goto :EOF
:_GetDate
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified by TheOutcaste http://forums.techguy.org from
:: SortDate Written byRob van der Woude http://www.robvanderwoude.com
:: to check for two digit years
:: If passed a parameter use that for the date
If "%~1"=="" (Set _Date=%Date%) Else Set _Date=%*
If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
Set _TDYM=
If %_iDate%==0 For /F "Tokens=%_NumTok% Delims=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
If %_iDate%==1 For /F "Tokens=%_NumTok% Delims=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
If %_iDate%==2 For /F "Tokens=%_NumTok% Delims=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
If "%_fdate:~7,1%"=="" For /F "Tokens=3 skip=3" %%I In ('Reg Query "HKCU\Control Panel\International\Calendars\TwoDigitYearMax" /V 1 2^>Nul') Do Set _TDYM=%%I
If Defined _TDYM (Set _MaxY=%_TDYM:~2%&Set _Cent=%_TDYM:~0,2%) Else (Set _MaxY=29&Set _Cent=20)
Set /A _Cm1=_Cent-1
If "%_fdate:~7,1%"=="" If %_fdate:~0,2% LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
Goto:EOF
:_GetTime
:: Modified by TheOutcaste http://forums.techguy.org from SortTime
:: Written by Rob van der Woude http://www.robvanderwoude.com
:: Time is returned in 24 hour format as hhmm in variable _hhmm
:: Time in minutes is returned in _mmmm
If "%~1"=="" (For /F "Tokens=*" %%A In ('TIME/T') Do Set _Time=%%A) Else Set _Time=%*
If %_iTime%==1 Goto _in24format
For /F "Tokens=1* Delims=%_sTime% " %%A In ('Echo %_Time%') Do (
	Set _Hour=%%A
	Set _Minutes=%%B
)
Set _AMPM=
Echo.%_Minutes%| Find /I "A" >Nul && Set _AMPM=A
Echo.%_Minutes%| Find /I "P" >Nul && Set _AMPM=P
If [%_AMPM%]==[] Set _AMPM=A
Set _Minutes=%_Minutes:~0,2%
If %_Hour:~0,1%==0 Set _Hour=%_Hour:~-1%
If %_Hour% LSS 12 If /I %_AMPM%==P Set /A _Hour=%_Hour%+12
If %_Hour% LSS 10 If /I %_AMPM%==A Set _Hour=0%_Hour%
If %_Hour% EQU 12 If /I %_AMPM%==A Set _Hour=00
Set _Time=%_Hour%%_sTime%%_Minutes%
:_in24format
For /F "Tokens=1,2* Delims=%_sTime% " %%A In ('Echo %_Time%') Do (
Set _hhmm=%%A%%B
If %_Hour:~0,1%==0 Set _Hour=%_Hour:~-1%
If %_Minutes:~0,1%==0 Set _Minutes=%_Minutes:~-1%
Set /A _mmmm=_Hour*60+_Minutes
)
Goto:EOF
:_JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date in _JDate
:: Usage
:: Call :_JDate %_GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
:: Code taken from datediff.bat written by Rob van der Woude
:: http://www.robvanderwoude.com
:: Modified to handle months and days without leading zeros
:: By TheOutcaste http://forums.techguy.org
Set _JMM=%2
Set _JDD=%3
If 1%_JMM% LSS 110 Set _JMM=%_JMM:~-1%
If 1%_JDD% LSS 110 Set _JDD=%_JDD:~-1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/Library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1  = %1 + 4800
Set /A _JDate  = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) / 100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF 
:_Time0
>Nul Ping 1.0.0.0 -n 1 -w %~1000
Goto :EOF
:_Time1
>Nul Sleep %~1
Goto :EOF
:_Time2
>Nul Timeout /NOBREAK /T %~1
Goto :EOF
```


----------



## vvijayagiri (May 15, 2010)

I want to delete those files only which have the sas-mail.txt and sas-fax.txt names. In those text files,the text file should be deleted which has 15mins delay with the current system time.


----------



## TheOutcaste (Mar 19, 2009)

OK, this will delete those two files. This checks the Modified time, so if the files are being modified after they are put in thie folder, they won't be deleted. In this case, you need to check the Created date rather than the modified date. To do that, change line 41 to this:

```
For /F "Tokens=1-3 Skip=4 Delims= " %%I In ('Dir /A-D/TC "%_FName%"') Do (
```
If the files are in use, they might not be able to be deleted either.


```
@Echo Off
:: Enter Path to the folder, the file names, and the age in minutes
:: when the file should be deleted. 
Set _TFolder=C:\Test Dir
Set _FN1=sas-mail.txt
Set _FN2=sas-fax.txt
Set _Delay=15
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
CD /D "%_TFolder%"
Call :_SetupVariables
:_CheckFN
Set _WTime1=1
Set _WTime2=1
If Exist "%_FN1%" (Call :_CheckFile "%_FN1%") Else Goto _CheckFN2
:: If older than Delay time, delete file, then check the next one
If %_TimeDif% GEQ %_Delay% Del /A "%_FN1%" & Goto _CheckFN2
:: If file not old enough to delete, set wait time to the remaining time
Set /A _WTime1=(_Delay-_TimeDif)*60
:_CheckFN2
If Exist "%_FN2%" (Call :_CheckFile "%_FN2%") Else Goto _SetWait
:: If older than Delay time, delete file, then set wait time
If %_TimeDif% GEQ %_Delay% Del /A "%_FN2%" & Goto _SetWait
:: If file not old enough to delete, set wait time to the remaining time
Set /A _WTime2=(_Delay-_TimeDif)*60
:_SetWait
:: Select the shortest wait time, or 60 seconds
If %_WTime1% GTR %_WTime2% (Set _WTime=%_WTime2%) Else Set _WTime=%_WTime1%
Call :%_tWait% %_WTime%
Goto _CheckFN
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::: 
:_CheckFile
Set _FName=%~1
:: Get date and time for the file
Set _Tdt=
Set _Ttm=
Set _Tap=
For /F "Tokens=1-3 Skip=4 Delims= " %%I In ('Dir /A-D "%_FName%"') Do (
  Set _Tdt=%%I
  Set _Ttm=%%J
  Set _Tap=%%K
  Goto _CheckDate
)
:_CheckDate
Echo.%_Tap%|Findstr /I /R /C:"[AP]M">Nul&&Set _Ttm=%_Ttm% %_Tap%
Call :_Getdate %_Tdt%
Call :_GetTime %_Ttm%
Call :_JDate %_fdate:~0,4% %_fdate:~4,2% %_fdate:~6,2%
Set _FileDate=%_Jdate%
Set _FileTime=%_mmmm%
:_CheckNow
:: Get current date and time
Call :_Getdate
Call :_GetTime
Call :_JDate %_fdate:~0,4% %_fdate:~4,2% %_fdate:~6,2%
Set /A _TimeDif=(_Jdate-_FileDate)*1440+_mmmm-_FileTime
:: Return with TimeDif set for current file.
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetupVariables
:: Get Regional Settings to use in date routines
Set _iDate=
Set _sDate=
Set _iTime=
Set _sTime=
Set _tWait=_Time0
:: Check if sleep or timeout commands availible
For %%I In (sleep.exe) Do If Not "%%~$PATH:I"=="" Set _tWait=_Time1
For %%I In (timeout.exe) Do If Not "%%~$PATH:I"=="" Set _tWait=_Time2
:: Default Delimiter of TAB and Space are used
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v iTime') Do Set _iTime=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v sTime') Do Set _sTime=%%B
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_GetDate
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified by TheOutcaste http://forums.techguy.org from
:: SortDate Written byRob van der Woude http://www.robvanderwoude.com
:: to check for two digit years
:: If passed a parameter use that for the date
If "%~1"=="" (Set _Date=%Date%) Else Set _Date=%*
If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
Set _TDYM=
If %_iDate%==0 For /F "Tokens=%_NumTok% Delims=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
If %_iDate%==1 For /F "Tokens=%_NumTok% Delims=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
If %_iDate%==2 For /F "Tokens=%_NumTok% Delims=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
If "%_fdate:~7,1%"=="" For /F "Tokens=3 skip=3" %%I In ('Reg Query "HKCU\Control Panel\International\Calendars\TwoDigitYearMax" /V 1 2^>Nul') Do Set _TDYM=%%I
If Defined _TDYM (Set _MaxY=%_TDYM:~2%&Set _Cent=%_TDYM:~0,2%) Else (Set _MaxY=29&Set _Cent=20)
Set /A _Cm1=_Cent-1
If "%_fdate:~7,1%"=="" If %_fdate:~0,2% LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
Goto:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_GetTime
:: Modified by TheOutcaste http://forums.techguy.org from SortTime
:: Written by Rob van der Woude http://www.robvanderwoude.com
:: Time is returned in 24 hour format as hhmm in variable _hhmm
:: Time in minutes is returned in _mmmm
If "%~1"=="" (For /F "Tokens=*" %%A In ('TIME/T') Do Set _Time=%%A) Else Set _Time=%*
If %_iTime%==1 Goto _in24format
For /F "Tokens=1* Delims=%_sTime% " %%A In ('Echo %_Time%') Do (
	Set _Hour=%%A
	Set _Minutes=%%B
)
Set _AMPM=
Echo.%_Minutes%| Find /I "A" >Nul && Set _AMPM=A
Echo.%_Minutes%| Find /I "P" >Nul && Set _AMPM=P
If [%_AMPM%]==[] Set _AMPM=A
Set _Minutes=%_Minutes:~0,2%
If %_Hour:~0,1%==0 Set _Hour=%_Hour:~-1%
If %_Hour% LSS 12 If /I %_AMPM%==P Set /A _Hour=%_Hour%+12
If %_Hour% LSS 10 If /I %_AMPM%==A Set _Hour=0%_Hour%
If %_Hour% EQU 12 If /I %_AMPM%==A Set _Hour=00
Set _Time=%_Hour%%_sTime%%_Minutes%
:_in24format
For /F "Tokens=1,2* Delims=%_sTime% " %%A In ('Echo %_Time%') Do (
Set _hhmm=%%A%%B
If %_Hour:~0,1%==0 Set _Hour=%_Hour:~-1%
If %_Minutes:~0,1%==0 Set _Minutes=%_Minutes:~-1%
Set /A _mmmm=_Hour*60+_Minutes
)
Goto:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date in _JDate
:: Usage
:: Call :_JDate %_GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
:: Code taken from datediff.bat written by Rob van der Woude
:: http://www.robvanderwoude.com
:: Modified to handle months and days without leading zeros
:: By TheOutcaste http://forums.techguy.org
Set _JMM=%2
Set _JDD=%3
If 1%_JMM% LSS 110 Set _JMM=%_JMM:~-1%
If 1%_JDD% LSS 110 Set _JDD=%_JDD:~-1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/Library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1  = %1 + 4800
Set /A _JDate  = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) / 100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF 
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Time0
>Nul Ping 1.0.0.0 -n 1 -w %~1000
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Time1
>Nul Sleep %~1
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Time2
>Nul Timeout /NOBREAK /T %~1
Goto :EOF
```


----------

