Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to extract directory names by modified date 1

Status
Not open for further replies.

ccoyle

MIS
Jan 30, 2003
17
US
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!
 
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
thread329-1663859
thread329-1481428

-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
 
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) 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
 
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
 
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!).
 
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([red]"C:\log.csv"[/red], 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 = [red]"[/red][blue]""[/blue]Folder[blue]""[/blue], [blue]""[/blue]Date Last Modified[blue]""[/blue][red]"[/red]
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 = [red]"[blue]""[/blue]"[/red] & strName & [red]"[blue]""[/blue],[blue]""[/blue]"[/red] & strDate & [red]"[blue]""[/blue]"[/red]
   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
 
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
 
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
 
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!
 
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:
[red][s]CONST[/s][/red] 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
 
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

 
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!
 
ah yes. Can't write to file if it's opened for reading.

Code:
set objCSV = objFSO.OpenTextFile("C:\log.csv", [red][b]2[/b][/red], true, 0)

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

Here is a link that I use to built and troubleshoot.

-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
 
Thanks a bunch for working with me on this Geates (and teaching me along the way)!
I gave you a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top