# Solved: Batch file to check a directory is 30days old



## trusp (Dec 5, 2008)

Hello, 
I need a batch file to list all directories which are 30days old in a specified path. 

Example C:\Sample\ 

Here all the directories which are 30days old from the modified date must be listed. 

Batch file must be DOS based. 

Please help me on this.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!
The file in [post=6301111]this post[/post] should do what you want.
Change the *_DaysKept* set statement to *30* and the *_Path* set statement to* C:\Sample*, then change the For loop to remove the number check.

Change it from this:

```
For /F "tokens=%_Tok% skip=4" %%I In ('dir "%_Path%[COLOR=Red]\1*[/COLOR]" /AD /OD /TW[COLOR=Red] ^|Findstr /E "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"[/COLOR]') Do (
```
to this:

```
For /F "tokens=%_Tok% skip=4" %%I In ('dir "%_Path%" /AD /OD /TW') Do (
```
Best to download the attached source. If you copy and paste, be sure to edit the lines shown in red to restore the tab character

HTH

Jerry


----------



## trusp (Dec 5, 2008)

Jerry

Thank you very much.

I tried this sample as below:
@ echo off
dir "C:\Sample" /AD /OD /TW > Temp.txt
For /F "tokens=* skip=4" %%I In (C:\Temp.txt) Do Echo %%I

And i got the output as

11/05/2008 02:08 PM xxxx
11/24/2008 05:59 PM Time
12/02/2008 09:53 AM Documentation
12/04/2008 06:36 PM .
12/04/2008 06:36 PM ..
0 File(s) 0 bytes
24 Dir(s) 18,592,337 bytes free

It listed all the root folder information.

But still i can't find any way to list the folders which are >=30days old from the last date of modification.

Please help me out in this issue.

Thanks.

Trusp


----------



## trusp (Dec 5, 2008)

Jerry 

I can't find _DaysKept where this is used.
I am new to this DOS.
So, Please provide more clarity in this.

Thanks Jerry

Trusp


----------



## TheOutcaste (Aug 8, 2007)

You need to use the complete batch file from the other post.

Download the test.txt file attached to the post I referenced above. Save it as _*filename*_.bat or _*filename*_.cmd. Use what ever you want for _*filename*_. Right click and choose *Edit*. It will open in Notepad.

Refer back to the post to find the *Set _DaysKept=7* and *Set _Path=C:\Test1* lines, they are shown in blue. Use the forum post to guide you to those lines in the file you downloaded that is now open in Notepad so you can edit them to match what you want, ie . *Set _DaysKept=30* and *Set _Path=C:\Sample*
You can also use Search (CTRL+F) in Notepad to find *Set _D* and *Set _P*

Then find the For loop and edit out the parts in red, so this line:

```
For /F "tokens=%_Tok% skip=4" %%I In ('dir "%_Path%[COLOR=Red]\1*[/COLOR]" /AD /OD /TW[COLOR=Red] ^|Findstr /E "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"[/COLOR]') Do (
```
will look like this:

```
For /F "tokens=%_Tok% skip=4" %%I In ('dir "%_Path%" /AD /OD /TW') Do (
```
Jerry


----------



## trusp (Dec 5, 2008)

Jerry,

It failed.. i changed those two variables and then the for loop:
Then to it said as 
For was unexpected at this time.

This is the CLI outcome on execution:
C:\>Test.cmd

C:\>Setlocal EnableDelayedExpansion
C:\>Set _DaysKept=30
C:\>Set _Path=C:\dhanesh
C:\>Call :GetDate
C:\>If NOT [] == [] Set Date=
C:\>If "Wed 12/10/2008A" LSS "A" (Set _NumTok=1-3 ) Else (Set _NumTok=2-4 )
C:\>For /F "SKIP=3 TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%B
C:\>For /F "SKIP=3 TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%B
For was unexpected at this time.
C:\>IF ==0 For /F "TOKENS=2-4 DELIMS= " %B In ("Wed 12/10/2008") Do Set _fdate=%D%B%C
C:\>

