Extracting string from any non-binary file irrespective of its location within file

前端 未结 2 938
臣服心动
臣服心动 2020-12-12 05:57

OK, here is a problem I have been unsuccessfully trying to cope with, writing a batch script. Suppose I have a file containing, say, some youtube addresses (for example a ht

相关标签:
2条回答
  • 2020-12-12 06:33

    I'll use another scripting language as Bat to do that. Here I made a little exemple in Autoit :

    StringBetween.au3

    #include <String.au3>
    Local $hOutFile=FileOpen("output.txt",2)
    Local $hTexte=FileRead($CmdLine[1])
    $AFind=_StringBetween($hTexte,$cmdline[2],$cmdline[3])
    For $i= 0 To UBound($Afind)-1 step 1
       FileWrite($hOutFile,$AFind[$i]&@crlf)
    Next
    FileClose($hOutFile)
    

    You can compile it yourself or Download it already compiled here :

    StringBetween.rar

    Usage :

    Stringbetween [InPutFile] [StringRight] [StringLeft]

    Ouput : "Output.txt"

    In your case :

    Stringbetween.exe "example.html" "<a href=" ">"
    

    A file "Output.txt" will be created with :

    https://www.youtube.com/watch?v=9bZkp7q19f0
    https://www.youtube.com/watch?v=kYtGl1dX5qI&list=RD9bZkp7q19f0
    https://www.youtube.com/watch?v=lWA2pjMjpBs&list=RD9bZkp7q19f0
    
    0 讨论(0)
  • 2020-12-12 06:40

    Thank you for your quick response. It really helped a lot. I am impressed very much.

    I have never used AutoIt and now I see it is a useful utility indeed! I have downloaded the program and had big fun trying it. I like the huge library of functions (although their being scattered all over various scripts makes them slightly messy and unintuitive to find) and particularly the ability to compile script code into executable files. I will certainly use it in future, too.

    I have slightly modified your script, to be able to process many files from one directory at a time. This is what it looks like right now:

    #include <String.au3>
    #include <File.au3>
    #include <Array.au3>
    #include <MsgBoxConstants.au3>
    #include <WinAPIFiles.au3>
    
    ;Parameters:
    Local $Ldelimiter, $Rdelimiter, $Filter, $Outputfilename
    
    ;Prompt for parameters if not stated in command line:
    If $CmdLine[0] < 1 Then
    $Ldelimiter=InputBox("","Enter the left delimiter :","") 
    Else
       $Ldelimiter=$CmdLine[1]
       EndIf
    If $CmdLine[0] < 2 Then
    $Rdelimiter=InputBox("","Enter the right delimiter :","") 
    Else
       $Rdelimiter=$CmdLine[2]
    EndIf
    If $CmdLine[0] < 3 Then
    $Filter=InputBox("","Enter the filter mask :","*.*") 
    Else
       $Filter=$CmdLine[3]
    EndIf
    If $CmdLine[0] < 4 Then
    $Outputfilename=InputBox("","Enter the name of output file :","output.txt") 
    Else
       $Outputfilename=$CmdLine[4]
    EndIf
    
    Local $hOutFile=FileOpen($Outputfilename,2) ;Open output file
    Local $curpath=_WinAPI_GetCurrentDirectory()    ;Get current directory
    
    Local $FileList=_FileListToArray($curpath,$Filter,1)    ;Make an array with the list of files to process
    
    For $k= 1 To UBound($FileList)-1 step 1 ;Process a file from the list
        Local $hTexte=FileRead($FileList[$k])   ;Read file content
        $AFind=_StringBetween($hTexte,$Ldelimiter,$Rdelimiter)  ;Make an array with the list of strings to be found
        For $i= 0 To UBound($Afind)-1 step 1    ;Get a string from the list
            FileWrite($hOutFile,$Ldelimiter&$AFind[$i]&$Rdelimiter&@crlf)   ;Write the string to output file
        Next
    Next
    FileClose($hOutFile)
    
    exit
    

    Usage:

    Stringbetween [StringLeft] [StringRight] [FileMask] [OutputFile]

    If you fail to provide parameters in command line, the program will prompt for them. FileMask is *.* by default (all files in directory will be processed). I have also added the left and right delimiter to the output.

    Regards

    PS: I still wonder if it is possible to do the same with simple BAT.

    0 讨论(0)
提交回复
热议问题