Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Script going too deep into subfolders, need help!

Reply
 
Old 05-07-2009   #1 (permalink)
Rich


 
 

Script going too deep into subfolders, need help!

OK, I found the following script, and it does exactly what I want it to do,
except it creates the subfolder SaveHere in every subfolder. What I want it
to do, is only create a SaveHere folder inside every folder one level deeper
than c:\temp. Like in C:\temp\1, C:\temp\2 i want it to create a SaveHere
folder, but not in C:\temp\1\a or c:\temp\1\a\z. I hope that makes sense.
Can someone help me out? I'm not very good at the whole recursive stuff yet.




Dim arrNewFolders
arrNewFolders=Array(_
"SaveHere")
newfolderpath = "C:\TEMP"
mysub(newfolderpath)
'**********start of recursive Sub routine *************
Sub mysub(path)
Dim filesys, newfolderpath, RootFolder, FolderColl, SubFolder
wscript.echo "Processing " & path
Set filesys=CreateObject("Scripting.FileSystemObject")
Set RootFolder = filesys.GetFolder(path)
' Process subfolders
if RootFolder.SubFolders.Count>0 then
For Each SubFolder In RootFolder.SubFolders
mysub SubFolder.Path
next
end if

for each strFolder in arrNewFolders
If Not filesys.FolderExists(path & "\" & strFolder) Then
filesys.CreateFolder(path & "\" & strFolder)
WScript.Echo ("A new folder has been created at: " & path & "\" &
strFolder)
End If
next
End Sub


My System SpecsSystem Spec
Old 05-07-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Script going too deep into subfolders, need help!


"Rich" <Rich@xxxxxx> wrote in message
news:6F1E60CF-C642-4587-A159-0E9A7AF18960@xxxxxx
Quote:

> OK, I found the following script, and it does exactly what I want it to
> do,
> except it creates the subfolder SaveHere in every subfolder. What I want
> it
> to do, is only create a SaveHere folder inside every folder one level
> deeper
> than c:\temp. Like in C:\temp\1, C:\temp\2 i want it to create a SaveHere
> folder, but not in C:\temp\1\a or c:\temp\1\a\z. I hope that makes
> sense.
> Can someone help me out? I'm not very good at the whole recursive stuff
> yet.
>
>
>
>
> Dim arrNewFolders
> arrNewFolders=Array(_
> "SaveHere")
> newfolderpath = "C:\TEMP"
> mysub(newfolderpath)
> '**********start of recursive Sub routine *************
> Sub mysub(path)
> Dim filesys, newfolderpath, RootFolder, FolderColl, SubFolder
> wscript.echo "Processing " & path
> Set filesys=CreateObject("Scripting.FileSystemObject")
> Set RootFolder = filesys.GetFolder(path)
> ' Process subfolders
> if RootFolder.SubFolders.Count>0 then
> For Each SubFolder In RootFolder.SubFolders
> mysub SubFolder.Path
> next
> end if
>
> for each strFolder in arrNewFolders
> If Not filesys.FolderExists(path & "\" & strFolder) Then
> filesys.CreateFolder(path & "\" & strFolder)
> WScript.Echo ("A new folder has been created at: " & path & "\" &
> strFolder)
> End If
> next
> End Sub
>
One solution (watch for line wrapping):
=======
Sub mysub(path)
Dim filesys, newfolderpath, RootFolder, FolderColl, SubFolder
wscript.echo "Processing " & path
Set filesys=CreateObject("Scripting.FileSystemObject")
Set RootFolder = filesys.GetFolder(path)
' Process subfolders
if RootFolder.SubFolders.Count>0 then
For Each SubFolder In RootFolder.SubFolders
for each strFolder in arrNewFolders
If Not filesys.FolderExists(SubFolder.Path & "\" &
strFolder) Then
filesys.CreateFolder(SubFolder.Path & "\" & strFolder)
WScript.Echo ("A new folder has been created at: " _
& SubFolder.Path & "\" & strFolder)
End If
next
next
end if
End Sub

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 05-07-2009   #3 (permalink)
Pegasus [MVP]


 
 

Re: Script going too deep into subfolders, need help!


"Rich" <Rich@xxxxxx> wrote in message
news:6F1E60CF-C642-4587-A159-0E9A7AF18960@xxxxxx
Quote:

> OK, I found the following script, and it does exactly what I want it to
> do,
> except it creates the subfolder SaveHere in every subfolder. What I want
> it
> to do, is only create a SaveHere folder inside every folder one level
> deeper
> than c:\temp. Like in C:\temp\1, C:\temp\2 i want it to create a SaveHere
> folder, but not in C:\temp\1\a or c:\temp\1\a\z. I hope that makes
> sense.
> Can someone help me out? I'm not very good at the whole recursive stuff
> yet.
There are a couple of problems with your approach. The main one is that
copying some script from somewhere without attempting to understand what it
does will rarely give you the desired result. This leads directly to your
second problem: You do *not* want a recursive routine, you simple want a
routine that enumerates all subfolders of the specified folder, then creates
a sub-sub-folder in each of them. Recursive routines will do exactly what
your script does: They go as deep as they can.

There is a third and minor issue: The name of the folder you're trying to
create is a string, not an array.

Try this approach:

sNewFolder = "\SaveHere"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\TEMP")
For Each oSubfolder In oFolder.SubFolders
WScript.Echo "Creating " & oSubfolder.Path & sNewFolder
' oFSO.CreateFolder oSubfolder.Path & sNewFolder
Next

Uncomment the second line from the bottom to activate the script. Note that
you must add your own code to check if the folder to be created does not
already exist.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Script to remove space in all files in all subfolders VB Script
Please help Im in a deep **** with Realtek Sound & Audio
Unexpected reboot after deep sleep Vista General
How to recover from deep sleep? Vista General
Re: World of Warcraft random deep freezes Vista Games


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46