directory command output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
HankBrandenburg
Posts: 2
Joined: 19 Nov 2007 08:41

directory command output

#1 Post by HankBrandenburg » 19 Nov 2007 08:46

I am trying to generate a list of complete path names to directories whose content has been modified after a specific date/time stamp. I am currently using dir /b /o:n /a:d /t:w \\%1\*.* to generate a list of folder names.

My questions:

1 - How do I restrict the list to folders with content modified after a certain date/time?
2 - How do I eliminate folders from the list that end with a specific character?
3 - How do I add the complete path to the directory name.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 20 Nov 2007 02:59

Hi Hank,
2 - How do I eliminate folders from the list that end with a specific character?

Look at :checkForValidName, first I copy the first parameter to a local var.
And on the local var I use the string manipulation function %var:~-3% (not possible on a parameter like %1).

3 - How do I add the complete path to the directory name.

On a parameter you can use %~f1 or here %%~fa (full help at for /?), to expand the name to a full pathname.

1 - How do I restrict the list to folders with content modified after a certain date/time?

A bit more difficult, but depends on the specific problem you want to solve.
I use a solution for comparing two file dateTimes for updating the "object" files if they are older (like make).
But perhaps you need some other solution.
Please post your special problem.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set dateTime="23.12.2007 23:55"

rem *** Iterate through all directories
FOR /f "tokens=*" %%a IN ('dir /b /o:n /a:d /t:w *.*') DO (
  call :checkForValidName "%%~a" result
  echo ValidName=!result! --- %%a
  if !result!==1 (
    call :checkForModifications "%%~fa" %dateTime% modified
  )
)
goto :eof



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:checkForValidName "<name>" <resultVar>
SETLOCAL

rem *** copy to local var for processing
set checkForValidName.tmp=%~1

rem *** Check the last 3 characters for the string "obj"
if "%checkForValidName.tmp:~-3%" EQU "obj" (
  set checkForValidName.result=0
) ELSE (
  set checkForValidName.result=1
)

rem *** set the result and return
(
  ENDLOCAL
  set %~2=%checkForValidName.result%
  goto :eof
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:checkForModifications "<path>" <DateTime> <resultVar>

SETLOCAL
echo    checkForModifications %1
REM *** ToDo: some usefull code
(
  ENDLOCAL
   rem *** Dummy, returns always 0
  set %~3=0
  goto :eof
)

HankBrandenburg
Posts: 2
Joined: 19 Nov 2007 08:41

#3 Post by HankBrandenburg » 21 Nov 2007 12:12

Thanks Jeb - I am trying to locate directory names whose content has been modified after a specific date, and whose name does not end in a $

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 21 Nov 2007 14:51

Hi Hank,

I changed :checkForModifications a bit
and adding some comments, it should work for it.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

REM **** Format "YYYYMMDD"
set dateTime=20070719

rem *** Iterate through all directories
FOR /f "tokens=*" %%a IN ('dir /b /o:n /a:d /t:w *.*') DO (
  call :checkForValidName "%%~a" result
 rem *** echo ValidName=!result! --- %%a
  if !result!==1 (
    call :checkForModifications "%%~fa" "%dateTime%" modified
    if !modified! EQU 1 ECHO One or more files in "%%~a" are modified
  )
)
goto :eof



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:checkForValidName "<name>" <resultVar>
SETLOCAL

rem *** copy to local var for processing
set checkForValidName.tmp=%~1

rem *** Check the last character for the string "$"
if "%checkForValidName.tmp:~-1%" EQU "$" (
  set checkForValidName.result=0
) ELSE (
  set checkForValidName.result=1
)

rem *** set the result and return
(
  ENDLOCAL
  set %~2=%checkForValidName.result%
  goto :eof
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:checkForModifications "<path>" <Date> <resultVar>
:: checks all files in a directory if one or more are newer than <date>
:: FileDate (%%~tf) expected in German/normal format TT.MM.JJJJ
:: resultVar is 0, when no modifications found after <Date>, else it will set to 1


SETLOCAL
rem echo - checkForModifications %1
set modified=0

rem *** Iterate through all files in <path>
FOR %%f IN ("%~1\*.*") DO (

rem   echo  - check file:%%f  with dateTime:%%~tf
  rem *** Split the date from the rest
  FOR /F "tokens=1,2,3 delims=. " %%a IN ("%%~tf") DO (   
    REM *** Format date to a form "YYYYMMDD" so it can compare
    set fileDate=%%c%%b%%a
rem   echo    - !fileDate! %~2
    IF "!fileDate!" GTR "%~2" (
   set modified=1
    )       

  )

)
rem echo modifications(!modified!) in %1

(
  ENDLOCAL
  rem *** Move the result to <resultVar>
  set %~3=%modified%
  goto :eof
)


I suppose, your system use german date/time format, if not you have
to change this line, so it fits to your format.

Code: Select all

FOR /F "tokens=1,2,3 delims=. " %%a IN ("%%~tf")


greetings from nrw
jeb

Post Reply