vbscript - VBS: For-each loop (sometimes) iterates through file collection indefinitely -


the goal of following vbscript prepend user-defined string files particular extension within specified directory:

directory = "c:\users\xxxxxxxx\desktop\test\"   'include final backslash extension = ".doc"  'include period, ex: ".tab" ''''''''''''''''''''''''''''''''''''''''''  addstr = inputbox("enter text prepend:", , "xxxxxxxx_xxxxxxxxxx_x_xx_xxx_")   set objfso = createobject("scripting.filesystemobject") set objfolder = objfso.getfolder(directory) set colfiles = objfolder.files  each file in colfiles     abspath = objfso.getabsolutepathname(file)     currentextension = objfso.getextensionname(abspath)     if  strcomp(currentextension, mid(extension, 2)) = 0         file.name = addstr & objfso.getfilename(file)     end if next 

the script works well, demonstrates problematic behavior:

when running script on directory lots of files and/or files long names, script appears iterate on collection of files (i.e. prepends files have been prepended) , until filenames become long recognized fso, crashing script.

the threshold of number of files/length of filenames @ occurs appears distinct , reproducible. example, if create target directory (e.g. "...\desktop\test") file named '1.doc' copied/pasted several times, script rename 31 files, demonstrates problematic behavior 32+ files. similarly, if run script twice on 31 files (generated in same manner), script demonstrates problematic behavior on second run.

any thoughts underlying issue appreciated--thanks in advance!

you may have issues here because you're modifying files while iterating them. try creating array of file names first , iterate on array, changing names.

redim a(colfiles.count - 1) = 0  each file in colfiles     a(i) = file.path     = + 1 next  = 0 ubound(a)     if strcomp(objfso.getextensionname(a(i)), mid(extension, 2)) = 0         objfso.getfile(a(i))             .name = addstr & .name         end     end if next 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -