Batch file 从任何非二进制文件中提取字符串,而不考虑其在文件中的位置

Batch file 从任何非二进制文件中提取字符串,而不考虑其在文件中的位置,batch-file,Batch File,好的,我一直试图解决一个问题,写一个批处理脚本,但没有成功。假设我有一个包含(比如)一些youtube地址的文件(例如一个链接到youtube页面的html文件) 文件的内容可能如下所示: Blaaaa blaa blaa blaa blaa <a href=https://www.youtube.com/watch?v=9bZkp7q19f0>Gangnam1</a> blaaa blaa <a href=https://www.youtube.com/watch

好的,我一直试图解决一个问题,写一个批处理脚本,但没有成功。假设我有一个包含(比如)一些youtube地址的文件(例如一个链接到youtube页面的html文件)

文件的内容可能如下所示:

Blaaaa blaa
blaa blaa blaa <a href=https://www.youtube.com/watch?v=9bZkp7q19f0>Gangnam1</a> blaaa blaa
<a href=https://www.youtube.com/watch?v=kYtGl1dX5qI&list=RD9bZkp7q19f0>Scream and shout</a> blaa blaa
blaaaaa <a href=https://www.youtube.com/watch?v=lWA2pjMjpBs&list=RD9bZkp7q19f0>Diamonds</a> blaa
blaa bla bla
(或类似的东西)

保存在另一个文件中的输出应如下所示:

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>
搜索当然也会考虑其他字符串,而不仅仅是与youtube相关的字符串

不能使用像FIND或FINDSTR这样的简单命令,因为它们返回包含字符串的整行。类似地,对于with标记和分隔符,这里似乎没有什么用处,因为要查找的字符串不规则地分散在文件中,有时在同一行中有几个字符串


我真的不知道如何解决这个问题。这可能看起来很简单,但我从未找到一个脚本或程序,可以提供这样的输出。也许甚至有一个现成的编译程序来做这件事。如果有任何帮助,我将不胜感激。

我将使用另一种脚本语言作为Bat来实现这一点。 在这里,我在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)

谢谢你的快速回复。这真的帮了大忙。我印象深刻

我从未使用过AutoIt,现在我看到它确实是一个有用的工具!我已经下载了这个程序,并在尝试中获得了巨大的乐趣。我喜欢庞大的函数库(尽管它们分散在各种脚本中,使得它们有点杂乱无章,难以找到),尤其是将脚本代码编译成可执行文件的能力。我将来肯定也会使用它

我稍微修改了您的脚本,以便能够一次处理一个目录中的多个文件。这就是它现在的样子:

#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
#包括
#包括
#包括
#包括
#包括
;参数:
本地$Ldelimiter、$Rdelimiter、$Filter、$Outputfilename
;如果命令行中未说明,则提示输入参数:
如果$CmdLine[0]<1,则
$Ldelimiter=InputBox(“,”输入左分隔符:“,”)
其他的
$ldeliflimiter=$CmdLine[1]
恩迪夫
如果$CmdLine[0]<2,则
$Rdelimiter=InputBox(“,”输入右分隔符:“,”)
其他的
$rdeLimitor=$CmdLine[2]
恩迪夫
如果$CmdLine[0]<3,则
$Filter=InputBox(“,”输入筛选器掩码:“,”***”)
其他的
$Filter=$CmdLine[3]
恩迪夫
如果$CmdLine[0]<4,则
$Outputfilename=InputBox(“,”输入输出文件的名称:“,”output.txt”)
其他的
$Outputfilename=$CmdLine[4]
恩迪夫
本地$hOutFile=FileOpen($Outputfilename,2);打开输出文件
本地$curpath=\u WinAPI\u GetCurrentDirectory();获取当前目录
本地$FileList=\u FileListToArray($curpath,$Filter,1);使用要处理的文件列表创建一个数组
对于$k=1到UBound($FileList)-1步骤1;处理列表中的文件
本地$hTexte=FileRead($FileList[$k]);读取文件内容
$AFind=_StringBetween($hTexte、$ldeliflimiter、$rdeliflimiter);使用要查找的字符串列表创建一个数组
对于$i=0到UBound($Afind)-1步骤1;从列表中获取字符串
文件写入($hOutFile、$Ldelimiter和$AFind[$i]&$Rdelimiter和@crlf);将字符串写入输出文件
下一个
下一个
文件关闭($hOutFile)
出口
用法:

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

如果无法在命令行中提供参数,程序将提示输入这些参数。默认情况下,文件掩码为**(将处理目录中的所有文件)。我还向输出中添加了左分隔符和右分隔符

问候


PS:我仍然想知道是否可以用简单的BAT做同样的事情。

sachadee的答案似乎很有用,而使用
repl.BAT的正则表达式可以是另一种解决方案。
Stringbetween.exe "example.html" "<a href=" ">"
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
#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