donnie91910
asked on
Code will not search subfolders.
I have written search code to find files on my computer. The problem that I am having is that the code will not search in subfolders. Is there a way to make my code do that? Here is the code:
Dim objFSO
Dim sSourceFolder
Dim sDestFolder
Dim sDBFile
Dim sDateTimeStamp
Set objFSO = CreateObject("Scripting.FileSystemObject")
sSourceFolder = "C:\Oceanside\"
sBackupFolder = "C:\Files_Found\"
sDBFile = array _
("ROWP19-0499","ROWP19-0543","ROWP19-0544","ROWP19-0545","ROWP19-0550","ROWP19-0559","ROWP19-0562","ROWP19-0567","ROWP19-0645")
sDBFileExt = "pdf"
'If the backup folder doesn't exist, create it.
If Not objFSO.FolderExists(sBackupFolder) Then
objFSO.CreateFolder(sBackupFolder)
End If
'Copy the file as long as the file can be found
For i = 0 to Ubound(sDBFile)
If objFSO.FileExists(sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt) Then
objFSO.CopyFile sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt,_
sBackupFolder & "\" & sDBFile(i) & "." & sDBFileExt
MsgBox sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt & " was copied to " & sBackupFolder
End if
Next
Set objFSO = Nothing
Thanks in advance!
Might I suggest you move to PowerShell instead of VB Script? VB Script is basically dead; anything that gets scripted these days will happen in PS.
It is so much more powerful (and easier to learn and manage) than VB Script that it's not even funny anymore, you can test every command interactively in the console, and help is just a Get-Help away ...
It is so much more powerful (and easier to learn and manage) than VB Script that it's not even funny anymore, you can test every command interactively in the console, and help is just a Get-Help away ...
$SourceFolder = "C:\Oceanside"
$BackupFolder = "C:\Files_Found"
$DBFile = "ROWP19-0499", "ROWP19-0543", "ROWP19-0544", "ROWP19-0545", "ROWP19-0550", "ROWP19-0559", "ROWP19-0562", "ROWP19-0567", "ROWP19-0645"
$DBFileExt = "pdf"
If (-not (Test-Path -Path $BackupFolder)) {
New-Item -ItemType Directory -Path $BackupFolder | Out-Null
}
$copied = Get-ChildItem -Path $SourceFolder -Recurse -File -Filter $DBFileExt |
Where-Object {$DBFile -contains $_.BaseName} |
Copy-Item -Destination $BackupFolder -PassThru -Verbose
Add-Type -AssemblyName System.Windows.Forms
If ($copied) {
$message = "Copied the following files to '$($BackupFolder)':`r`n$($copied.FullName -join "`r`n")"
} Else {
$message = "No files were found!"
}
[void][System.Windows.Forms.Messagebox]::Show($message)
If you want to stay with VBS, here's an approach to drill down into subfolders.
»bp
Dim objFSO
Dim sSourceFolder
Dim sDestFolder
Dim sDBFile
Dim sDateTimeStamp
Set objFSO = CreateObject("Scripting.FileSystemObject")
sSourceFolder = "C:\Oceanside"
sBackupFolder = "C:\Files_Found"
sDBFile = array("ROWP19-0499","ROWP19-0543","ROWP19-0544","ROWP19-0545","ROWP19-0550","ROWP19-0559","ROWP19-0562","ROWP19-0567","ROWP19-0645")
sDBFileExt = "pdf"
'If the source folder doesn't exist, exit.
If Not objFSO.FolderExists(sSourceFolder) Then
MsgBox "Source folder """ & sSourceFolder & """does not exist."
WScript.Quit()
End If
'If the backup folder doesn't exist, create it.
If Not objFSO.FolderExists(sBackupFolder) Then
objFSO.CreateFolder(sBackupFolder)
End If
'Copy the file as long as the file can be found
ProcessFolder objFSO.GetFolder(sSourceFolder)
Set objFSO = Nothing
Sub ProcessFolder(oFolder)
For i = 0 to Ubound(sDBFile)
If objFSO.FileExists(oFolder.Path & "\" & sDBFile(i) & "." & sDBFileExt) Then
objFSO.CopyFile oFolder.Path & "\" & sDBFile(i) & "." & sDBFileExt, sBackupFolder & "\" & sDBFile(i) & "." & sDBFileExt
MsgBox sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt & " was copied to " & sBackupFolder
End if
Next
For Each oSubFolder In oFolder.SubFolders
ProcessFolder oSubFolder
Next
End Sub
»bp
ASKER
All,
I stuck with the VB Script method for the moment and it worked very well. I will research the PowerShell method. Can someone send me a excellent tutorial for PowerShell beginners so I can get my self versed in this programming language.
Thanks in advance.
I stuck with the VB Script method for the moment and it worked very well. I will research the PowerShell method. Can someone send me a excellent tutorial for PowerShell beginners so I can get my self versed in this programming language.
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i want to thank everyone for their solutions and participation.
I’ll check back in the morning...
~bp