home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Numbering files

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2556
Discussions: 200,996, Posts: 2,379,944, Members: 246,364
Old June 24th, 2003, 12:24 PM   Digg it!   #1 (permalink)
Ultimate Member
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,569
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 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.
vass0922 is online now   Reply With Quote
Old June 24th, 2003, 12:53 PM     #3 (permalink)
Ultimate Member
 
implexant's Avatar
 
Join Date: Jun 2002
Location: Vancouver, WA, USA
Posts: 2,696
Send a message via ICQ to implexant Send a message via AIM to implexant Send a message via MSN to implexant Send a message via Yahoo 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 June 24th, 2003, 03:46 PM     #4 (permalink)
Ultimate Member
 
korgul's Avatar
 
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.
korgul is offline   Reply With Quote
Old June 24th, 2003, 04:59 PM     #5 (permalink)
Ultimate Member
 
korgul's Avatar
 
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."
korgul is offline   Reply With Quote
Old June 24th, 2003, 06:39 PM     #6 (permalink)
Ultimate Member
 
implexant's Avatar
 
Join Date: Jun 2002
Location: Vancouver, WA, USA
Posts: 2,696
Send a message via ICQ to implexant Send a message via AIM to implexant Send a message via MSN to implexant Send a message via Yahoo to implexant
lol, thats awesome! thanks for sharing.

-Chris
implexant is offline   Reply With Quote
Old June 24th, 2003, 07:33 PM     #7 (permalink)
Ultimate Member
 
korgul's Avatar
 
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
korgul is offline   Reply With Quote
Old 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. )
vass0922 is online now   Reply With Quote
Old June 24th, 2003, 10:05 PM     #9 (permalink)
Ultimate Member
 
korgul's Avatar
 
Join Date: Oct 2001
Location: York, PA.
Posts: 1,569
Thanks Vass I will look into that.
korgul is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3092)
Foxconn Blackops x48 MoBo (5)
Charges against non-tippers dropped.. (22)
Health Care Rationing (16)
Nvidia GTX 260 problem (14)
Delete an OS (17)
Laptop with wireless problem. (13)
Wireless Televisions. (12)
windows vista security holes (19)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (9)
[F@H SPAM 11/16/09] ! 1/2 months to.. (41)
windows 7 problem (7)
Recent Discussions
Nvidia GTX 260 problem (14)
add ram to existing (0)
Laptop with wireless problem. (13)
Point and Shoot Camera Suggestions. (9)
Is the PSU I received dead? (16)
FreeAgent drive software not x64 comp.. (1)
Intel 5100 AGN issues fixed yet? (28)
Foxconn Blackops x48 MoBo (5)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (41)
Print spooler problem (17)
Q9650 vs. Q9550 (2)
Desktop Calendar Application (2)
Looking for new motherboard (1)
soundmon.exe (8)
Jedi Academy Problem (3)
Can a page file be "too big".. (1)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)


All times are GMT -4. The time now is 11:26 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28