Link to home
Start Free TrialLog in
Avatar of jlymn
jlymn

asked on

delete from text file

I have a text file with thousands of lines like this...

c:\documents and settings\administrator\my documents\$user\ab\aa - backup\test.txt

using vb or vbscript how can i find the last "\" on each line and delete it and all preceding text on that line ?

(just want to be left with the filename and extension on each line)

I have access or excel if its easier to use these..?

Thanks
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

trim the string
get the length of the trimmed string
test each character moving backwards from the end until a \ is found
then use Mid(trimmed string, pos+1, length(trimmed) - pos - 1) to get the file name...

hth  
 
Avatar of fds_fatboy
fds_fatboy

Similarly

...

SplitPath = Split(Trim$(strFilePath), "\")
strFileName = SplitPath(ubound(SplitPath))
...
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here ya go...hope this helps...

    Dim strFileName As String
    Dim strNewFileName As String
   
    Open "c:\InputFile.txt" For Input As #1
    Open "c:\OutputFile.txt" For Output As #2
        Do Until EOF(1)
            DoEvents
            Line Input #1, strFileName
            strNewFileName = Mid(strFileName, InStrRev(strFileName, "\") + 1)
            Print #2, strNewFileName
        Loop
    Close #2
    Close #1
sorry,had a typo there.The second file should be open for output:

Private Sub Command1_Click()
Dim ff As Integer, nf As Integer, Ln As String
Dim sFile As String, nFile As String

sFile = "C:\Somefile.txt"
nFile = Environ("Temp") & "\TmpFile.tmp"

ff = FreeFile
Open sFile For Input As #ff
nf = FreeFile
Open nFile For Output As #nf
Do Until EOF(ff)
Line Input #ff, Ln
Print #nf, Mid$(Ln, InStrRev(Ln, "\") + 1)
DoEvents
Loop
Close #ff
Close #nf
FileCopy nFile, sFile

End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I only wrote the above because vinnyd79's and connah0047's approaches won't work if you want to use a VBScript:

  -  Command1_Click() only possible in VB/VBA
  -  Open ... For ... As #... only possible in VB/VBA

whereas, you can copy my code above into a .VBS file and it'll run without compilation...

J.
jimbobmcgee: Oh you're think you're so hot, huh? jk :) I like that approach...I've never thought to do it like that...
>> Oh you're think you're so hot, huh? jk :)
No, I'm not hot ;)  I just loathe VBS enough (particularly for the lack of Open#, API declares and Dim ... As ...)

>> I like that approach...I've never thought to do it like that...
Me too and me neither, that's why I think fds_fatboy should definately get some points!!

J.