Numbering files  | |
June 24th, 2003, 12:24 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: York, PA.
Posts: 1,569
|
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 |
| |
June 24th, 2003, 12:35 PM
|
#2 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,401
|
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 
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
|
| |
June 24th, 2003, 12:53 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Jun 2002 Location: Vancouver, WA, USA
Posts: 2,696
|
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 |
| |
June 24th, 2003, 03:46 PM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: York, PA.
Posts: 1,569
|
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.
|
| |
June 24th, 2003, 04:59 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: York, PA.
Posts: 1,569
|
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."
|
| |
June 24th, 2003, 06:39 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Jun 2002 Location: Vancouver, WA, USA
Posts: 2,696
|
lol, thats awesome! thanks for sharing.
-Chris |
| |
June 24th, 2003, 07:33 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: York, PA.
Posts: 1,569
|
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 |
| |
June 24th, 2003, 07:50 PM
|
#8 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,401
|
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. ) |
| |
June 24th, 2003, 10:05 PM
|
#9 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: York, PA.
Posts: 1,569
|
Thanks Vass I will look into that. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |