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!

How to limit changes to filename syntax to first 9 files only

Status
Not open for further replies.

electricwizard84

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

This thread is related to another thread i have just posted (For Each command skipping over some files), but refers to a different problem. For convenience, i have created two separate scripts with the aim of embedding elements of this script below to the main script in the first thread.

I am trying to write a script that renames only the first 9 files within a directory to a particular syntax. For example, the idea is that the first 9 video files are renamed to something such as 'Game of Thrones - S01E0X.mkv' where X is incremented up to 9. My first script actually already performs this but i needed to change 'E' (S01E10) to 'E0' (E01E09) for the first 9 files.

I have dabbled with while loops, and .Count statements to count the first 9 files, but there is an issue with embedded this loop within the For Each loop.

Any support 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)

'--------------------------------------------------

i = 0
For Each fil In fol.Files
'Do While i<9

fil.name = Replace(fil.Name, "good", "test")
'i = i+1
'loop
next

'--------------------------------------------------

WScript.Echo "Completed!
 
Dear all,

Please note that in the sample script above, i was focusing on making any filename changes to the first 9 files. Therefore, replacing "good" with "test" is merely an example.
 
There a ton of other free renaming tools that will accomplish what you are trying to do much faster/easier and more robustly. Where you can create rules/macros..etc than a custom script.
You can not rewrite the name of the file object in this manner. You would need to 'move' this file from a.txt to A.txt
 
electricwizard84:
If all you want to do is force a leading zero for the episode number, so episodes show as E01, E02, ... E09, E10, E11 etc... then using the code from your other thread:

fil.Name = Name & " - S" & Season & "E" & [highlight #FCE94F]Right("0" & Episode, 2)[/highlight] & "." & Extension
 
Hi Guitarzan,

Thanks very much for this. I still don't understand how it stops the leading zero after E10 (i thought it would simply go E010...) but it works a treat. I will now look into the dictionary object as per your other comment to scan the files first then rename from the array.

Thanks again.
 
Here is what the Right() function does:

If you still don't understand why [highlight #FCE94F]Right("0" & Episode, 2)[/highlight] works, let us know. To summarize, it forces the number "Episode" to be a 2 digit string that will include a leading zero if needed.

Only the first 2 digits will me considered. if three digit numbers are needed, this will have to be modified.
 
Thank you very much for the link and your summary. This is much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top