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
jlymnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LowfatspreadCommented:
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  
 
0
fds_fatboyCommented:
Similarly

...

SplitPath = Split(Trim$(strFilePath), "\")
strFileName = SplitPath(ubound(SplitPath))
...
0
vinnyd79Commented:
You would have to rewrite the file,then copy it over the original

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 Input 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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

connah0047Commented:
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
0
vinnyd79Commented:
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
0
jimbobmcgeeCommented:
Here is a complete VBS implementation of my favourite approach (fds_fatboy's -- so credit him if you use it):

      Const ForReading = 1
      Const ForWriting = 2

      Dim oFSO, oFileIn, oFileOut, aLine

      Set oFSO = CreateObject("Scripting.FileSystemObject")                     'GET OBJECT TO MANAGE FILES
      Set oFileIn = oFSO.OpenTextFile(InputBox("File to read"), ForReading)      'OPEN INPUT FILE
      Set oFileOut = oFSO.CreateTextFile(InputBox("File to write"), ForWriting)      'OPEN OUTPUT FILE

      Do Until oFileIn.AtEndOfStream            'LOOP THROUGH IN FILE

            aLine = Split(oFileIn.ReadLine, "\")            'READ LINE AND SPLIT IT
            If IsArray(aLine) = True Then
                  oFileOut.WriteLine aLine(Ubound(aLine))      'WRITE LAST ENTRY TO OUT FILE
            Else
                  oFileOut.WriteLine aLine
            End If

      Loop

      oFileIn.Close            'CLOSE FILES
      oFileOut.Close

      Set oFileIn = Nothing
      Set oFileOut = Nothing
      Set oFSO = Nothing            'CLEANUP

HTH

J.
0
jimbobmcgeeCommented:
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.
0
connah0047Commented:
jimbobmcgee: Oh you're think you're so hot, huh? jk :) I like that approach...I've never thought to do it like that...
0
jimbobmcgeeCommented:
>> 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.