2014-09-29 148 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

相關問題