Solved

vb script - would like it to output results in txt file

Posted on 2009-04-03
8
502 Views
Last Modified: 2012-05-06
Hi
I have the following vb created for me.
Looks in a folder and deletes all occurences of files with extension older than 3 days.
All works fine.
Im calling the script via a batch file in windows scheduler to run nightly.

What id like to do is capture more results of the run of this.

Preferably like to:
Capture the files that were deleted if possible in a text file that is overwritten to every nght when the job runs.

If this isnt possible - then just to capture the run and how many files were deleted ?

Id like the first option if possible

Can anyone help ? I know nothing about vb scripting.

thanks

Mo
On Error Resume Next

 

strRoot = "D:\Backups\"

intDays = 3

arrExtensions = Array("bak", "trn","txt")

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objRoot = objFSO.GetFolder(strRoot)
 

For Each objFile in objRoot.Files

    strFileExt = objFSO.GetExtensionName(objFile.Path)

  

    For Each strExt in arrExtensions

        

	If LCase(strFileExt) = LCase(strExt) Then 

	  msgbox "bollox"

            If DateDiff("d", objFile.DateLastModified, Now) > intDays Then

		WScript.Echo objFile.Path

                objFile.Delete True

            End If

        End If

    Next

Next

 

For Each objFolder in objRoot.SubFolders

    GetSubFolders objFolder.Path

Next

 

Sub GetSubFolders(strFolderPath)

    Set objSub = objFSO.GetFolder(strFolderPath)

    

    For Each objFile2 in objSub.Files

        strFileExt = objFSO.GetExtensionName(objFile2.Path)

    

        For Each strExt in arrExtensions

            If LCase(strFileExt) = LCase(strExt) Then

                If DateDiff("d", objFile2.DateLastModified, Now) > intDays Then

                    WScript.Echo objFile2.Path 

			

                    objFile2.Delete True

                End If

            End If

        Next

    Next

        

    For Each objFolder2 in objSub.SubFolders

        GetSubFolders objFolder2.Path

    Next

End Sub

Open in new window

0
Comment
Question by:mooriginal
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:aehare70
ID: 24061124
mooriginal,
This can be handled several ways.  The most simple and crude way would be to put a line in your .bat file that does something like this;

dir c:\example > c:\log.txt    --- look at the command DIR /? for options you wish to use.

The other requires read/writes through-out the vbscript in all the areas that involve the files.  if this is the case, a few questions need to be answered.  What would you like to see in this log, and what's the retention on this log?  Are you planning on appending to the log or write a new log each day, leaving the other alone?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24061425
Here you go...
Const ReportFile = "D:\FileDeletion.txt"
 

On Error Resume Next

 

strRoot = "D:\Backups\"

intDays = 3

arrExtensions = Array("bak", "trn","txt")

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objRoot = objFSO.GetFolder(strRoot)

Set objOut = objFSO.CreateTextFile(ReportFile)
 

For Each objFile in objRoot.Files

    strFileExt = objFSO.GetExtensionName(objFile.Path)

  

    For Each strExt in arrExtensions

        

        If LCase(strFileExt) = LCase(strExt) Then 

          msgbox "bollox"

            If DateDiff("d", objFile.DateLastModified, Now) > intDays Then

                WScript.Echo objFile.Path

                objFile.Delete True

                If Err.Number <> 0 Then

                  objOut.WriteLine "COULD NOT DELETE " & objFile.Name & " " & Err.Description

                Else

                  objOut.WriteLine objFile & " successfully deleted."

                End If

            End If

        End If

    Next

Next

 

For Each objFolder in objRoot.SubFolders

    GetSubFolders objFolder.Path

Next
 

objOut.Close

 

Sub GetSubFolders(strFolderPath)

    Set objSub = objFSO.GetFolder(strFolderPath)

    

    For Each objFile2 in objSub.Files

        strFileExt = objFSO.GetExtensionName(objFile2.Path)

    

        For Each strExt in arrExtensions

            If LCase(strFileExt) = LCase(strExt) Then

                If DateDiff("d", objFile2.DateLastModified, Now) > intDays Then

                    WScript.Echo objFile2.Path 

                    objFile2.Delete True

                    If Err.Number <> 0 Then

                      objOut.WriteLine "COULD NOT DELETE " & objFile2.Name & " " & Err.Description

                    Else

                      objOut.WriteLine objFile2 & " successfully deleted."

                    End If

                End If

            End If

        Next

    Next

        

    For Each objFolder2 in objSub.SubFolders

        GetSubFolders objFolder2.Path

    Next

End Sub

Open in new window

0
 

Author Comment

by:mooriginal
ID: 24061612
tried this and it doesnt work

it creates a file but didnt put anything in it..when I ran it with test files
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24061862
did they get deleted?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mooriginal
ID: 24065159
hi guys
this script works to delete files so thats not the issue.

want i want is not to change it functionality

just improve it if possible.

to answer questions
id like a txt file that is overwritten every time the script is called - once a night.
so not appending just overwritting -

if possible id like to do a count of total number of files that the script deletes.
outputted via the vbs to a txt file to show this.

0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24069904
here it is
Const ReportFile = "D:\FileDeletion.txt"

 

On Error Resume Next

 

strRoot = "D:\Backups\"

intDays = 3

arrExtensions = Array("bak", "trn","txt")

iCount = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objRoot = objFSO.GetFolder(strRoot)

Set objOut = objFSO.CreateTextFile(ReportFile)

 

For Each objFile in objRoot.Files

    strFileExt = objFSO.GetExtensionName(objFile.Path)

  

    For Each strExt in arrExtensions

        

        If LCase(strFileExt) = LCase(strExt) Then 

          msgbox "bollox"

            If DateDiff("d", objFile.DateLastModified, Now) > intDays Then

                WScript.Echo objFile.Path

                objFile.Delete True

                If Err.Number <> 0 Then

                  objOut.WriteLine "COULD NOT DELETE " & objFile.Name & " " & Err.Description

                Else

                  iCount = iCount + 1

                  objOut.WriteLine objFile & " successfully deleted."

                End If

            End If

        End If

    Next

Next

 

For Each objFolder in objRoot.SubFolders

    GetSubFolders objFolder.Path

Next

 

objOut.WriteLine

objOut.WriteLine "Total file(s) deleted: " & iCount

objOut.Close

 

Sub GetSubFolders(strFolderPath)

    Set objSub = objFSO.GetFolder(strFolderPath)

    

    For Each objFile2 in objSub.Files

        strFileExt = objFSO.GetExtensionName(objFile2.Path)

    

        For Each strExt in arrExtensions

            If LCase(strFileExt) = LCase(strExt) Then

                If DateDiff("d", objFile2.DateLastModified, Now) > intDays Then

                    WScript.Echo objFile2.Path 

                    objFile2.Delete True

                    If Err.Number <> 0 Then

                      objOut.WriteLine "COULD NOT DELETE " & objFile2.Name & " " & Err.Description

                    Else

                      iCount = iCount + 1

                      objOut.WriteLine objFile2 & " successfully deleted."

                    End If

                End If

            End If

        Next

    Next

        

    For Each objFolder2 in objSub.SubFolders

        GetSubFolders objFolder2.Path

    Next

End Sub

Open in new window

0
 

Author Comment

by:mooriginal
ID: 24075503
great cheers that worked !
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24076227
Glad I could help - thanx for the grade! :^)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now