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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading paths from text file (3500 subdirectories)

Status
Not open for further replies.

dantho

Technical User
Apr 25, 2006
6
US
I am not very familiar with VBScript, but would like to use it to read in paths from a text file (3500 subdirectories) to be assigned to the variable: "path1". The files in the subdirectories need deleted that are older than 'x' number of days.

Here is my poor attempt at writing the script, but it needs some expert revision:

----------------------------------------------------------

'Path1 = (path from Test_Del_Spam.txt)'

Dim fso
Dim oFolder
Dim oFile

Const ForReading = 1

Set fso = createobject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(Path1)
Set objTextFile = fso_OpenTextFile("d:\download\" _
& "Test_Del_Spam.txt", ForReading)

Do While objTextFile.AtEndOfStream <> True
If inStr(objtextFile.Readline, "") Then

For Each oFile In oFolder.files
If DateDiff("d", oFile.DateCreated,Now) > 30 Then
oFile.Delete True
End If
Next

End If
i = i + 1
Loop

Set oFolder = Nothing
Set fso = Nothing

----------------------------------------------------------

Here is a portion of the text file (Test-Del-Spam.txt) with some of the paths that need to be assigned to path1:

d:\MDaemon\Users\1stmetro.net\aadams\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aaduku\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aagtarap\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aaguilera\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aallen\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aalva-laupsa\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aalvalaupsa\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aammons\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\aaustin\Spam.IMAP\
d:\MDaemon\Users\1stmetro.net\abacares\Spam.IMAP\

I really would like some help with correcting my VBScript.

Thanks, Dan
 
[tt]
'Path1 = (path from Test_Del_Spam.txt)'

Dim fso
Dim oFolder
Dim oFile

Const ForReading = 1

Set fso = createobject("Scripting.FileSystemObject")
'Set oFolder = fso.GetFolder(Path1)
Set objTextFile = fso_OpenTextFile("d:\download\" _
& "Test_Del_Spam.txt", ForReading)

Do While objTextFile.AtEndOfStream <> True
'If inStr(objtextFile.Readline, "") Then
[blue]If trim(objtextFile.Readline)<>"" Then
Set oFolder = fso.GetFolder(trim(objtextFile.Readline))[/blue]

For Each oFile In oFolder.files
If DateDiff("d", oFile.DateCreated,Now) > 30 Then
oFile.Delete True
End If
Next

End If
'i = i + 1
Loop

[blue]objtextFile.close
Set objtextFile = Nothing[/blue]
Set oFolder = Nothing
Set fso = Nothing
[/tt]
 
Tsuji,

You are so kind to help me with the VBScript. Thanks for the revisions.

When I run the script, I get the following Windows Script Host error message:

Script: C:\Download\1MM-PurgeOldSpam.vbs
Line: 9
Char: 1
Error: Incorrect function
Code: 80070001
Source (null)


Here is the 1MM-PurgeOldSpam.vbs script:

Dim fso
Dim oFolder
Dim oFile

Const ForReading = 1

Set fso = createobject("Scripting.FileSystemObject")

Set objTextFile = fso_OpenTextFile("d:\download\" _
& "Test_Del_Spam.txt", ForReading)

Do While objTextFile.AtEndOfStream <> True

If trim(objtextFile.Readline)<>"" Then
Set oFolder = fso.GetFolder(trim(objtextFile.Readline))

For Each oFile In oFolder.files
If DateDiff("d", oFile.DateCreated,Now) > 30 Then
oFile.Delete True
End If
Next

End If

Loop

objtextFile.close
Set objtextFile = Nothing
Set oFolder = Nothing
Set fso = Nothing

-------------------------------------------------------

Any suggestions?

Thanks,

Dan
 
Tsuji,

Getting closer, I had Test_Del_Spam.txt instead of Test-Del-Spam.txt for the objTextFile.

I now get an error on Line: 14, Char: 7, Error: Path not found, Code: 800A004C, Source: Microsoft VBScript runtime error.

Here is the current revision that needs revised:

Dim fso
Dim oFolder
Dim oFile

Const ForReading = 1

Set fso = createobject("Scripting.FileSystemObject")

Set objTextFile = fso_OpenTextFile("c:\download\Test-Del-Spam.txt", ForReading)

Do While objTextFile.AtEndOfStream <> True

If trim(objtextFile.Readline)<>"" Then
Set oFolder = fso.GetFolder(trim(objtextFile.Readline))

For Each oFile In oFolder.files
If DateDiff("d", oFile.DateCreated,Now) > 300 Then
oFile.Delete True
End If
Next

End If

Loop

objtextFile.close
Set objtextFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
 
...
Dim Path1
Do While objTextFile.AtEndOfStream <> True
Path1 = Trim(objtextFile.Readline)
If Path1 <> "" Then
Set oFolder = fso.GetFolder(Path1)
For Each oFile In oFolder.files
If DateDiff("d", oFile.DateCreated,Now) > 300 Then
oFile.Delete True
End If
Next
End If
Loop
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes, as said.
[tt]
Path1 = Trim(objtextFile.Readline)
If Path1 <> "" Then
Set oFolder = fso.GetFolder(Path1)
[/tt]
Every call to .readline makes the pointer to the next. Hence, my revision jump away one line from the target.
 
Thanks, I will retest the script tomorrow and let you know the results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top