Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each command skipping over some files

Status
Not open for further replies.

electricwizard84

Technical User
Feb 18, 2011
17
0
0
AU
Dear all,

I have developed a simple script to rename the filename of a video file according to the TV show name, Season No., Episode number and file extension. The script then renames all files in the directory and increments the episode number by one. For example, the first video is renamed 'Game of Thrones - S01E01.mkv' and the second video is renamed 'Game of Thrones - S01E02.mkv'.

Although the script executes well, i had noticed that the episode numbers that are incremented and added to the filename did not match the number of files in the directory. Thus, the increment skips over some files.

Any thoughts would be much appreciated.

----

Dim sName, source, c, Name, Season, Episode, i
Dim fso
Dim fol

' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' find location of files
source1 = InputBox("Enter video source path:")
source = source1 & "\"
Set fol = fso.GetFolder(source)


Name1 = InputBox("What is the TV Show Name")
Name = CStr(Name1)

Season1 = InputBox("What is the Season No. i.e 01")
Season = CStr(Season1)

Extension1 = InputBox("What is the video extension i.e mp4, mkv, avi")
Extension = CStr(Extension1)

'Episode = Int(1)
Episode = 1

For Each fil In fol.Files

fil.Name = Name & " - S" & Season & "E" & Episode& "." & Extension

Episode = Episode + 1

next

WScript.Echo "Completed!
 
I think the problem is that you are both scrolling through the files and renaming them at the same time, which changes the order the files are in and throws your script off. Better would be to scroll through the folder and store the filenames in an array or dictionary object, then scroll through those and do the renames.

If fact, jges posted a nice solution that uses the dictionary object in the thread below:
thread329-1676740
 
I have updated my script with the solution from jges and it now works perfectly. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top