Please help me...

trusp


----------



## TheOutcaste (Aug 8, 2007)

I don't know why, but for some reason the TAB character in three of the statements has been converted to spaces. Also, one long line got word wrapped. Probably because I used the .txt extension. Seems the forum might modify those files eventually.
I just tested that; I can upload a file with a .txt extension, then download it and it's OK, so it doesn't happen right away.

I've modified the file so it uses a variable in those lines, so only one line has to be edited if needed, fixed the word wrapped line, and changed _DaysKept to 30.

I changed the _Path to C:\Sample; If you don't want the real path shown, you may want to edit the above post to remove it. If it's been more than 24 hours, you can use the report button and ask a moderator to change it to C:\Sample.

I've attached the file as a zip this time -- hopefully the .cmd file inside won't get modified.

I also fixed one thing I forgot, that would prevent it from finding folders with names more than 10 characters in length.

Try this out and see how it works for you.

Jerry


----------



## trusp (Dec 5, 2008)

Jerry

Thank you very much for your response.
Actually the execution of the script started and it responded as:

The system cannot find the path specified.
The system cannot find the path specified.
No Folders Found to delete

So please still your help is needed here for me,
I downloaded the attachment and executed. It produced some outcome as " The system cannot find the path specified."
I have placed the CLI outcome for your reference.

Please provide your suggestions on it.

