使用batch file为每个关键字实例创build一个换行符

我发现在系统上曾经是一个0天的Java漏洞利用程序,它给了我一个想法,即查看每个用户configuration文件中的Java .idx文件中是否存在可能指向0天威胁的可疑URL。

我从这开始:

for /d %%A in ("C:\Documents and Settings\*") do ( findstr /S "http://" "%%A\Application Data\Sun\Java\Deployment\cache\6.0\*" >> C:\temp\javaurls.txt) 

这会产生一个充满URL的文件,但是它们与很多控制信息和其他不起眼的东西混杂在一起。 我想要做的是为每个find的“http://”的小写实例创build一个换行符。 任何人都知道如何使用batch file中的命令行来做到这一点?

我知道“只要使用其他工具!” 答案可能很烦人。 但是,Windows批处理文件/命令行实用程序是如此糟糕,我认为这是值得研究其他选项,如果你必须做很多这样的东西。 除非你喜欢搞清楚没有记录的,非标准的,破坏的正则表达式 , 甚至对于最基本的功能 ,也需要hacky的解决方案 。

一些选项:

  • Perl的
  • 为Windows的Grep (一个更好的相当于findstr)
  • Cygwin (类似Linux的整个Linux环境,包括grep)。

这是一行Perl,你可以添加到你的批处理文件中,在每个http://之前添加一个换行符:

 perl -pi.bak -e "s|(http://)|\n$1|g" C:\temp\javaurls.txt 

但是,如果我从头开始,我会在Perl中完成整个工作,而不是使用批处理文件。

我一般都同意别人的看法 – 批处理文本是不好的。 使用其他语言或工具是您的任务的好主意。

但可以批量完成:-)

在每个html://之前插入一个换行符并不难

 @echo off setlocal disableDelayedExpansion :: The first FOR loop loads a quoted linefeed into %%L :: The blank line within the IN() clause is critical. Do not remove. for %%L in (^"^ ^") do ( for /d %%A in ("C:\Documents and Settings\*") do ( for /f "eol=: delims=" %%B in ( 'findstr /S "http://" "%%A\Application Data\Sun\Java\Deployment\cache\6.0\*"' ) do ( set "line=%%B" setlocal enableDelayedExpansion echo(!line:http://=%%~Lhttp://! endlocal ) ) ) >c:\temp\javaurls.txt 

但是只保留以http://开头的结果行,并且在地址变成真正的痛苦之前保存每个文件的名称。

 @echo off setlocal disableDelayedExpansion :: The first FOR loop loads a quoted linefeed into %%L :: The blank line within the IN() clause is critical. Do not remove. for %%L in (^"^ ^") do ( for /d %%A in ("C:\Documents and Settings\*") do ( for /f "tokens=1* delims=:" %%B in ( 'findstr /S "http://" "%%A\Application Data\Sun\Java\Deployment\cache\6.0\*"' ) do ( set "line=%%C" setlocal enableDelayedExpansion set "line=!line:http://=%%~Lhttp://!" for /f "delims=" %%D in ( '"cmd /v:on /c echo(^!line^!|findstr http://"' ) do ( if "!!" equ "" endlocal echo %%B: %%D ) ) ) ) >c:\temp\javaurls.txt exit /b