Edit a text file automatically and save it.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • antonopn
    New Member
    • Mar 2008
    • 42

    Edit a text file automatically and save it.

    Hello to everyone!

    I'm a newbie here, I just started my VB "carier".. :)

    Here is my question (might me simple to you but I 've found it a bit hard).

    I have a txt file in which I want to replace every ":" with a "." !!!

    After this replacement I want to replace every ".3" with a ".5" !!!

    In fact the txt is a clock file and there are a lot of them here... I want to do this task automatically and not by opening every sigle file with notepad and replacing them manually.

    THANK YOU FOR YOUR TIME AND YOUR PATIENCE!!!!
  • gobblegob
    New Member
    • Dec 2007
    • 133

    #2
    Hi antonopn

    load the txt file into textbox (text1.text)
    then you can save it.
    Code: ( VB 6 )
    Code:
    Private Sub Command1_Click()
        text1.Text = MyReplace(text1.Text, ":", ".") 'choose characters that you want to
                                                     'convert
        text1.Text = MyReplace(text1.Text, "3", "5")
    End Sub
    
    Public Function MyReplace(strExpression As String, strFind As String, strReplace As String)
        Dim intX As Integer
        If (Len(strExpression) - Len(strFind)) >= 0 Then
            For intX = 1 To Len(strExpression)
                If Mid(strExpression, intX, Len(strFind)) = strFind Then
                    strExpression = Left(strExpression, (intX - 1)) + strReplace + Mid(strExpression, intX + Len(strFind), Len(strExpression))
                End If
            Next
        End If
        MyReplace = strExpression
    End Function
    GobbleGob.

    Comment

    • antonopn
      New Member
      • Mar 2008
      • 42

      #3
      Thanks a lot for your help!!!!!

      I've got only one problem. I get a runtime error 424. "Object required"
      Lets suppose that my text file is nik.txt and is in C:\ directory.
      C:\nik.txt

      How should I write the following in order to get it done?

      Code:
      Private Sub Command1_Click()
            nik.txt = MyReplace(nik.txt, ":", ".")
            nik.txt = MyReplace(nik.txt, ".3", ".5")
      End Sub
      Thank you so much!!!!

      Comment

      • Robbie
        New Member
        • Mar 2007
        • 180

        #4
        Originally posted by antonopn
        I have a txt file in which I want to replace every ":" with a "." !!!

        After this replacement I want to replace every ".3" with a ".5" !!!

        In fact the txt is a clock file and there are a lot of them here... I want to do this task automatically and not by opening every sigle file with notepad and replacing them manually.

        THANK YOU FOR YOUR TIME AND YOUR PATIENCE!!!!
        Well, first you'll need to open the file for input, grab its contents and close the file.

        [code=vb]
        Dim FileID as integer 'Our handle to the open file
        Dim WholeFile as string 'String which will contain entire contents of text file
        FileID = FreeFile 'Pick the first available handle
        Open FILENAME for input as FileID
        WholeFile = Input(LOF(FileI D), #FileID)
        'LOF() gives the length of the file, in bytes. We read this many characters and store them in WholeFile.
        Close FileID 'We're done with the file now for now.
        [/code]
        FILENAME should be the full path and filename of the text file. Or, it could just be the filename, if the text file is in the same folder as the program (App.Path).

        To replace characters, use VB's Replace() function.
        [code=vb]
        WholeFile = Replace(WholeFi le, ORIGTEXT, REPLACEMENTTEXT )
        [/code]
        Of course, you can repeat this for however many replacements you need to perform.

        Save this string in a new file.
        [code=vb]
        FileID = FreeFile
        Open FILENAME for output as FileID
        Print #FileID, WholeFile
        Close FileID
        [/code]
        NOTE: The file will be completely erased and re-built when that code executes. Also note that the 'Print' statement adds new-line and carriage-return characters to the end of the file (chr(13) and chr(10)).
        Hope that helps you on your way. =)

        Comment

        • antonopn
          New Member
          • Mar 2008
          • 42

          #5
          Originally posted by Robbie
          Well, first you'll need to open the file for input, grab its contents and close the file.

          [code=vb]
          Dim FileID as integer 'Our handle to the open file
          Dim WholeFile as string 'String which will contain entire contents of text file
          FileID = FreeFile 'Pick the first available handle

          [/code]
          Thanks for your help, I get a "freefile", "invalid outside procedure".
          Any suggestions?

          Comment

          • Robbie
            New Member
            • Mar 2007
            • 180

            #6
            Hmm? Are you doing this within a sub/function?
            e.g.
            [code=vb]
            Private Sub Form_Load()
            AllTheCodeIPost ed
            End Sub
            [/code]

            Comment

            • antonopn
              New Member
              • Mar 2008
              • 42

              #7
              I always get a error when entering the path of my file. No matter if I do it using your code or Googlebob's!

              For example the filename and directory is C:\nik.txt
              I get an error on ":" implying that an "AS" is expected.

              No matter what I do i get the same error.

              Thanks for your patience!!!
              VB is not my expertise (you can see for yourself).

              Comment

              • tristanlbailey
                New Member
                • Apr 2007
                • 30

                #8
                Single clock file?

                Open the clock file in Notepad.
                Goto "Edit" > "Replace".
                In the "Find what" field, type: :
                In the "Replace with" field, type: .
                Click "Replace All".
                In the "Find what" field, type: .3
                In the "Replace with" field, type: .5
                Click "Replace All".
                Goto "File" > "Save".
                Done.

                Multiple clock files?

                This VB code assumes that all text files found in the application's directory are clock files. If you have other text files within the same directory, the code will need to be adjusted.

                [CODE=vb]
                Private FileStr As String, TempStr As String

                Sub Main()

                'Store the first text file found
                '(all text files in the application's
                'directory are assumed to be clock files)
                FileStr = Dir(App.Path & "\*.txt")

                'Go through all text files found
                Do Until FileStr = vbNullString
                'Store the current text file's path
                FileStr = App.Path & "\" & FileStr
                'Open the current text file for reading
                Open FileStr For Input As #1
                TempStr = Input$(LOF(1), 1)
                'Close the file
                Close #1
                'Replace all occurances of ":" with "."
                TempStr = Replace(TempStr , ":", ".")
                'Replace all occurances of ".3" with ".5"
                TempStr = Replace(TempStr , ".3", ".5")
                'Open the current text file for writing
                Open FileStr For Output As #1
                'Write changes to the file
                Print #1, TempStr
                'Close the file
                Close #1
                'Find the next text file
                FileStr = Dir
                Loop

                End Sub
                [/CODE]

                Comment

                • Katerina22
                  New Member
                  • Apr 2008
                  • 1

                  #9
                  Hello everyone! I find this site really interesting!

                  It is a useful script.. Though I do not have clock files but other type of files. I change "," with "." in my files!

                  But I do not know anything about VB. :(

                  I copy-paste this code in VB, in a new standard EXE. And press the "play" button.
                  But nothing happens. (though there are no errors in the code after compilation)

                  My files are in directory C:\ktr\

                  Please help me...
                  I know it is a funny question for you :)

                  Kisses Katerina

                  Comment

                  • Robbie
                    New Member
                    • Mar 2007
                    • 180

                    #10
                    Originally posted by antonopn
                    I always get a error when entering the path of my file. No matter if I do it using your code or Googlebob's!

                    For example the filename and directory is C:\nik.txt
                    I get an error on ":" implying that an "AS" is expected.

                    No matter what I do i get the same error.

                    Thanks for your patience!!!
                    VB is not my expertise (you can see for yourself).

                    Ah! You just need to surround the filename in quotes, like "this". ;)
                    Sorry, I didn't think to show that in my example.

                    Comment

                    • tristanlbailey
                      New Member
                      • Apr 2007
                      • 30

                      #11
                      Originally posted by Katerina22
                      Hello everyone! I find this site really interesting!

                      It is a useful script.. Though I do not have clock files but other type of files. I change "," with "." in my files!

                      But I do not know anything about VB. :(

                      I copy-paste this code in VB, in a new standard EXE. And press the "play" button.
                      But nothing happens. (though there are no errors in the code after compilation)

                      My files are in directory C:\ktr\

                      Please help me...
                      I know it is a funny question for you :)

                      Kisses Katerina
                      Katerina,

                      Ha ha, "clock files"... I don't think that this is an actual file type, but it could become one. Anyway;

                      In the example I have provided previously, find:
                      [CODE=vb]TempStr = Replace(TempStr , ":", ".")[/CODE]
                      "TempStr" is the string variable in which you want the replacement text to be stored. Replace is the function in Visual Basic that allows a character or string of characters to be replaced in a particular string. In the code above, the function's first parameter "TempStr" is also the variable that you want the Replace function to search, while the second parameter (in this case :) is the character (or string) you want replaced, and the third parameter (in this case .) is the character (or string) you want the second parameter to be replaced with.

                      If you have the MSDN Library installed on your computer, you can look up more information on the Replace functions, as well as many other functions, methods, properties, etc. in Visual Basic. The MSDN Library usually comes with a Visual Studio package. If not, you can always try the net.

                      Unless you save the Visual Basic project in the same directory as your text files (in your case, "C:\ktr\"), or compile your project into an .exe file using the "File" > "Make..." menu option, and place the EXE into "C:\ktr\", the code won't work.

                      If you don't want your text files to be in the same place as your program is, change:
                      [CODE=vb]FileStr = Dir(App.Path & "\*.txt")[/CODE]
                      to:

                      [CODE=vb]FileStr = Dir("Your text file directory here\Your text file here.txt")[/CODE]
                      or:

                      [CODE=vb]FileStr = Dir("Your text file directory here\*.txt")[/CODE]
                      for all text files in that directory.

                      Save the project before running it.

                      Comment

                      • antonopn
                        New Member
                        • Mar 2008
                        • 42

                        #12
                        Well thank you for the advice!

                        It did not work to me! None of the solutions. I do not know if Katerina succeeded.
                        The code has no errors but nothing is done!

                        I save it inside the txt file's directory and run it but nothing. It might be a missing library (project->prefences).
                        This is the only I can imagine, because I created a DTS package in SQL SERVER 2000 and run it through VB (save as -> VB script). The table was created but data from txt did not copy! This sounds like a problem that does not allow txt files to be used (edited, copied) through VB.

                        A library? I do not know. I'm tired with this!

                        THANK YOU ALL!!!

                        p.s. Katerina if you made it, I'll eat my hat :)

                        Comment

                        • tristanlbailey
                          New Member
                          • Apr 2007
                          • 30

                          #13
                          Antonopn, I'm not sure if this program would work as a Visual Basic Script. I used Visual Basic 6 to create it, and it works fine with that.

                          Do you have Visual Basic 6?

                          Comment

                          • antonopn
                            New Member
                            • Mar 2008
                            • 42

                            #14
                            yes I do! Visual Basic 6.

                            What do you create?

                            1)standard exe
                            2)activeX exe
                            3)dll

                            Something's wrong. Do not know what. I feel totally idiot with that :)

                            Thanks for your patience!

                            Comment

                            • tristanlbailey
                              New Member
                              • Apr 2007
                              • 30

                              #15
                              A Standard EXE program will do.
                              Remove the form that it puts in automatically for you, and instead add a module (Project > Add Module). Place the previous code I gave you in the module, save the project in the same directory as your clock files, and then try running the program (click the triangular play button).

                              Once you are satisfied that the program works, goto "File > Make "your project name"", type a name for your program EXE file, and then click OK. You can now run the program from the EXE file.

                              Comment

                              Working...