C:\>delbydate.cmd
C:\>Set _TabSpace=
C:\>Setlocal EnableDelayedExpansion
C:\>Set _DaysKept=30
C:\>Set _Path=C:\Sample
C:\>Call :GetDate
C:\>If NOT [] == [] Set Date=
C:\>If "Thu 12/11/2008A" LSS "A" (Set _NumTok=1-3 ) Else (Set _NumTok=2-4 )
C:\>For /F "SKIP=3 TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%B
C:\>Set _iDate=
C:\>Set _iDate=0
C:\>For /F "SKIP=3 TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%B
C:\>Set _sDate=
C:\>Set _sDate=/
C:\>IF 0 == 0 For /F "TOKENS=2-4 DELIMS=/ " %B In ("Thu 12/11/2008") Do Set _fdate=%D%B%C
C:\>Set _fdate=20081211
C:\>IF 0 == 1 For /F "TOKENS=2-4 DELIMS=/ " %B In ("Thu 12/11/2008") Do Set _fdate=%D%C%B
C:\>IF 0 == 2 For /F "TOKENS=2-4 DELIMS=/ " %B In ("Thu 12/11/2008") Do Set _fdate=%B%C%D
C:\>Goto:EOF
C:\>Set _yy=2008
C:\>Set _mm=12
C:\>Set _dd=11
C:\>Call :JDate 2008 12 11
C:\>Set _JMM=12
C:\>Set _JDD=11
C:\>IF 112 LSS 110 Set _JMM=2
C:\>IF 111 LSS 110 Set _JDD=1
C:\>Set /A _JMonth1 = ( 12 - 14 ) / 12
C:\>Set /A _JYear1 = 2008 + 4800
C:\>Set /A _JDate = 1461 * ( 6808 + 0 ) / 4 + 367 * ( 12 - 2 -12 * 0 ) / 12 - ( 3 * ( ( 6808 + 0 + 100 ) / 100 ) ) / 4 + 11 - 32075
C:\>For %A In (_JMonth1 _JYear1) Do Set %A=
C:\>Set _JMonth1=
C:\>Set _JYear1=
C:\>Goto:EOF
C:\>Set _JToday=2454812
C:\>Set /a _DelDate=_JToday-30-1
C:\>For /F "TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v iTime') Do Set _iTime=%B
C:\>Set _iTime=VERSION 3.0
C:\>Set _iTime=
C:\>Set _iTime=0
C:\>If Exist "1\tf}1{" Del "1\tf}1{"
C:\>PushD C:\Sample
C:\Sample>Set _s=s
C:\Sample>If 30 == 1 set _s=
C:\Sample>Echo Please wait, searching for folders more than 30 days old
Please wait, searching for folders more than 30 days old
C:\Sample>If 0 == 0 (Set _Tok=1,4* ) Else (Set _Tok=1,3* )
C:\Sample>For /F "tokens=1,4* skip=4" %I In ('dir "C:\Sample" /AD /OD /TW') Do (
et _Name=%K
Set If %J == (
Call :GetDate %I
Call :JDate !_fDate:~,4! !_fDate:~4,2! !_fDate:~6,2!
If !_JDate! LEQ 2454781 (If Exist "1\tf}1{" (Echo %I:%~fK 1>>"1\tf}1{" ) Else (
Echo.1>"1\tf}1{"
Echo Do you wish to delete the following folders? 1>>"1\tf}1{"
Echo Date Name 1>>"1\tf}1{"
Echo %I:%~fK 1>>"1\tf}1{"
) ) Else (Goto :_allFound )
)
)
C:\Sample>(
Set _Name=fold2
If == (
Call :GetDate 10/11/2008
Call :JDate !_fDate:~,4! !_fDate:~4,2! !_fDate:~6,2!
If !_JDate! LEQ 2454781 (If Exist "1\tf}1{" (Echo 10/11/2008:C:\Sample\fold2 1>>"1\tf}1{" ) Else (
Echo.1>"1\tf}1{"
Echo Do you wish to delete the following folders? 1>>"1\tf}1{"
Echo Date Name 1>>"1\tf}1{"
Echo 10/11/2008:C:\Sample\fold2 1>>"1\tf}1{"
) ) Else (Goto :_allFound )
)
)
C:\Sample>If NOT [10/11/2008] == [] Set Date=10/11/2008
C:\Sample>If "10/11/2008A" LSS "A" (Set _NumTok=1-3 ) Else (Set _NumTok=2-4 )
C:\Sample>For /F "SKIP=3 TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%B
C:\Sample>Set _iDate=
C:\Sample>Set _iDate=0
C:\Sample>For /F "SKIP=3 TOKENS=2* DELIMS= " %A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%B
C:\Sample>Set _sDate=
C:\Sample>Set _sDate=/
C:\Sample>IF 0 == 0 For /F "TOKENS=1-3 DELIMS=/ " %B In ("10/11/2008") Do Set _fdate=%D%B%C
C:\Sample>Set _fdate=20081011
C:\Sample>IF 0 == 1 For /F "TOKENS=1-3 DELIMS=/ " %B In ("10/11/2008") Do Set _fdate=%D%C%B
C:\Sample>IF 0 == 2 For /F "TOKENS=1-3 DELIMS=/ " %B In ("10/11/2008") Do Set _fdate=%B%C%D
C:\Sample>Goto:EOF
C:\Sample>Set _JMM=10
C:\Sample>Set _JDD=11
C:\Sample>IF 110 LSS 110 Set _JMM=0
C:\Sample>IF 111 LSS 110 Set _JDD=1
C:\Sample>Set /A _JMonth1 = ( 10 - 14 ) / 12
C:\Sample>Set /A _JYear1 = 2008 + 4800
C:\Sample>Set /A _JDate = 1461 * ( 6808 + 0 ) / 4 + 367 * ( 10 - 2 -12 * 0 ) / 12 - ( 3 * ( ( 6808 + 0 + 100 ) / 100 ) ) / 4 + 11 - 3207
C:\Sample>For %A In (_JMonth1 _JYear1) Do Set %A=
C:\Sample>Set _JMonth1=
C:\Sample>Set _JYear1=
C:\Sample>Goto:EOF
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.


----------



