2

I am trying to write a VB script (having never attempted before) - I need it to search the folder'\file001\source$' - whilst in the folder search for all 'Update.exe'files - If this is done manually, in Windows it takes a long long time! I would like all the files that it finds with this name - to be copied into a new folder.

Looking at various help forums I am getting more and more confused.

Below is what I have attempted:

Set fso = CreateObject("Scripting.FileSystemObject")
ShowSubfolders fso.GetFolder("\\file001\source$")

'foldername = "\file001\source$" 'filename = "Updater.exe"

Function ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        ShowSubFolders Subfolder
    Next
End Function

This is to search through a folder, recursively through the folders sub folders to find all files with this name.

I have also done research into -directory.getfiles. But have no clue if this is the right direction.

As a newbie to VB script, I have researched and attempted to play around with vb script, to get the function I desire. I would be grateful to any help I can get.

Again - my target is to - find all files within the given folder and subfolders with the name update.exe - and then to copy these files found into a new folder. Thank you in advance.

Kiwi
  • 143
  • 2
  • 5
  • 12
  • 1
    http://stackoverflow.com/questions/7161739/vbs-how-can-i-check-if-a-file-exists – Guy P Apr 17 '13 at 12:34
  • A little confused at this link. This is solving a different problem? I do not want to open the file or take the first line – Kiwi Apr 17 '13 at 12:39

4 Answers4

3

If you only want to check the content of a single folder for the existence of a particular file you can do that like this:

Set fso = CreateObject("Scripting.FileSystemObject")

foldername = "\\file001\source$"
filename   = "Update.exe"

If fso.FileExists(fso.BuildPath(foldername, filename)) Then
  WScript.Echo filename & " exists."
End If

If you want to check the subfolders of foldername as well, you need to recurse into subfolders with something like this. You can either integrate the check from the above code sample in the loop over the subfolders, or add another loop over the files in the folder:

Set fso = CreateObject("Scripting.FileSystemObject")

CopyUpdater fso.GetFolder("\\file001\source$")

Sub CopyUpdater(fldr)
  For Each f In fldr.Files
    If LCase(f.Name) = "update.exe" Then
      'copy file to somewhere else
    End If
  Next

  For Each sf In fldr.SubFolders
    CopyUpdater sf
  Next
End Sub
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you for assistance with searching for a single file - I have posted my update above - I am struggling with check subfolders additionally. – Kiwi Apr 22 '13 at 13:30
1

See my question here, i benchmark three languages (vbscript also) which do a subdirectory traversal with full working samples and optimised for the language. benchmarks: does python have a faster way of walking a network folder?

Community
  • 1
  • 1
peter
  • 41,770
  • 5
  • 64
  • 108
0

Thats a good attempt . Read more on below link and understand things better .

Community
  • 1
  • 1
Pavan G jakati
  • 115
  • 1
  • 7
-1
dim sFilename

Dim objDict
Set objDict=CreateObject("Scripting.Dictionary")
sFilename = ""

'root folder path where subfolder exists 
fileLocation="C:\Users\u258251\Desktop\TestSubfolder"

Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Add all files with specific extention to dictonary 

Call Recurse(fileLocation)



ItemArray = objDict.Items


'Loop through dictonary
For i = 0 To objDict.count -1
    sFilename = sFilename & ItemArray(i) & VBCRLF
Next 

msgbox(sFilename)

'find a specific file by name and return path 
if objDict.Exists("DP103.txt") then 
    msgbox(objDict.Item("DP103.txt")) 
end if

Sub Recurse(strFolderPath)
    Dim objFolder
    Set objFolder = objFSO.GetFolder(strFolderPath)
    Dim objFile
    Dim objSubFolder

    For Each objFile In objFolder.Files
        If (InStr(objFile.Name, ".") > 0) Then

            'proceed if extention is .txt 
            If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then 
                if objDict.Exists(objFile.Name)=false then 
                'add files and path to dictonary 
                objDict.Add objFile.Name,objfile.Path 
                End if 
            End if 
        End If 
    Next

    For Each objSubFolder In objFolder.SubFolders
        Call Recurse(objSubFolder.Path)
    Next
End Sub