Solved

convert vba to vb script

Posted on 2013-01-03
2
352 Views
Last Modified: 2013-01-17
I have the following VBA code that works fine. How do I convert it to run in VB script? I understand Dir is not supported in VB script. Is there a substitute?  

Basically, I am looking for Microsoft Word documents in all subfolders on all connected network drives that have  the phrases "Progress Report" or "Consultation Report" in their name.  If there are two files with matching names except one file has "Signed" appended to it, delete the file without "Signed".  Also delete any files that start with "~$".

TIA
Sub ClearAllUneededFiles()
Dim fs

Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists("\\Jane-pc\jane's files\Patent Records\FindUnsignedDocuments.doc") Then
    ClearUneededFiles "\\Jane-pc\jane's files\Patent Records\"
    ClearUneededFiles "\\Jane-pc\jane's files\Patent Records\_NOT SEEN\"
End If
If fs.FileExists("\\FUJITU\My Documents\JANE'S FILES\Patent Records\FindUnsignedDocuments.doc") Then
    ClearUneededFiles "\\FUJITU\My Documents\JANE'S FILES\Patent Records\"
    ClearUneededFiles "\\FUJITU\My Documents\JANE'S FILES\Patent Records\_NOT SEEN\"
End If
If fs.FileExists("\\DESKTOP\My Documents\Jane's files\Patent Records\FindUnsignedDocuments.doc") Then
    ClearUneededFiles "\\DESKTOP\My Documents\Jane's files\Patent Records\"
    ClearUneededFiles "\\DESKTOP\My Documents\Jane's files\Patent Records\_NOT SEEN\"
End If
ActiveDocument.SaveAs Environ("temp") & "\temp.doc"
Application.Quit wdPromptToSaveChanges
End Sub


Sub ClearUneededFiles(mypath)
Dim DirList()
Dim counter
Dim MyName
Dim fs

Set fs = CreateObject("Scripting.FileSystemObject")

counter = 0
ReDim DirList(1)

On Error Resume Next
MyName = Dir(mypath, vbDirectory)
If Err = 53 Then
    Exit Sub
End If
On Error GoTo 0

Do While MyName <> ""   ' Start the loop to find subfolders
    If (GetAttr(mypath & MyName) And vbDirectory) = vbDirectory Then
        If MyName <> "." And MyName <> ".." And InStr(1, MyName, "_") = 0 Then
            ReDim Preserve DirList(counter)
            DirList(counter) = mypath & MyName
            counter = counter + 1
        End If
    End If
    MyName = Dir    ' Get next entry.
Loop
For counter = 0 To UBound(DirList)
    MyName = Dir(DirList(counter) & "\*.doc")

    Do While MyName <> ""   ' Start the loop to find files in each subfolder
        If InStr(1, MyName, "Consultation Report") > 0 Or InStr(1, MyName, "Progress Report") > 0 Then
            If InStr(1, MyName, "Signed") = 0 Then
                If fs.FileExists(DirList(counter) & "\" & Left(MyName, Len(MyName) - 4) & " Signed.doc") Then
                    Kill DirList(counter) & "\" & MyName
                ElseIf Left(MyName, 2) = "~$" Then
                    Kill DirList(counter) & "\" & MyName
                End If
            End If
        End If
    MyName = Dir    ' Get next entry.
    Loop
Next
End Sub

Open in new window

0
Comment
Question by:thenelson
2 Comments
 
LVL 12

Accepted Solution

by:
HugoHiasl earned 500 total points
Comment Utility
You are already using a FileSystemObject.

You could use the methods of this object:

msdn.microsoft.com/en-us/library/aa262402(v=vs.60).aspx
0
 
LVL 39

Author Closing Comment

by:thenelson
Comment Utility
thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

16 Experts available now in Live!

Get 1:1 Help Now