## TheOutcaste (Aug 8, 2007)

For the line *If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"*
You show your system sees it as this:
*C:\>If Exist "1\tf}1{" Del "1\tf}1{"

*Looks like the %temp% variable on your system is set to 1 instead of to the path to the current users Temp folder.
Are you running a batch file that changes the temp variable before you are running this file? That's why I always add an underscore to variable names, so I don't inadvertantly modify a system variable. Usually do the same for labels as well.

If you open a Command Prompt, and type *Set t*, what do you get?

You should get this on Win2K or XP:

```
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>set t
TEMP=C:\DOCUME~1\[COLOR=DarkRed][I]THEOUT~1[/I][/COLOR]\LOCALS~1\Temp
TMP=C:\DOCUME~1\[COLOR=DarkRed][I]THEOUT~1[/I][/COLOR]\LOCALS~1\Temp

C:\>
```
And this on Vista:

```
Microsoft Windows [Version 6.0.6000]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\TheOutcaste>set t
TEMP=C:\Users\[COLOR=DarkRed][I]THEOUT~1[/I][/COLOR]\AppData\Local\Temp
TMP=C:\Users\[COLOR=DarkRed][I]THEOUT~1[/I][/COLOR]\AppData\Local\Temp

C:\Users\TheOutcaste>
```
With the appropriate _user_ name of course.

Also, are you typing in the results, or copying and pasting? I ask because of these lines:


trusp said:


