Thread View: comp.lang.basic.visual
5 messages
5 total messages
Started by Freak
Tue, 18 Mar 2003 05:38
Changing only the first letter in a file name
Author: Freak
Date: Tue, 18 Mar 2003 05:38
Date: Tue, 18 Mar 2003 05:38
6 lines
2200 bytes
2200 bytes
I know this should be a very simple task but I am pulling my hair out over it. I am using the commondialog.showopen to access the files and want the first letter of the file I select to be changes to the letter "b". I am using vb6. Thank you for your help (and my hair thanks you also)!!! Corey
Re: Changing only the first letter in a file name
Author: Visual Boodu .NET
Date: Tue, 18 Mar 2003 06:13
Date: Tue, 18 Mar 2003 06:13
46 lines
3014 bytes
3014 bytes
Is this what your after?
REQ: form, label, common dialog control & command button
Private Sub Command1_Click()
' Set CancelError is True
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' Set flags
CommonDialog1.Flags = cdlOFNHideReadOnly
' Set filters
CommonDialog1.Filter = "All Files (*.*)|*.*|"
' Specify default filter
CommonDialog1.FilterIndex = 2
' Display the Open dialog box
CommonDialog1.ShowOpen
' Display name of selected file
Dim A As Integer, FullFileName As String
FullFileName = CommonDialog1.FileName
For A = 0 To 1000
'take out directories from the full file path
If InStr(1, FullFileName, "\") Then
FullFileName = Mid(FullFileName, InStr(1, FullFileName, "\") _
+ 1, Len(FullFileName) - InStr(1, FullFileName, "\"))
Else
Exit For
End If
Next A
'Change first letter to b
Label1.Caption = "b" & Mid(FullFileName, 2, Len(FullFileName) - 1)
Exit Sub
ErrHandler:
'User pressed the Cancel button
Exit Sub
End Sub
Re: Changing only the first letter in a file name
Author: Oci-One Kanubi
Date: Tue, 18 Mar 2003 14:17
Date: Tue, 18 Mar 2003 14:17
112 lines
5884 bytes
5884 bytes
Well, if the OP just wants the filename/extension pair, and will be
discarding the rest of the path, that would work fine. But if he
wants to retain the entire path (i.e., "C:\MyFiles\Important.xls" =>
"C:\MyFiles\bmportant.xls"), he needs to work forward from the end.
Dim aNum as Integer, len as Integer, before as Integer, _
after as Integer
Dim OldPath as String, aChar as String, NewPath as String
'CommonDialog code goes here, to get OldPath, including filename
len = Length(OldPath) ' *1
before = 0 ' Assume no path precedes filename/exten, so we...
after = len - 1 ' ...want everything from the second character on.
For aNum = len to 1 Step -1 ' Begin at the end & work forward
aChar = Mid(OldPath, aNum , 1) ' Checking each character.
If aChar = "\" Or aChar = ":" Then ' *2
before = aNum ' When a Dir symbol is found,...
after = (len - aNum) - 1 ' ...keep all up to the symbol...
Exit Loop ' ...& all after filename initial.
End If
Next aNum
NewPath = Left(OldPath, before) & "b" & Right(OldPath, after) ' *3
' *1 I doubt if it is possible for CommonDialog to return a path of
' the form "myfile.foo", but we have to initialize "before" and
' "after" to *something*!
' *2 I don't remember whether it is possible for CommonDialog to
' return a path of the form "C:myfile.foo" (that is, a "path"
' without at least one backslash); if not, remove the colon test
' and the aChar variable, changing the test line to:
' If Mid(OldPath, aNum , 1) = "\" Then
' *3 Don't trust my untested, off-the-cuff code! Key this in and
' TEST IT before you use it!
Another approach would be:
For aNum = 1 to Length(OldPath) ' aNum2 will identify final...
aChar = Mid(OldPath, aNum , 1) ' ...occurance of Dir delimiter
If aChar = ":" Or aChar = "\" then aNum2 = aNum
Next aNum
NewPath = Left(OldPath, aNum2) & "b" & _
Right(OldPath, (Length(OldPath) - aNum2) - 1 )
or simplest of all, *IF* the filepath returned by CommonDialog
must always include at least one backslash:
Dim aNum as Integer, OldPath as String, NewPath as String
For aNum = Length(OldPath) to 1 Step -1
If Mid(OldPath, aNum , 1) = "\" Then Exit Loop
Next aNum
NewPath = Left(OldPath, aNum) & "b" & _
Right(OldPath, (Length(OldPath) - aNum) - 1 )
-Richard
======================================================================
Richard Hopley, Winston-Salem, NC, USA
rhopley@earthlink.net 1-301-775-0471
Nothing really matters except Boats, Sex, and Rock'n'Roll.
rhopley@wfubmc.edu 1-336-713-5077
OK, OK; computer programming for scientific research also matters.
"Visual Boodu .NET" <sn@il.mail> wrote in message news:<aOyda.4231$L57.39496@news-server.bigpond.net.au>...
> Is this what your after?
>
> REQ: form, label, common dialog control & command button
>
> Private Sub Command1_Click()
> ' Set CancelError is True
> CommonDialog1.CancelError = True
> On Error GoTo ErrHandler
> ' Set flags
> CommonDialog1.Flags = cdlOFNHideReadOnly
> ' Set filters
> CommonDialog1.Filter = "All Files (*.*)|*.*|"
> ' Specify default filter
> CommonDialog1.FilterIndex = 2
> ' Display the Open dialog box
> CommonDialog1.ShowOpen
> ' Display name of selected file
> Dim A As Integer, FullFileName As String
>
> FullFileName = CommonDialog1.FileName
>
> For A = 0 To 1000
>
> 'take out directories from the full file path
> If InStr(1, FullFileName, "\") Then
>
> FullFileName = Mid(FullFileName, InStr(1, FullFileName, "\") _
> + 1, Len(FullFileName) - InStr(1, FullFileName, "\"))
>
> Else
>
> Exit For
>
> End If
>
> Next A
>
> 'Change first letter to b
> Label1.Caption = "b" & Mid(FullFileName, 2, Len(FullFileName) - 1)
> Exit Sub
>
> ErrHandler:
> 'User pressed the Cancel button
> Exit Sub
> End Sub
Re: Changing only the first letter in a file name
Author: Freak
Date: Fri, 21 Mar 2003 14:14
Date: Fri, 21 Mar 2003 14:14
1 lines
2064 bytes
2064 bytes
Thanks, That did the trick!!! I used the first example.
Re: Changing only the first letter in a file name
Author: Oci-One Kanubi
Date: Mon, 24 Mar 2003 16:15
Date: Mon, 24 Mar 2003 16:15
16 lines
2388 bytes
2388 bytes
HTH,
-R
======================================================================
Richard Hopley, Winston-Salem, NC, USA
rhopley@earthlink.net 1-301-775-0471
Nothing really matters except Boats, Sex, and Rock'n'Roll.
rhopley@wfubmc.edu 1-336-713-5077
OK, OK; computer programming for scientific research also matters.
======================================================================
"Freak" <freak@austin.rr.com> typed:
> Thanks, That did the trick!!! I used the first example.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads