Announcement

Collapse
No announcement yet.

Delete the word from a file using script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete the word from a file using script

    I need either a vb script or batch script to do this operation.
    Delete from all the files which ever file contains the word “test” from a folder.
    Last edited by raj2009; 7th December 2009, 21:09.

  • #2
    Re: Delete the word from a file using script

    Originally posted by raj2009 View Post
    Delete from all the files which ever file contains the word “test” from a folder.
    Please help...Thanks in advance
    someone please help me

    Comment


    • #3
      Re: Delete the word from a file using script

      Do you really expect that someone is helping you within 3 minutes?
      Marcel
      Technical Consultant
      Netherlands
      http://www.phetios.com
      http://blog.nessus.nl

      MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
      "No matter how secure, there is always the human factor."

      "Enjoy life today, tomorrow may never come."
      "If you're going through hell, keep going. ~Winston Churchill"

      Comment


      • #4
        Re: Delete the word from a file using script

        Not exactly but I tried to explain more.

        Comment


        • #5
          Re: Delete the word from a file using script

          Originally posted by raj2009 View Post
          Not exactly but I tried to explain more.
          Well, not to be anal, but you didn't. Your question does not parse. You seem to have some luck, as my crystall ball is not out of batteries at the moment.

          Do you wish to check all the files in some directory, look for string "test" and delete such files? If not, try and provide an example about your needs.

          Batch: try a for loop to go through the files. Use find /i to look for the string. Check the errorlevel to see if the string is found and finally delete such an occurance. I wouldn't pick this approach, as it is the most cubersome.

          Vbscript: get a Files collection via FileSystemObject. Read the file and look for string test. Delete if appropriate. This would work, and is the most portable one.

          Powershell: get-childitem for the directory, pipe items for select-string, pipe items for select-object and remove-item whatever you got. I'd go with Powershell, though it is not available in 2008 Core installs.

          -vP

          Comment


          • #6
            Re: Delete the word from a file using script

            Hi, Thank you so much for your reply.
            My requirement is to check all the files in some directory, look for string "test" and delete that particular line alone.
            I'm very new to VBscript, so please guide me.

            Thanks in advance.

            Comment


            • #7
              Re: Delete the word from a file using script

              Originally posted by raj2009 View Post
              check all the files in some directory, look for string "test" and delete that particular line alone.
              How come this has been asked recently? A common homework assignment in IITs?

              Here are some pointers. Try and work towards a solution.

              Code:
              ' Let's get the files and have a run with them
              set objFso = CreateObject("Scripting.FileSystemObject") 
              set objFolder = objFso.GetFolder(Where-your-files-are) 
               set colFiles = objFolder.Files 
                
              
              for each file in colFiles
               
                ...
              
              next
              Now I've shown how to get files from a folder. How about looking for the string? Maybe something like this would work:

              Code:
              set outFile = objFso.CreateTextFile(some-output-file)
              
              do until file.atEndOfStream
                str = objFile.ReadLine
              
                if( instr(lcase(str), "test") ) then
                ' found test, do nothing
                else
                ' did not, so write it into output file
                  outFile.WriteLine str
                end if
              loop
              
              objFile.Close
              outFile.Close
              What's going on? First, an output file is created. Then input file is readed line by line. Each line is searched for "test". If such a line is found, it will be skipped. All other files are written into the output file. This is because one cannot just delete a line from a file. What actually happens is that the old file is overwritten with new content.

              What's left is renaming the output file and putting the pieces together.
              -vP

              Comment


              • #8
                Re: Delete the word from a file using script

                ok, Thank you so much. I wil try as you suggested. The same I have to find for all the files under subfolders.

                Another main thing I need to get the files alone from the subfolders.

                Please help.

                Comment


                • #9
                  Re: Delete the word from a file using script

                  raj, WHY do you need to do this?
                  ** Remember to give credit where credit is due and leave reputation points where appropriate **

                  Comment


                  • #10
                    Re: Delete the word from a file using script

                    Originally posted by raj2009 View Post
                    from all the files which ever file contains the word “test”
                    What type of files?


                    Originally posted by raj2009 View Post
                    Another main thing I need to get the files alone from the subfolders.
                    Code:
                    ' Let's get the files and have a run with them
                    set objFso = CreateObject("Scripting.FileSystemObject")
                    Set objFolder = objFSO.GetFolder("path-of-base-folder")
                    
                    For Each objSubfolder in objFolder.Subfolders
                    
                        set colFiles = objSubfolder.Files
                    
                        For each file in colFiles
                         ' Wscript.echo file.Path
                    
                         . . .
                    
                       Next
                    
                    Next
                    \Rems

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: Delete the word from a file using script

                      Code:
                      @echo off
                      for /F %%a in ('dir /b /s /a-d file*txt') do (
                              findstr /v /i "test" %%a >temp
                              ren temp %%a
                      
                      )

                      Comment

                      Working...
                      X