> C:\Sample>For /F "tokens=1,4* skip=4" %I In ('dir "C:\Sample" /AD /OD /TW') Do (
> * et* _Name=%K
> * Set *If %J == (
> Call :GetDate %I
> ...


The S is missing from the Set _Name line, but the output shows it's there, and there is Set IF on the next line, but the output doesn't show the set.

To copy rather than having to type, you can right click in the Command Prompt window, choose *Select All*, then press enter. You can then paste the contents into notepad or the reply window. Or choose Select All from the Context menu (ALT+Spacebar) under edit.

If you are using *temp*, or it's been modified for some other reason, and can't change it, there are a couple of options. If you aren't using *tmp* we can use that, or we can set it to use the same folder the batch file is in.

It looks like it found a folder named *fold2* with a modified date of Oct 11, 2008, but then failed because it couldn't find the Temp folder, so once we get that part resolved, we should be good to go.

HTH

Jerry


----------



## trusp (Dec 5, 2008)

Jerry

It worked fine.Thank you very much for your support.

Trusp


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!

Couple of questions in case someone else has any issues getting it to work.
Was it just the temp variable had been modified?
Did opening a new Command Prompt resolve that, or did you have to switch to using *tmp* instead of *temp*?
Or did you find something else?

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------



## trusp (Dec 5, 2008)

Jerry

I created a new temp in the batch file path itself. and it executed.
It also worked by changing the temp path.

Thanks

Trusp


----------



## trusp (Dec 5, 2008)

Jerry,

I am getting the list successfully.But also . and .. is been listed.
Can you tell me how to avoid . and .. while list is been generated.

Example:
dir1
dir2
.
..

In the above list you can find that . and ..
I need a list with out . and .. 
Please help me in this issue.

Trusp


----------



## TheOutcaste (Aug 8, 2007)

Change this line (it's line 31) 
*If %%J==^<DIR^> (*
to this
*If NOT "%%K"=="." If NOT "%%K"==".." If %%J==^<DIR^> (*

The *.* and *..* will still be found during the loop, so if you are using an *Echo* statement to list the names it finds they will still appear; that's just the way the *Dir* command works.
The two additional *If* statements will keep them from being selected for deletion.

The *.* entry is the current folder and the *..* entry is the Parent folder, so for the C:\Sample folder, the list of files to be deleted would include 
*C:\* for the *..*
*C:\Sample* for the *.*

My parent and Sample test folders were created new, so they never appeared in my list to be deleted, and the previous version of the file was looking for folders with names of exactly 10 digits so I never noticed those two entries might be included.

Thanks for finding that.:up:
HTH

Jerry


----------



## trusp (Dec 5, 2008)

Jerry

It worked fine.Thank you very much for your response.
I have another doubt:
Ex:1
@ echo off
set TestPath="C:\Test Dir\Testing\tmp.txt"
for /f "eol= tokens=* delims= " %%i in (%TestPath%) do (
echo %%i
)

(or)

Ex:2
@ echo off
set TestPath=C:\Test Dir\Testing
for /f "eol= tokens=* delims= " %%i in ("%TestPath%\tmp.txt") do (
echo %%i
)

Here as found in the above examples. I am trying to access the file content. But its not responding as expected. 

Here I am trying to access the file contents. But the for loop is not supporting the path names if there is a space in any directory.

The same code(for loop) worked fine when i not use any space(Test_Dir in path) in the path name.as in below Example:
Ex:3
@ echo off
set TestPath=C:\Test_Dir\Testing
for /f "eol= tokens=* delims= " %%i in ("%TestPath%\tmp.txt") do (
echo %%i
)

Please provide your suggestions in this. 

I need to pass a path name to the for loop to access the file content. The path will have space (since the directory has some space)

How can we solve this issue. ?
Please help me.

Trusp


----------



## TheOutcaste (Aug 8, 2007)

From For /?:

```
FOR /F ["options"] %variable IN ([COLOR=Blue][B]file-set[/B][/COLOR]) DO command [command-parameters]
FOR /F ["options"] %variable IN ([COLOR=Red][B]"[/B][/COLOR][COLOR=DarkRed][B]string[/B][/COLOR][COLOR=Red][B]"[/B][/COLOR]) DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
```
If you put quotes around *file-set* (or use a variable that contains quotes) it will be interpreted as a *string*, not a file to be read, so it will return the file path/name.
To use paths/file names with spaces use the *usebackq* option.

```
@echo off
set TestPath=[COLOR=Red][B]"[/B][/COLOR]C:\Test Dir\Testing\tmp.txt[COLOR=Red][B]"[/B][/COLOR]
for /f  "eol=  tokens=* delims= " %%i in (%TestPath%) do (
echo %%i
)
```
In the above %TestPath% expands to *"**C:\Test Dir\Testing\tmp.txt**"* so it will display *C:\Test Dir\Testing\tmp.txt*

```
@echo off
 set TestPath="C:\Test Dir\Testing\tmp.txt"
 for /f  "usebackq eol= tokens=* delims= " %%i in (%TestPath%) do (
 echo %%i
 )
```
The above will display the _contents_ of *C:\Test Dir\Testing\tmp.txt*.

Same with example 2:

```
@echo off
set TestPath=C:\Test Dir\Testing
for /f "eol= tokens=* delims= " %%i in ("%TestPath%\tmp.txt") do (
echo %%i
)
```
In the above *"%TestPath%\tmp.txt"* expands to *"**C:\Test Dir\Testing\tmp.txt**"* so it displays *C:\Test Dir\Testing\tmp.txt*

```
@echo off
set TestPath=C:\Test Dir\Testing
for /f "usebackq eol= tokens=* delims= " %%i in ("%TestPath%\tmp.txt") do (
echo %%i
)
```
The above will display the _contents_ of *C:\Test Dir\Testing\tmp.txt*.

Note that if you use *tokens=** the *delims* option will be ignored, so it doesn't need to be specified. *"tokens=1 delims="* or just *"delims="* has the same effect.
You also don't need to use the *"eol=* *"* option. It is not set by default so you only use that if you want to set a value.
So all you need is *"usebackq tokens=*"* or *"usebackq delims="*

HTH

Jerry


----------



## trusp (Dec 5, 2008)

Jerry

Thank you very much for your great support.

Trusp


----------

