Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Script to extract directory names by modified dateHelpful Member! 

ccoyle (MIS) (OP)
9 Nov 11 20:04
Hi!
Help! :P

I have a ton of directories and subdirectories under a network share (H:). I need to identify any directory not modified since a certain date and list them in a file - .csv would be swell!

I've used the following cmd to extract the directory data but it isn't giving me the criterea I need (obviously).

C:\Boot>dir /s /a:d /o:-d > c:\txt.txt

The output looks like this:



 Volume in drive C is OSDisk
 Volume Serial Number is 64FF-6D04

 Directory of C:\Boot

05/11/2011  09:54 PM    <DIR>          ..
05/11/2011  09:54 PM    <DIR>          .
05/11/2011  09:54 PM    <DIR>          zh-TW
05/11/2011  09:54 PM    <DIR>          zh-HK
05/11/2011  09:54 PM    <DIR>          zh-CN


If it's easier, maybe a methood to extract any line with a certain date and list in another file would also work.



Thanks for any help!
Geates (Programmer)
10 Nov 11 10:19
This has been addressed in one iteration or another. Recursively traverse a directory.  Check the date modified as you go and record any item dates that are old.

Take a look that these threads.
thread329-1651080: Moving Files Based on Modified Data Recursively
thread329-1663859: Recursive Date Modifed Search & Log
thread329-1481428: Delete Folder & Contents based on modified date

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ccoyle (MIS) (OP)
10 Nov 11 14:09
Thanks Geates!

I'm trying to use the second option you gave me here but having issues. The second option thread329-1663859: Recursive Date Modifed Search & Log: Recursive Date Modifed Search & Log) is used to list all files accessed within the last 180 days. I need a script that tells me all the folders (and subfolders) NOT accessed within the last 771 days (10/1/2009). So basically I need to list out the folders with a last modified date of 10/1/2009 or older. Can that script be tweaked to give me what I need?

Here's the script:

const ForAppending = 8
set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
set objTextFile = objFSO.OpenTextFile ("C:\Log.txt", ForAppending, True)

'************************************************************************
' FUNCTIONS'************************************************************************

