Re: vbscript to traverse subfolders



On Mar 13, 10:26 am, Tom Lavedas <tglba...@xxxxxxx> wrote:
On Mar 13, 9:40 am, tiv <jmatt...@xxxxxxxxx> wrote:



On Mar 12, 6:22 pm, "Pegasus \(MVP\)" <I....@xxxxxxxxxx> wrote:

"tiv" <jmatt...@xxxxxxxxx> wrote in message

news:822d8659-5955-42f5-beb4-6980982ac723@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On Mar 12, 3:34 pm, "Pegasus \(MVP\)" <I....@xxxxxxxxxx> wrote:
"tiv" <jmatt...@xxxxxxxxx> wrote in message

news:0d234151-d169-4b27-b64d-d0a0d5d9dd2a@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Hey All....

{snip}
YES!!! That does exactly what i want for looking through the
subfolders. Thing is, there is only going to be one file per unique
name in the entire folder tree. So is there any way that if it finds
and copies the file to the destination = true, that it moves on to the
next file in the lookup txt file? I've tested and after it finds and
copies the file, it continues to look through the subsequent folders
for the same file. I'm not sure, but would it need an IF statement
and return in the event if the COPY statement is true? Actually after
reading this it still sounds unclear....I want it to copy File1 from
the lookup txt file - "sfile", return to the root of the folders
"source" and move to the next file "File2" in the lookup txt - "sfile"
and so on and so on. Being that i need to find about 1500 files in
"sfile"....

THANK YOU so much again! This is invaluable!! (And the permissions
to work great as well!!) :)

I believe the following modifications would do the job (but I didn't
take the time to test it) ...

Function FindFile(S, F) ' now a function - returns True if found
FindFile = False
FN = S & "\" & F
WScript.Echo "Searching " & S & " for " & F
If objFSO.FileExists(FN) Then
' intRunError = objShell.Run("%COMSPEC% /c Echo Y| fileacl " _
' & FN & " /o administrators", 2, True)
' If intRunError <> 0 Then
' WScript.Echo "Error assigning permissions for user " _
' & strUser & " to file " & FN
' End If
objFSO.copyfile FN, dest & "\" & F, True
FindFile = True ' return True when found
exit sub ' stop searching
Else
' --------------------------------------------------------
' I don't think this is the right place for this
' I believe it puts a log entry for every folder searched
' that fails to have the file in it.
'
objErrLog.WriteLine FN _
& " - Error: File Does Not Exist! - " & Now
End If
' --------------------------------------------------------

Set objFolder = objFSO.GetFolder(S)
Set colFolders = objFolder.SubFolders
For Each sFolder In colFolders
FindFile sFolder, Filename
if FindFile then exit next ' ensure exit from recursion
Next
End Sub

As I said, this is not tested (just don't have the time). Recursive
routines can be tricky and time consuming to test. Hopefully, I got
it right.

Tom Lavedas
===========http://members.cox.net/tglbatch/wsh/

This is what i have now....it works, but continues to traverse the
subfolders after finding/copying the file.

Function FindFile(S, F) ' now a function - returns True if found
FindFile = False
FN = S & "\" & F
WScript.Echo "Searching " & S & " for " & F
If objFSO.FileExists(FN) Then
intRunError = objShell.Run("%COMSPEC% /c Echo Y| fileacl " _
& FN & " /o administrators", 2, True)
If intRunError <> 0 Then
WScript.Echo "Error assigning permissions for user " _
& strUser & " to file " & FN
End If
objFSO.copyfile FN, dest & "\" & F, True
if FindFile = True then
exit Function ' ensure exit from recursion

end if

Else

Set objFolder = objFSO.GetFolder(S)
Set colFolders = objFolder.SubFolders
For Each sFolder In colFolders
FindFile sFolder, Filename
if FindFile = True then exit Function ' ensure exit from recursion
Next
end if
End Function
.



Relevant Pages

  • Re: vbscript to traverse subfolders
    ... FindFile = True ' return True when found ... ' I believe it puts a log entry for every folder searched ... FindFile sFolder, Filename ... if FindFile then exit next ' ensure exit from recursion ...
    (microsoft.public.scripting.vbscript)
  • Re: vbscript to traverse subfolders
    ... FindFile = True ' return True when found ... ' I believe it puts a log entry for every folder searched ... FindFile sFolder, Filename ... if FindFile then exit next ' ensure exit from recursion ...
    (microsoft.public.scripting.vbscript)
  • Re: vbscript to traverse subfolders
    ... FindFile = True ' return True when found ... ' I believe it puts a log entry for every folder searched ... FindFile sFolder, Filename ... if FindFile then exit next ' ensure exit from recursion ...
    (microsoft.public.scripting.vbscript)
  • Re: vbscript to traverse subfolders
    ... FindFile = True ' return True when found ... ' I believe it puts a log entry for every folder searched ... if FindFile then exit next ' ensure exit from recursion ...
    (microsoft.public.scripting.vbscript)
  • Re: vbscript to traverse subfolders
    ... FindFile = True ' return True when found ... FindFile sFolder, Filename ... if FindFile then exit next ' ensure exit from recursion ... subfolders after finding/copying the file. ...
    (microsoft.public.scripting.vbscript)

Loading