Article View: comp.lang.basic.visual
Article #120Re: Changing only the first letter in a file name
From: 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
Technical Details
Message-ID:
<5ef7701c.0303180617.295d8c97@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mailReferences:
<nhyda.36614$fa.1451379@twister.austin.rr.com> <aOyda.4231$L57.39496@news-server.bigpond.net.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f94ee,87b1850e9e17a40b X-Google-Attributes: gidf94ee,public X-Google-Thread: 1053fc,87b1850e9e17a40b X-Google-Attributes: gid1053fc,public X-Google-Thread: 110e3b,87b1850e9e17a40b X-Google-Attributes: gid110e3b,public X-Google-Thread: fb992,87b1850e9e17a40b X-Google-Attributes: gidfb992,public X-Google-Thread: 106fd2,87b1850e9e17a40b X-Google-Attributes: gid106fd2,public X-Google-Thread: ffb5a,87b1850e9e17a40b X-Google-Attributes: gidffb5a,public X-Google-Thread: f645d,87b1850e9e17a40b X-Google-Attributes: gidf645d,public X-Google-Thread: 108062,87b1850e9e17a40b X-Google-Attributes: gid108062,public X-Google-ArrivalTime: 2003-03-18 06:17:06 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail From: rhopley@earthlink.net (Oci-One Kanubi) Newsgroups: comp.lang.basic.visual,comp.lang.basic.visual.misc,comp.lang.visual.basic,comp.lang.visual.basic.misc,cz.comp.lang.basic.visual,es.comp.lenguajes.visual-basic,free.it.programmazione.visualbasic,free.it.visual-basic,it.comp.lang.visual-basic Subject: Re: Changing only the first letter in a file name Date: 18 Mar 2003 06:17:05 -0800 Organization: http://groups.google.com/ Lines: 112 Message-ID: <5ef7701c.0303180617.295d8c97@posting.google.com> References: <nhyda.36614$fa.1451379@twister.austin.rr.com> <aOyda.4231$L57.39496@news-server.bigpond.net.au> NNTP-Posting-Host: 152.11.128.133 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1047997025 7418 127.0.0.1 (18 Mar 2003 14:17:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 18 Mar 2003 14:17:05 GMT