»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 06-24-2003, 09:24 AM   #1 (permalink)
Registered User
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,326
korgul is on a distinguished road
Numbering files

Here is what I am trying to do. I want to change the name of all files in a specified folder. That part I have, the only problem is the way that they are numbered. They start with 1,2,3,4,5,6 ect so if there are say 30 files in the folder they are displayed like this 1,10,11,12,13,14,15,16,....2,20,21,....3,30. What I am trying to do is get them so that the number that is tacked onto the end is something like 001, 002, 003 etc. that way the files are displayed in numeric order.

here is what I have so far

Code:
Dim oFSO
Dim oFile
Dim sPath
Dim sBaseFileName
Dim sExt
Dim lCount

'  Change to reflect the path you want to use:
Function where
sPath = inputbox ("What is the location of the files")
End Function

'  Change to whatever you want to be the base filename:
Function What
sBaseFileName = inputbox("What do you want to name them to?")
End Function

'  Initialize variables.
lCount = 0

Where
What
'  Create the FileSystemObject.
Set oFSO = CreateObject("Scripting.FileSystemObject")

'  Loop each file in the selected folder.
For Each oFile In oFSO.GetFolder(sPath).Files
    '  Increment the index value.
    lCount = lCount + 1
    '  Get the file's extension.
    sExt = Mid(oFile.Name, InStrRev(oFile.Name, ".") +1)
    '  Rename the file with the base filename, index, and file's extension.
    oFile.Name = sBaseFileName & lCount & "." & sExt
Next

'  Release the FileSystemObject.
Set oFSO = Nothing

MsgBox "Your files have been renamed."
Thanks

korgul is offline   Reply With Quote
Old 06-24-2003, 09:35 AM   #2 (permalink)
Registered User
 
Join Date: Oct 2001
Location: TOO close to Wash DC
Posts: 7,956
vass0922 is on a distinguished road
I was looking into the STUFF function, but that may only be for SQL

I don't have time to write it out at the moment unfortunately but what you may have to do is something like
Code:
Select Case lCount
Case < 10
   sPadding = "00"
Case < 100
   sPadding = "0"
Case < 1000
   sPadding = ""
End Select
oFile.Name = sBaseFileName & sPadding & lCount & "." & sExt

There is also a builtin function for getting a file extension, I'll look it up later I can't remember at the moment

Here is a good page if you just need a quick reference to FSO objects

http://www.sloppycode.net/fso/?m=61
Its not great, but it works if you just can't remember the syntax
__________________
<< Insert exceedingly large and overly verbose message of how 1337 you are here including full specs of every vehicle you've ever driven and PC you've owned >>
vass0922 is offline   Reply With Quote
Old 06-24-2003, 09:53 AM   #3 (permalink)
Registered User
 
implexant's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 1,991
implexant is on a distinguished road
Send a message via ICQ to implexant Send a message via AIM to implexant Send a message via Yahoo to implexant Send a message via Skype™ to implexant
I have a program that does precisely what your trying to do. I got it free from somewhere, but I didn't code it myself. If you want it just drop me an email: chris@implexantsystems.com

Of course if your just coding it for the fun of it, best of luck to ya

-Chris
implexant is offline   Reply With Quote
Old 06-24-2003, 12:46 PM   #4 (permalink)
Registered User
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,326
korgul is on a distinguished road
Well I got it very very close. I can live with the way it names them now. It starts at 1000 and goes up from there. Here is what I added

Code:
If lcount >=99 then
	ofile.name = sbasefilename & lcount & "." & sExt
else 
	ofile.name = sbasefilename & "0" & lcount & "." & sExt
end if
I added it right after this part of the code.

Code:
'  Loop each file in the selected folder.
For Each oFile In oFSO.GetFolder(sPath).Files
    '  Increment the index value.
    lCount = lCount + 1
    '  Get the file's extension.
    sExt = Mid(oFile.Name, InStrRev(oFile.Name, ".") +1)
    '  Rename the file with the base filename, index, and file's extension.
korgul is offline   Reply With Quote
Old 06-24-2003, 01:59 PM   #5 (permalink)
Registered User
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,326
korgul is on a distinguished road
Tweaked it a bit more so you can not change files in the C:\Windows or C:\Windows\system32 folders.

Is there a way to put a wild card in so that if the spath = any c:\windows it will kick you out.
So if I enter the path c:\windows\drivers it will kick me out also.

here is the code for what I have now. Not trying to toot my own horn here but this is actually the first thing that I made with VBS that is actually useful.

Code:
Dim oFSO
Dim oFile
Dim sPath
Dim sBaseFileName
Dim sExt
Dim lCount

'  Change to reflect the path you want to use:
Function where
sPath = inputbox ("What is the location of the files")
if spath = "c:\windows" then
	msgbox ("Sorry, That is a system directory and cannot be changed")
	where
end if
if spath = "c:\windows\system32" then
	msgbox ("Sorry, That is a system directory and cannot be changed")
	where
end if
End Function

'  Change to whatever you want to be the base filename:
Function What
sBaseFileName = inputbox("What do you want to name them to?")
End Function

'  Initialize variables.
lcount=0

Where
What
'  Create the FileSystemObject.
Set oFSO = CreateObject("Scripting.FileSystemObject")

'  Loop each file in the selected folder.
For Each oFile In oFSO.GetFolder(sPath).Files
    '  Increment the index value.
    lCount = lCount + 1
    '  Get the file's extension.
    sExt = Mid(oFile.Name, InStrRev(oFile.Name, ".") +1)
    '  Rename the file with the base filename, index, and file's extension.
If lcount >=99 then
	ofile.name = sbasefilename & lcount & "." & sExt
else 
	ofile.name = sbasefilename & "00" & lcount & "." & sExt
end if

    
Next

'  Release the FileSystemObject.
Set oFSO = Nothing

MsgBox "Your files have been renamed."
korgul is offline   Reply With Quote
Old 06-24-2003, 03:39 PM   #6 (permalink)
Registered User
 
implexant's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 1,991
implexant is on a distinguished road
Send a message via ICQ to implexant Send a message via AIM to implexant Send a message via Yahoo to implexant Send a message via Skype™ to implexant
lol, thats awesome! thanks for sharing.

-Chris
implexant is offline   Reply With Quote
Old 06-24-2003, 04:33 PM   #7 (permalink)
Registered User
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,326
korgul is on a distinguished road
No problem Chris.

I am really enjoying the programing aspect. To be able to see something that you made actually work is great.

Don't worry more to come.

korgul
korgul is offline   Reply With Quote
Old 06-24-2003, 04:50 PM   #8 (permalink)
Registered User
 
Join Date: Oct 2001
Location: TOO close to Wash DC
Posts: 7,956
vass0922 is on a distinguished road
Use InStr

InStr looks in the string to find an instance of a given string.. so if you want you could have it look for \Windows\ so it won't matter what drive its on... OR you could use an environment variable.

Dim WshShell
Set objShell = CreateObject("Wscript.Shell")

sWinDir = objShell.ExpandEnvironmentStrings("%WINDIR%")

This will give you the exact windows directory the machine is using, so you just search for that (ie if its a 2k/NT box it will use C:\WInnt instead.. or in the ODD case that its on the D: drive. )
__________________
<< Insert exceedingly large and overly verbose message of how 1337 you are here including full specs of every vehicle you've ever driven and PC you've owned >>
vass0922 is offline   Reply With Quote
Old 06-24-2003, 07:05 PM   #9 (permalink)
Registered User
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,326
korgul is on a distinguished road
Thanks Vass I will look into that.
korgul is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 04:09 AM.