Computing.Net > Forums > Programming > Batch file to list dir name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to list dir name

Reply to Message Icon

Name: trusp
Date: December 10, 2008 at 03:18:20 Pacific
OS: DOS
CPU/Ram: Intel DUO
Product: Ms / E6550
Comment:

Hi,
I need a batch file to list the directory
name satisfying the following condition:
-Display directory names which are greater
than or equal to 7days old from the last
modification date.

Example:
Test.cmd may be the batch file
C:\test-Dir may be the directory to be
verified.
The batch file must read all the root
directory names inside this C:\test-Dir and
should display the directory names which are
satisfying the condition
Note: The root directory names format may be
of any type like the following:
12.23.12
1234.3.2
2.2.1234
3.2.3.4.5
sample
sample dir
xxx yyy zzz
9.9_test
8_test


Im new to dos. I tried but cant find any
command for it.
Please help me on this issue.

Thanks
Trusp



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: December 10, 2008 at 07:23:32 Pacific
Reply:

Why does this sound so eerily familiar?

These are NOT DOS directory names:

sample dir
xxx yyy zzz

I've been using DOS for 22 years and have yet to make 'date math' work.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: trusp
Date: December 10, 2008 at 07:35:30 Pacific
Reply:

These are sample names of the directories.

Actually i tried to say some sample directory
names inside the specified path.

But to say as "xxx yyy" I specified wrongly.
i.e., What i mean is "program Files" there is
a directory inside the path.

Similarly.,
Do we be able to access the directory last
modified date and check the date was >=7days
from accessing.

Please help me on this.

thanks

Trusp


0

Response Number 3
Name: Holla
Date: December 10, 2008 at 10:55:25 Pacific
Reply:

M2,

I appreciate your patience in trying to
educate people about difference between DOS
and the other stuff that looks like DOS,
which people use interchangingly.

--
Holla.


0

Response Number 4
Name: reno
Date: December 20, 2008 at 10:24:33 Pacific
Reply:

try this...
coding is a bit messy though =P

@echo off
SetLocal EnableDelayedExpansion

set /a OLD=-7
set DIR=c:\

set /a yyyy=%date:~-4%
set /a mm=%date:~3,2%
set /a dd=%date:~0,2%
echo TimeStamp=%yyyy%%mm%%dd% and OLD=%OLD%

call :AdjustDate %OLD%
echo AdjustedTimeStamp=%yyyy%%mm%%dd%
set AdjustedTimeStamp=%yyyy%%mm%%dd%

for /f "tokens=1* delims= " %%a in ('dir %DIR% /tw/ad/o-d/4^|findstr "\/"') do (
set filedate=%%a
set /a filedate=!filedate:~-4!!filedate:~3,2!!filedate:~0,2!
if !filedate! geq !AdjustedTimeStamp! (echo %%a %%b) else (goto :breakLoop)
)
:breakLoop
goto :eof

REM **
REM FUNCTION AdjustDate(howmanydays as %1)
REM **
:AdjustDate
if %1==0 (goto :eof) else (if %1 GEQ 0 (set /a sign=1) else (set /a sign=-1))
set /a loopUntil=%1-!sign!

for /L %%i in (0,%sign%,%loopUntil%) do (
set /a dd+=%sign%
::adjust dd & mm for + sign
for %%a in (29 30 31 32) do if !dd!==%%a (
call :GetNumOfDays !mm! !yy!
if !dd! gtr !numOfDays! (
set /a dd=1
set /a mm+=1
if !mm! gtr 12 (
set /a yyyy+=1
set /a mm=1
)
)
)
::adjust dd & mm for -sign
if !dd!==0 (
set /a mm-=1
if !mm!==0 (
set /a yyyy-=1
set /a mm=12
)
call :GetNumOfDays !mm! !yy!
set /a dd=!numOfDays!
)
:: echo i=%%i !yyyy!-!mm!-!dd! numDays=!numOfDays!
echo >nul
)
goto :eof

REM **
REM FUNCTION GetNumOfDays month as %1, year as %2
REM **
:GetNumOfDays
for %%i in (1 3 5 7 8 10 12) do if %1==%%i set /a numOfDays=31
for %%i in (4 6 9 11) do if %1==%%i set /a numOfDays=30
if %mm%==2 (
set /a remainder=%2-%2/4*4
if !remainder!==0 (set /a numOfDays=29) else (set /a numOfDays=28)
)
goto :eof


0

Response Number 5
Name: reno
Date: December 20, 2008 at 12:52:47 Pacific
Reply:

sorry, there is a bug in the code above, when day or month is the 8th or 9th. it gives octal error.

i'll post a fix solution maybe tomorrow.

basically i know 2 ways to do it:
1. parse the "/" in date format
2. set /a dd=1%dd%-100


0

Related Posts

See More



Response Number 6
Name: reno
Date: December 21, 2008 at 00:57:53 Pacific
Reply:

i just learn new trick from "razor" in the other thread using vbs. it makes the code neater and simple.
i also have the code without vbs if needed.


@echo off
SetLocal EnableDelayedExpansion

set /a DAYS=-7
set DIR=c:\

call :DateAdd(!DAYS!)
echo DateAdd=!DateAdd!

for /f "tokens=1* delims= " %%a in ('dir %DIR% /tw/ad/o-d/4^|findstr "\/"') do (
set filedate=%%a
set /a filedate=!filedate:~-4!!filedate:~3,2!!filedate:~0,2!
if !filedate! geq !DateAdd! (echo %%a %%b) else (echo NOT %%a)
)
:breakLoop
goto :eof


REM Function DateAdd(day as %1) Return Int yyyymmdd
:DateAdd
set /a day=%1
echo d=DateAdd("d",WScript.Arguments(0), date) > #.vbs
echo WScript.Quit DatePart("yyyy", d) * 10000 + DatePart("m", d) * 100 + DatePart("d", d) >> #.vbs
cscript //NOLOGO #.vbs !day!
Set /a DateAdd=%ErrorLevel%
del #.vbs
goto :eof


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to list dir name

Batch file to check a directory is www.computing.net/answers/programming/batch-file-to-check-a-directory-is-/17337.html

DOS batch file to list used variabl www.computing.net/answers/programming/dos-batch-file-to-list-used-variabl/17937.html

batch file to copy & increment www.computing.net/answers/programming/batch-file-to-copy-increment/18184.html