• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBS Help

Tripp

Member
Hello,

Can anyone point me in the right direction/ recommend where to get some help with a small vbs script?

I realize this probably isn't the place but any help on where to find some help would be appreciated.

Regards,

Tripp
 
Tripp

Why not simply post what you are trying to achieve and what you have done so far here?

See what responses you get ?
 
Sure,

I've been trying to get this code to move files containing specific text into another folder but I keep getting an error on the second line saying "Expecting '='" cant figure out why.

There is a sub folder called Test and a few blank .txt documents called test 1, test 2 etc for testing purposes obviously..

Code:
Sub LoopFolder()
    Const SourceFolder As String = "C:\Users\TRIPP\Documents\VBS"
    Dim oFSO
    Dim oFolder As Object
    Dim oFile As Object
    Dim NewFolder As String
   
    Set oFSO = CreateObject("Scripting.FileSystemObject")
   
    Set oFolder = oFSO.GetFolder(SourceFolder)
   
    For Each oFile In oFolder.Files
       
        If oFile.Type Like "*Text Document*" Then
            Select Case True
            Case oFile Like "*Test*"
                NewFolder = "Test\"
            'Case oFile Like "*Lite*"
                'NewFolder = "Lite\"
                'etc
            End Select
            Name oFile.Path As SourceFolder & NewFolder & oFile.Name
        End If
    Next oFile
   
    Set oFolder = Nothing
    Set oFSO = Nothing
   
End Sub

Im very new to vbs so any pointers would be great.

Regards,

Tripp
 
Can't say for sure, but shouldn't Case oFile... line be

Code:
Case oFile.Name Like "*Test*"

Since "Like" operator is for string comparison and you are trying to perform it on Object.
 
Oh and const line should be.
Code:
Const SourceFolder = "YourPath"

Basically in VBS string or number nested in " " is considered string literal

Edit: Oh and you can't dim variable "As Type" in VBS. Only use "Dim oFSO" etc.

Your code should look like...
Code:
Sub LoopFolder()
    Const SourceFolder = "C:\Test\"
    Dim oFSO
    Dim oFolder
    Dim oFile
    Dim NewFolder

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    Set oFolder = oFSO.GetFolder(SourceFolder)

    For Each oFile In oFolder.Files
    
        If oFile.Type Like "*Text Document*" Then
            Select Case True
            Case oFile.Name Like "*Test*"
                NewFolder = "Test\"
            'Case oFile Like "*Lite*"
              'NewFolder = "Lite\"
              'etc
            End Select
            Name oFile.Path, SourceFolder & NewFolder & oFile.Name
        End If
    Next

    Set oFolder = Nothing
    Set oFSO = Nothing
End Sub

Note: Not tested. You may want to change "Name" to "MoveFile".
 
Last edited:
Back
Top