strange issue with vbscript

PowerYoga

Diamond Member
Nov 6, 2001
4,603
0
0
A client of mine has a bunch of folders and files that need renaming, so I wrote a quick VBS to iterate through subfolders and move the contents to the correct places as well as rename the folder. To do this, I'm primarily using the objFSO.moveFile oldname, newname command.

Now the problem is this thing works fine on my computer with a few tiny files and a few folders. My client has thousands of these folders and when he runs the script the program will cease up after a few hundred of these folders. The number this stops on varies from machine to machine, so it looks like it's a resource issue that's causing the script to break down. I realize there's no "rename" function in VBS and it renames stuff by calling a move command. Is there some sort of memory leak or some big no-no with this procedure?

Code:
sub movefiles(byVal strFolderPath, byVal newpath, byVal basepath)
	Dim objCurrentFolder, colSubfolders, name
	dim path
	dim oldname
	dim newname
	dim lastpath
	
	'new counter
	dim multipleFiles
	dim counter
	
	'verification
	If objFSO.FolderExists(strFolderPath) Then
		'set necessary current variable things
		Set objCurrentFolder = objFSO.GetFolder(strFolderPath)
		Set colSubfolders = objCurrentFolder.SubFolders
		Set colFiles = objCurrentFolder.Files
		multipleFiles = colFiles.count
		counter = 1
		
		path = newpath + "\" + objCurrentFolder.name
	
		'iterate through sub folders & change directory path that the shell is currently in. 
		For Each objFolder in colSubfolders
			objShell.CurrentDirectory = objFolder
			call movefiles(objFolder.Path, path, basepath)
		Next
	
		'copy file
		If (multipleFiles > 1) Then
			For Each objFile in colFiles
				oldname = objFile
				newname = basepath & "\" & objCurrentFolder.name & "_" & counter & Right(objFile.name,4)
				objFSO.moveFile oldname, newname
				counter = counter + 1
			Next
		else
			For Each objFile in colFiles
				oldname = objFile
				newname = basepath & "\" & objCurrentFolder.name & Right(objFile.name,4)
				objFSO.moveFile oldname, newname
			Next
		end if
	end If
End sub
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
It might help to handle only the SubFolders recursion before loading the Files. Basically keep movefiles as slim as possible until that recursion is done.

Say, are you restoring the original value of objShell.CurrentDirectory after that recursion? :sneaky:

I hope nobody's been doing this! (SFW, despite the title.)
 

PowerYoga

Diamond Member
Nov 6, 2001
4,603
0
0
I passed a different objFSO inside the loop (that I'm setting to nothing after each loop) to see if it helps, as the object is only there for me to make movefile calls. Will keep posted!