?
Solved

delete from text file

Posted on 2004-11-24
12
Medium Priority
?
176 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:jlymn
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 12668009
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12668061
Similarly

...

SplitPath = Split(Trim$(strFilePath), "\")
strFileName = SplitPath(ubound(SplitPath))
...
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 500 total points
ID: 12668078
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:connah0047
ID: 12668085
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12668086
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
 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 500 total points
ID: 12680502
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12680533
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
 
LVL 1

Expert Comment

by:connah0047
ID: 12681313
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12696030
>> 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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question