Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: remove lines in txt files  (Read 7399 times)

0 Members and 1 Guest are viewing this topic.

PhilD

    Topic Starter


    Rookie

    • Experience: Experienced
    • OS: Windows 7
    Re: remove lines in txt files
    « Reply #15 on: April 26, 2017, 03:10:54 PM »
    Hi Hakoo - I'm on to it  - Thanks * 80

    I'll be back soon as possible
    I'm back - fan effing fantastic ;D ;D ;D

    Couple of changes would make it perfect
    • Could you write the the write the output to a subfolder (Edited Files) of the input folder,
    • ditto the Exceptions folder
    • The output files must keep the same names - they are used in the next step, for data matching

    Thanks heaps 

    PhilD





     











    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: remove lines in txt files
    « Reply #16 on: April 26, 2017, 06:04:30 PM »
    Tmeout is found in a resource kit.
    https://ss64.com/nt/timeout.html

    Syntax
          TIMEOUT delay [/nobreak]

    Key
       delay  Delay in seconds (between -1 and 100000) to wait before continuing.
              The value -1 causes the computer to wait indefinitely for a keystroke
              (like the PAUSE command)

       /nobreak
              Ignore user key strokes. (Windows 7 or greater)


    PhilD

      Topic Starter


      Rookie

      • Experience: Experienced
      • OS: Windows 7
      Re: remove lines in txt files
      « Reply #17 on: April 26, 2017, 08:13:35 PM »
      I'm back - fan effing fantastic ;D ;D ;D

      Couple of changes would make it perfect
      • Could you write the output to a subfolder (Edited Files) of the input folder,
      • ditto the Exceptions folder
      • The output files must keep the same names - they are used in the next step, for data matching
      @Hakoo - could we add three 'nice to have' changes  ;)
      • if nothing gets written to the Exceptions folder delete it, or only create it if/when its needed,
      • when finished, rather than opening the Edited Files folder in Explorer, show a "Finished" popup with an OK button.
      • if its possible and its easy to do, could the progress window be made to stay on top.
      Thanks again PhilD

      PhilD

        Topic Starter


        Rookie

        • Experience: Experienced
        • OS: Windows 7
        Re: remove lines in txt files
        « Reply #18 on: April 27, 2017, 12:25:52 AM »
        @Hakoo

        Hi - we have run a couple of hundred files though the program and found one problem

        It seems the files sometimes (~5%) have "Purpose" repeated, but these need to be retained - so once the first one is found your program needs to stop looking for the Stop_String.

        For the attached sample input file, the output should start at line 18 and continue through line to 29

        Thanks PhilD

        [attachment deleted by admin to conserve space]

        Hackoo



          Hopeful
        • Thanked: 42
        • Experience: Expert
        • OS: Windows 10
        Re: remove lines in txt files
        « Reply #19 on: April 27, 2017, 07:39:07 AM »
        Sorry, i'm busy so i can't reply to you quickly !
        Just give a try for this modification :
        Code: [Select]
        @echo off
        Title Search for files and remove lines in text files
        mode con cols=75 lines=5 & Color 0A
        REM We set the variable Folder with the function Browse4Folder
        Call :Browse4Folder "Choose source folder to scan for files" "c:\scripts"
        Set "Folder=%Location%"
        Rem if the variable %Folder% have a trailing back slash, we remove it !
        IF %Folder:~-1%==\ SET "Folder=%Folder:~0,-1%"
        If "%errorlevel%" EQU "0" (
        echo( & echo(
        echo You choose this folder "%Folder%"
        Timeout /T 2 /nobreak>nul
        ) else (
        echo( & echo(
        echo  "%Folder%"
        Timeout /T 2 /nobreak>nul & Exit
        )
        Set "Stop_String=Purpose"
        Set "Edited_Files=%HomeDrive%\Edited_Files"
        Set "Exceptions_Folder=%HomeDrive%\Exceptions"
        If not exist "%Edited_Files%" MD "%Edited_Files%"
        If not exist "%Exceptions_Folder%" MD "%Exceptions_Folder%"
        SetLocal EnableDelayedExpansion
        for /f "delims=" %%f in ('Dir /b /s "%Folder%\*.txt"') Do (
        Cls
        echo.
        set /A "Count=0"
        set "InputFile=%%~dpFf"
        findstr /I /C:"%Stop_String%" "!InputFile!" >nul 2>&1
        If "!ErrorLevel!" EQU "0" (
        Call :Counting "%Stop_String%" "!InputFile!"
        echo We found the string "%Stop_String%" at line number !Count! on "!InputFile!"
        Timeout /T 4 /nobreak>nul
        Set "OutPutFile=%Edited_Files%\%%~nxf"
        if exist "!OutPutFile!" Del "!OutPutFile!"
        Call :Write2File "!InputFile!" "!OutPutFile!"
        ) else (
        Call :Copyfiles "!InputFile!" "!Exceptions_Folder!"
        )
        )
        Rem Explorer "%Edited_Files%" & exit
        echo(
        set "Title=Search for files and remove lines in text files"
        set "Msg=%Title%\nEnd of the script"
        Call :Speak "%Msg%"
        Rem 64=vbInformation, 48=vbExclamation, 16=vbCritical 32=vbQuestion
        Call :MsgBox "%Msg%" 64 "%Title%"
        exit
        ::********************************************************************
        :Counting <Stop_String> <InputFile>
        For /f "tokens=1 delims=:" %%A in ('findstr /I /N "%~1" "%~2"') Do (
        Set /a "Count=%%A"
        )
        exit /b
        ::********************************************************************
        :Write2File <InputFile> <OutPutFile>
        For /f "skip=%Count% delims=*" %%a in ('Type "%~1"') do (
        echo %%a >> "%~2"
        )
        exit /b
        ::********************************************************************
        :Browse4Folder
        set Location=
        set vbs="%temp%\_.vbs"
        set cmd="%temp%\_.cmd"
        for %%f in (%vbs% %cmd%) do if exist %%f del %%f
        for %%g in ("vbs cmd") do if defined %%g set %%g=
        (
            echo set shell=WScript.CreateObject("Shell.Application"^)
            echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
            echo if typename(f^)="Nothing" Then
            echo wscript.echo "set Location=Dialog Cancelled"
            echo WScript.Quit(1^)
            echo end if
            echo set fs=f.Items(^):set fi=fs.Item(^)
            echo p=fi.Path:wscript.echo "set Location=" ^& p
        )>%vbs%
        cscript //nologo %vbs% > %cmd%
        for /f "delims=" %%a in (%cmd%) do %%a
        for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
        for %%g in ("vbs cmd") do if defined %%g set %%g=
        goto :eof
        ::********************************************************************
        :Copyfiles <Source> <Target>
        Copy /Y "%~1" "%~2" >nul 2>&1
        goto :eof
        ::********************************************************************
        :MsgBox <Msg> <Type> <Title>
        echo MsgBox Replace("%~1","\n",vbCrLf),"%~2","%~3" > "%tmp%\%~n0.vbs"
        Cscript /nologo "%tmp%\%~n0.vbs" & Del "%tmp%\%~n0.vbs"
        exit /b
        ::********************************************************************
        :Speak <msg>
        (
            echo Set sapi=Createobject("sapi.spvoice"^)
            echo sapi.Speak("%~1"^)
        )>"%tmp%\%~n0.vbs"
        Cscript /nologo "%tmp%\%~n0.vbs"
        Del "%tmp%\%~n0.vbs"
        exit /b
        ::*********************************************************************

        PhilD

          Topic Starter


          Rookie

          • Experience: Experienced
          • OS: Windows 7
          Re: remove lines in txt files
          « Reply #20 on: April 27, 2017, 02:32:20 PM »
          Sorry, i'm busy so i can't reply to you quickly !
          Just give a try for this modification :
          Hi Hackoo

          Thanks for the changes, but the issue that really matters is the one I tried to explain in my last post. 

          The attached zip contains a Sample .txt file which has "Purpose:" at lines 15 and 21

          And a Sample Expected.txt file that I created using a text editor, in which I removed lines 1-15 but retained everything after that, including line 21

          So once the first Stop_String is found, the program can write the rest of the input file to the output file, and move on to the next file.

          I hope that makes sense

          PhilD



          [attachment deleted by admin to conserve space]
          « Last Edit: April 27, 2017, 02:46:37 PM by PhilD »

          PhilD

            Topic Starter


            Rookie

            • Experience: Experienced
            • OS: Windows 7
            Re: remove lines in txt files
            « Reply #21 on: April 27, 2017, 05:07:01 PM »
            Hackoo - on the Sample.txt I never see see the message
            Code: [Select]
            We found the string Purpose at [b]line number 15[/b] on Sample.txt
            but I do see the message
            Code: [Select]
            We found the string Purpose at [b]line number 21[/b] on Sample.txt
            I assumed I would see both   ???

            PhilD