Page 1 of 1
checking consecutive dates
Posted: 15 Jul 2009 15:31
by alo
ok here's an interesting problem that i have an idea on how to solve but wanted to get some expert advice so i'll post it here...
i have a folder with 0 to thousands of subfolders where all the subfolders are exactly 10 characters long named YYYY-MM-DD (on the day it was created)
i'm trying to make a .bat file that will print names of subfolders that aren't consecutive.
in a simple example there would be a directory structure like
Code: Select all
-- DATA
-- 2009-01-01
-- 2009-01-02
-- 2009-01-03
-- 2009-01-05
-- 2009-01-09
-- 2009-01-10
...
and the batch file would loop through the folders in DATA\* and print something like
Code: Select all
2009-01-04 missing
2009-01-06 missing
2009-01-07 missing
2009-01-08 missing
Posted: 15 Jul 2009 20:04
by ghostmachine4
here's a vbscript
Code: Select all
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
Dim ArrFolders()
i=0
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
strFolderName = Replace(eFolder.Name,"-","")
If InStr(strFolderName,"2009") > 0 Then
ReDim Preserve ArrFolders(i)
ArrFolders(i)=strFolderName
i=i+1
End If
Go eFolder
Next
End If
End Sub
' sort folder datetime from earliest to latest
For t=LBound(ArrFolders) To UBound(ArrFolders)
For n = (t + 1) To UBound(ArrFolders)
If ArrFolders(t) >= ArrFolders(n) Then
temp = ArrFolders(n)
ArrFolders(n) = ArrFolders(t)
ArrFolders(t) = temp
End If
Next
Next
For j=ArrFolders(LBound(ArrFolders)) To ArrFolders(UBound(ArrFolders))
found=0
For t=LBound(ArrFolders) To UBound(ArrFolders)
If StrComp(ArrFolders(t),j) =0 Then
found=1
Exit For
End If
Next
If found=0 Then
WScript.Echo "Missing " & j
End If
Next
save as missing.vbs and on command line
Code: Select all
C:\test>cscript /nologo missing.vbs
Missing 20090104
Missing 20090106
Missing 20090107
Missing 20090108
Posted: 16 Jul 2009 19:45
by avery_larry
Hmmm . . . I'd probably use a for loop on a dir ouput that's sorted alphabetically, converting the directory name to a julian date (using the date scripts on this site), and then compare to a simple incremented counter that starts with the first date and then output when the increment doesn't match the folder (and increment until it does match the folder).
That would assume the folder structure is exactly as you said, and it never deviates, and there's never any other folder in the list.
psuedo-code:
Code: Select all
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /on /b /s "path\to\folders"') do (
for /f "tokens=1-3 delims=-" %%b in ("%%~a") do (
set yr=%%b
set /a mnth=100%%c %% 100
set /a dy=100%%d %% 100
call :setjuliandate !yr! !mnth! !dy! (I didn't look up the correct syntax)
call :datecompare
)
)
goto :eof
:setjuliandate
::This code can be found on this site somewhere. Make sure to
::set the variable juliandate in this section
goto :eof
:datecompare
if not defined datecount (
set /a datecount=%juliandate% + 1
goto :eof
)
if not %datecount%==%juliandate% (
call :findnormaldate %datecount%
set /a datecount+=1
goto :datecompare
)
goto :eof
:datecompare
::Again, there is code on this site that will convert
::a julian date to year, month, day. Once you use that code to
::convert, then echo the results as missing.
set mnth=0%mnth%
set dy=0%dy%
echo Missing %yr%-%mnth:~-2%-%dy:~-2%
goto :eof