Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

foreach - Prevent VBScript Folder Loop From Looping Through Newly Created Folders

I have a simple loop that goes through all the folders in a directory and if a certain condition is true, it creates a copy of the folder with a new foldername.

My problem is that after a new folder is created it gets processed later in the loop and I don't want that.

The loop is checking folder names and if it is 7, 6 or 5 characters long, do a certain action. A 7 length folder gets renamed to a 6 digit folder, but later in the loop that new 6 digit folder is getting included and further processed. I do not want this.

Thanks

question from:https://stackoverflow.com/questions/65863425/prevent-vbscript-folder-loop-from-looping-through-newly-created-folders

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ended up using a Dictionary object to save the original folder list and then check if the looped folder existed on that list before proceeding.

Set FSO = CreateObject("Scripting.FileSystemObject")
Set startingSubFoldColl = CreateObject("Scripting.Dicitonary")

Set Folder = FSO.GetFolder(*targetFolderPath*)

For Each Subfolder in Folder.Subfolders
startingSubFoldColl.add Cstr(Subfolder),Cstr(Subfolder)
Next

For Each SubFolder in Folder.Subfolders
If startingSubFoldColl.Exists(Cstr(Subfolder)) Then
'Do Stuff
End If
Next

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...