function getOldProjects(strSource, intAge)
    dim objFolder
      set objFolder = objFSO.GetFolder(strSource)

    for each objSubFolder in objFolder.SubFolders
        strLog = strLog & getOldProjects(objSubFolder.Path, intAge)
    next

    for each objFile in objFolder.Files
        if (strRecent = "") then strRecent = objFile.Path
        if (objFile.DateLastModified > objFSO.GetFile(strRecent).DateLastModified) then strFile = objFile.Path
    next

    if (strFile <> "") then
        strModDate = objFSO.GetFile(strFile).DateLastModified
        strProjectFolder = objFSO.GetParentFolderName(strFile)
        if (datediff("d", strModDate, now) > intAge) then
            if (ubound(split(strProjectFolder, "\")) = 7) then
                strLog = strLog & vbCRLF & strProjectFolder & " | " & objFolder.DateLastModified & " | " & objFolder.DateLastAccessed
            end if
        end if
    end if
'if strLog <> "" then
'    msgbox strLog
'End if
    getOldProjects = strLog
end function

'************************************************************************
' MAIN'************************************************************************

intAge = 180 '30 days * 6 months
strSource = "S:\Engineering\"

strLog = getOldProjects(strSource, intAge)

Header = "Folder | Last Date Modified | Last Date Accessed"
objTextFile.WriteLine(Header)
objTextFile.WriteLine(strLog)
objTextFile.Close




Thanks!
Chris
Geates (Programmer)
10 Nov 11 14:58
yes it can be modified...

Are you asking how?

CODE

CONST strFolder = "S:\Engineering"
CONST intAge = 771 'days

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
set objTextFile = objFSO.OpenTextFile("C:\log.txt", 1, true, 0)

function obsoleteDir(strDir, intAge)
    set objFolder = objFSO.GetFolder(strDir)
    for each objSubFolder in objFolder.SubFolders
        strResults = strResults & obsoleteDir(objSubFolder.Path, intAge)
    next
    
    for each objFile in objFolder.Files
        if (datediff("d", objFile.DateLastModified, now) > intAge) then strResults = strResults & objFolder.Path & vbNewLine
        exit for
    next

    obsoleteDir = strResults
end function

strOldDirs = obsoleteDir(strFolder, intAge)
arrFolders = split(strOldDirs, vbNewLine)

for i = 0 to ubound(arrFolders) - 1
   strFolder = arrFolders(i)
   objTextFile.WriteLine strFolder
next i

-Geates
 

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ccoyle (MIS) (OP)
10 Nov 11 15:25
Thanks Geates but where's the end of the script?

Honestly, if I knew how to script I wouldn't need someone to do it for me. Even side by side (the 2 scripts together) I can't figure out how to end your script ...

I do appreciate you're help (and your quick response on my last post!).
 
Geates (Programmer)
10 Nov 11 18:06
That is the whole script.  The original contained code that is irrelevant to your question (I assume).  The code I posted will do what your original post asks (almost).

CODE

CONST strFolder = "S:\engineering"
CONST intAge = datediff("d", "10/1/2009", now) 'days

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

function obsoleteDir(strDir, intAge)
    set objFolder = objFSO.GetFolder(strDir)
    for each objSubFolder in objFolder.SubFolders
        strResults = strResults & obsoleteDir(objSubFolder.Path, intAge)
    next
    
    for each objFile in objFolder.Files
        if (datediff("d", objFile.DateLastModified, now) > intAge) then strResults = strResults & objFolder.Path & "|" & objFolder.DateLastModified & vbNewLine
        exit for
    next

    obsoleteDir = strResults
end function

strObsoleteDirs = obsoleteDir(strFolder, intAge)
wscript.echo strObsoleteDirs

I put echoed strObsoleteDirs so you can see what the function returned and how the values are later processed.  

I see that you would like to export the information to a .csv.  As you probably know a csv is a comma separated value file.  All we really need to do is make sure our output text file has an extention of .csv

CODE

set objCSV = objFSO.OpenTextFile("C:\log.csv", 1, true, 0)

We also want to make sure that our values are encapsulated by quotes and separated by commas to clearly define the values in the csv file.  A csv file typically has a header row followed by the data.  There is no designation for a header row, the first row in a csv is assumed as the header.

It makes for confusion but a quote is the escape character in vbs, unlike the commas back-slash (\).  I color coded the quote so it's easier to read. Once we write the header to the csv file, we want to split up strObsoleteDirs into an array.  Traverse through and split those values (all but the last one) to get the foldername and date.  Then write the info to the file. Close it and we're done!

CODE

strHeader = """Folder"", ""Date Last Modified"""
objCVS.WriteLine strHeader

arrRows = split(strObsoleteDirs, vbNewLine)
for i = 0 to ubound(arrRows) - 1 'all but the last one.
   arrData = split(arrRows(i), "|")
   strName = arrData(0)
   strDate = arrData(1)
   strRow = """" & strName & """,""" & strDate & """"
   objCSV.WriteLine strRow
next

objCSV.close

Simply paste all the code in a vbs file and you're good (I think).  I strongly recommend thoroughly reading the code to get an idea of what it's doing.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ccoyle (MIS) (OP)
10 Nov 11 20:19
Thanks a MILLION Geates!

I'll check it out in the morning and see where we're at.

I really do read through the scripts and try to understand what's going on - some of it I understand, but alot of it is just beyond me - especially cause there seems to be endless ways to "skin the cat" in vbs. The only way I'll ever be able to do this stuff is if I devote my life to it (which I would love to do) but there's too much demand on me elsewhere ... and that sux.

Thanks again for your time and guidance, it's truly appreciated!

-Chris
 
Geates (Programmer)
11 Nov 11 8:45

Quote:

especially cause there seems to be endless ways to "skin the cat" in vbs

There are also several ways to chop an onion but you don't need to know them all.  It certainly helps to know them all when perfecting a recipe but, when learning, as long as add the onion, how it's chopped doesn't matter.  


-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ccoyle (MIS) (OP)
11 Nov 11 10:33
Ha! Good point!

So we have an error:
Error: Expected literal constant
Line: 3
Char: 48

Here's what I put together:


CONST strFolder = "F:\_WD\Pers"
CONST intAge = datediff("d", "10/1/2009", now) '772days

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
set objCSV = objFSO.OpenTextFile("C:\log.csv", 1, true, 0)

function obsoleteDir(strDir, intAge)
    set objFolder = objFSO.GetFolder(strDir)
    for each objSubFolder in objFolder.SubFolders
        strResults = strResults & obsoleteDir

(objSubFolder.Path, intAge)

next

    for each objFile in objFolder.Files
        if (datediff("d", objFile.DateLastModified, now) >

intAge) then strResults = strResults & objFolder.Path & "|" &

objFolder.DateLastModified & vbNewLine
    exit for

next

    obsoleteDir = strResults

end function

strObsoleteDirs = obsoleteDir(strFolder, intAge)
wscript.echo strObsoleteDirs

strHeader = """Folder"", ""Date Last Modified"""
objCSV.WriteLine strHeader

arrRows = split(strObsoleteDirs, vbNewLine)
for i = 0 to ubound(arrRows) - 1 'all but the last one.
arrData = split(arrRows(i), "|")
strName = arrData(0)
strDate = arrData(1)
strRow = """" & strName & """,""" & strDate & """"
objCSV.WriteLine strRow
next

objCSV.close



It looks to me like it wants a value for the days. Your initial script just had 'days but that still gave the error. I added the 772 with and without a space but still getting the error. I also corrected an objCSV reference (it had read objCVS ... which reminds me I need to get more toothpaste :))


Thanks Geates!
Geates (Programmer)
11 Nov 11 12:18
That's right.  Constant must be assigned definitive values.  Even though the function returns a definitive value, a call to a function is simply that, a call.  A value isn't returned until the function executes it's code.

intAge should be a regular variable, not a constant.

CODE

CONST intAge = datediff("d", "10/1/2009", now)

-Geates
 

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ccoyle (MIS) (OP)
14 Nov 11 8:55
Ok, we're getting close - I can taste it!

It looks like the script is getting the correct info but it just displays it in a Windows Script Host window – doesn't write it to C:\log.csv and all I can do is close the window (can't copy the text).
log.csv is created but the data isn't written to the file and when I close the window, I get an error:
Error: Bad file mode
Line 30
Char: 1

Please help me bring this one home!

This is where we're at:

CONST strFolder = "H:\BOARD"
intAge = datediff("d", "10/1/2009", now) 'days

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
set objCSV = objFSO.OpenTextFile("C:\log.csv", 1, true, 0)

function obsoleteDir(strDir, intAge)
    set objFolder = objFSO.GetFolder(strDir)
    for each objSubFolder in objFolder.SubFolders
        strResults = strResults & obsoleteDir(objSubFolder.Path, intAge)

next

    for each objFile in objFolder.Files
        if (datediff("d", objFile.DateLastModified, now) > intAge) then strResults = strResults & objFolder.Path & "|" & objFolder.DateLastModified & vbNewLine
    exit for

next

    obsoleteDir = strResults

end function

strObsoleteDirs = obsoleteDir(strFolder, intAge)
wscript.echo strObsoleteDirs

strHeader = """Folder"", ""Date Last Modified"""
objCSV.WriteLine strHeader

arrRows = split(strObsoleteDirs, vbNewLine)
for i = 0 to ubound(arrRows) - 1 'all but the last one.
arrData = split(arrRows(i), "|")
strName = arrData(0)
strDate = arrData(1)
strRow = """" & strName & """,""" & strDate & """"
objCSV.WriteLine strRow
next

objCSV.close

 
ccoyle (MIS) (OP)
14 Nov 11 9:12
After further review ....

We're not picking up the right info with the script above. It looks like the script is just listing all folders (and modified date) it doesn't seem to care about the criteria we've set ....

Whaddya think is the issue?

Thanks!
Helpful Member!  Geates (Programmer)
14 Nov 11 13:21
ah yes.  Can't write to file if it's opened for reading.

CODE

set objCSV = objFSO.OpenTextFile("C:\log.csv", 2, true, 0)

Also, when you post code, please put it inside the [code] code here[/code] tags.

Here is a link that I use to built and troubleshoot.
http://raqxtr.sstar.com/caspdoc/html/vbscript_language_reference.htm

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ccoyle (MIS) (OP)
17 Nov 11 8:58
Thanks a bunch for working with me on this Geates (and teaching me along the way)!
I gave you a star!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close