2014-09-29 67 views
0

我希望你能帮助解释问题是什么。像大多数问题一样,我相信对于那些经常使用批处理脚本的人来说,这非常简单。批处理文件文件输出,字符串操作

我试图解决的挑战是从文件夹中获取XML文件列表,将它们传递到Sha1 exe文件,接收输出,修改它,合并结果,然后将结果输出到文件。我希望我不必输出到两个文件,但我无法将Sha1输出到一个变量来操纵它。

什么问题?目前,当文本输出回来时,它看起来像“size = size:3”,当我尝试和操纵字符串时,它会混淆输出。下面的例子。

验证码:

set "xmlfilelocation=C:\Users\ADMIN\Documents\test" 
set "manifestfile=C:\Users\ADMIN\Documents\manifest.txt" 
set "Sha1=C:\Users\ADMIN\\sha1.exe" 
set "sha1output=C:\Users\ADMIN\Documents\sha1output.txt" 

) 
if exist %xmlfilelocation% (
    for /f %%f in ('dir /b %xmlfilelocation%') do (

     rem Takes the file name which is currently been passed to the Sha1 engine 
     set filename = %%f 

     rem Run the Sha1 command 
     C:\Users\IBM_ADMIN\Documents\Sha1.exe -r %xmlfilelocation%\%%f >> %sha1output% 

     rem Waits _for the current Sha1 execution to be run. 
     PING 1.1.1.1 -n 1 -w 1000 >NUL 
    ) 
) else (
    echo No folder found 
) 

::read %THECSVFILE% and loop through each line 
for /F "usebackq tokens=* delims= " %%A in (%sha1output%) do (
    set the_line=%%A 
    call :process_line 
) 
goto TheEnd 

:process_line 
for /F "usebackq tokens=1,2,3,4,5 delims= " %%1 in ('%the_line%') do (
    set OUTPUTLINE=name=%%2 sha1=%%3 size=%%4 url=%xmlfilelocation%\%%2 
    echo %OUTPUTLINE% >> %manifestfile% 
) 

rem del = %sha1output% 

的字符串操作之前,会显示该文件正确除了SHA1 = SHA1:这应该只阅读SHA1 =和大小=:应阅读只是大小=其中,操纵意味着解决。

name=New6.xml sha1=sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size=size:0 url=C:\Users\IBM_ADMIN\Documents\test\New6.xml 
name=New1.xml sha1=sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size=size:0 url=C:\Users\IBM_ADMIN\Documents\test\New1.xml 
name=New2.xml sha1=sha1:f10e2821bbbea527ea02200352313bc059445190 size=size:3 url=C:\Users\IBM_ADMIN\Documents\test\New2.xml 
name=New3.xml sha1=sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size=size:0 url=C:\Users\IBM_ADMIN\Documents\test\New3.xml 
name=New4.xml sha1=sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size=size:0 url=C:\Users\IBM_ADMIN\Documents\test\New4.xml 
name=New5.xml sha1=sha1:f10e2821bbbea527ea02200352313bc059445190 size=size:3 url=C:\Users\IBM_ADMIN\Documents\test\New5.xml 

当我在其他行添加到操作字符串:

:process_line 
for /F "usebackq tokens=1,2,3,4,5 delims= " %%1 in ('%the_line:,=~%') do (
    set sha1=%%3 
    set size=%%4 
    set url=%xmlfilelocation%\%%2 
    set THISLINE=name=%%2 sha1=%sha1:~5% size=%size:~5% url=%url% 
    rem echo The Line: %OUTPUTLINE% 
    echo %OUTPUTLINE% >> %manifestfile% 
) 

返回一个文件,其中输出名称不能正常显示。通常,由于某种原因,第一个或最后一个文件Sha1值将作为目录输出。这只发生在我尝试和操纵字符串之后。

name=New6.xml sha1=f10e2821bbbea527ea02200352313bc059445190 size=3 url=C:\Users\IBM_ADMIN\Documents\test\New5.xml 
name=New1.xml sha1=ers\IBM_ADMIN\\sha1.exe size=0 url=C:\Users\IBM_ADMIN\Documents\test\New6.xml 
name=New2.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\IBM_ADMIN\Documents\test\New1.xml 
name=New3.xml sha1=f10e2821bbbea527ea02200352313bc059445190 size=3 url=C:\Users\IBM_ADMIN\Documents\test\New2.xml 
name=New4.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\IBM_ADMIN\Documents\test\New3.xml 
name=New5.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\IBM_ADMIN\Documents\test\New4.xml 

Sha1输出的一个示例,这是Sha1.exe输出的内容,没有对我进行任何操作。

prefetch New1.xml sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size:0 http://EXAMPLEURL/REPLACEME.exe 
prefetch New2.xml sha1:f10e2821bbbea527ea02200352313bc059445190 size:3 http://EXAMPLEURL/REPLACEME.exe  
prefetch New3.xml sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size:0 http://EXAMPLEURL/REPLACEME.exe  
prefetch New4.xml sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size:0 http://EXAMPLEURL/REPLACEME.exe  
prefetch New5.xml sha1:f10e2821bbbea527ea02200352313bc059445190 size:3 http://EXAMPLEURL/REPLACEME.exe  
prefetch New6.xml sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709 size:0 http://EXAMPLEURL/REPLACEME.exe 

最终输出应该是什么样子的6个文件是:

name=New6.xml sha1=f10e2821bbbea527ea02200352313bc059445190 size=3 url=C:\Users\ADMIN\Documents\test\New5.xml 
    name=New1.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\ADMIN\Documents\test\New6.xml 
    name=New2.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\ADMIN\Documents\test\New1.xml 
    name=New3.xml sha1=f10e2821bbbea527ea02200352313bc059445190 size=3 url=C:\Users\ADMIN\Documents\test\New2.xml 
    name=New4.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\ADMIN\Documents\test\New3.xml 
    name=New5.xml sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709 size=0 url=C:\Users\ADMIN\Documents\test\New4.xml 
+0

明确你所需要的输出文本的样子,给出的SHA1输出。 – foxidrive 2014-09-30 00:30:53

+0

当然是的,正如你从上面看到的,大部分都是正确的,只是在第一个例子中名称是错误的,第二个是Sha1值是目录路径。它应该是,name = file.xml sha1 = sha1value size = number URL =文件路径 – QuinsUK 2014-09-30 05:55:25

+0

你已经显示了'字符串操作之前'和'Sha1输出的一个例子',它们是不一样的。目前还不清楚输入是什么以及期望的输出 - 因为可以使用不同的脚本/工具来获得结果。 – foxidrive 2014-09-30 06:36:20

回答

0

这确实你显示什么:

这将使用名为repl.bat一个辅助批处理文件(由dbenham) - 从下载地址:https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

repl.bat放置在与批处理文件相同的文件夹中或位于路径上的文件夹中。

输入文件是file.txt和输出文件newfile.txt

@echo off 
type "file.txt" | repl "prefetch (.*?) sha1:(.*?) size:(.).*" "name=$1 sha1=$2 size=$3 url=C:\Users\IBM_ADMIN\Documents\test\$1" >"newfile.txt" 
pause 
+0

谢谢,这已经解决了我想要做的事情。 – QuinsUK 2014-10-01 11:05:09

相关问题