批处理文件文件输出,字符串操作

时间:2014-09-29 19:38:28

标签: string batch-file

我希望你能帮助解释问题所在。像大多数问题一样,我确信这对于经常使用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.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

1 个答案:

答案 0 :(得分: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