我希望你能帮助解释问题所在。 像大多数问题一样,我确信这对于经常使用Batch Scripts的人来说非常简单。
我要解决的挑战是从文件夹中获取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 =和size = size:应该只读取尺寸=操作要解决的问题。
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.xmlSha1输出的一个示例,这是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.exe6个文件的最终输出应该是什么样的:
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.xmlI am hoping that you can help and explain what the problem is. Like most problems I am sure that this will be very simple to some who uses Batch Scripts regularly.
The challenge which I am trying to solve is to take a list of XML files from a folder, pass them into a Sha1 exe, receive the output, modify it, combine the result and then finally output the result to a file. I was hoping that I didn't have to output to two files but I can't get the Sha1 output to a variable to manipulate it.
What is the problem? Currently when the output of the text comes back in it looks like "size= size:3" and when I try and manipulate string it messes with the output. Example below.
The Code:
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%Before the string manipulation, the file is displayed correctly apart from the sha1=sha1: which should read just sha1= and the size=size: should read just size= which the manipulation is meant to solve.
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.xmlWhen I add in the additional lines to manipulate the string:
: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% )Returns a file where the output name doesn't display properly. Normally the first or last file, the Sha1 value, for some reason is outputted as a directory. This only happens after I try and manipulate the string.
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.xmlAn example of the Sha1 output, this is what the Sha1.exe outputs with no manipulation from me.
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.exeWhat the final output should look like for the 6 files is:
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最满意答案
这就是你所展示的:
这使用名为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" pauseThis does what you have shown:
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
The input file is file.txt and the output file is 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更多推荐
发布评论