Rename files (remove last few random numbers)  
Author Message
angel_2002_123





PostPosted: Wed Jul 14 22:59:02 CDT 2004 Top

Visual Basic >> Rename files (remove last few random numbers) Hey everyone I've got a small problem with one of my codes that I can't
figure out how to get around. When I save files from my program it
outputs a name like ******_***_##.ps My problem is the ## is a counter
number generated by the program when it saves the file. What I?m trying
to do is remove this number and its preceding underscore. Below is the
code that I?m currently using. At the present time the code works
great, however, the counter number will soon reach 100, which is 3
digits. Right now the program counts back from the end and is set for 2
digits. I need the program to take this random number off the end
regardless of whether the counter is at 1 or 9999, which is the range of
the counter. The file names vary in length so I cannot count from the
front side of the filename either. The code needs to simply remove
everything from the second underscore on and then put the .ps back on
the end. If you could help me with this I would really appreciate it.
Thanks for your time.

Private Sub Command1_Click()
Dim MyName As String
Dim Temp As String
Dim MyDir As String

MyDir = "c:\temp"
NewDir = "c:\temp2"
Temp = Dir$(MyDir & "*.ps")
Do While Temp <> ""

MyName = Left$(Temp, Len(Temp) - 6)

Name MyDir & Temp As NewDir & MyName & Right$(Temp, 3)


Temp = Dir$()

Loop
msg = "It's done"
MsgBox (msg)
Unload Form1
End Sub



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Visual Studio65  
 
 
JayVinton





PostPosted: Wed Jul 14 22:59:02 CDT 2004 Top

Visual Basic >> Rename files (remove last few random numbers) > outputs a name like ******_***_##.ps My problem is the ## is a counter
> The code needs to simply remove
> everything from the second underscore on and then put the .ps back on

Hi Brian,

Crude but functional. You'll need to add error handling to deal with malformed filenames.

Function FixFileName(strFileName As String) As String

Dim lngEndFirstPart As Long
Dim lngStartLastPart As Long
Dim i As Long

lngStartLastPart = InStr(1, strFileName, ".")

For i = lngStartLastPart To 0 Step -1

If (Mid$(strFileName, i, 1) = "_") Then

lngEndFirstPart = i - 1
Exit For

End If

Next

FixFileName = Mid$(strFileName, 1, lngEndFirstPart) & _
Mid$(strFileName, lngStartLastPart)

End Function


Jay