2012-10-25 45 views
2

有人請幫忙...哦,我會很感激。我有一個非常長的批處理文件,除了每當用戶輸入輸入時,它都會替換所需文件中的字符串,但是它將刪除文件中的!s,這些文件由於它們是XML配置文件而引起問題,並且這些文件被註釋需要留下的部分。除非要求,否則我不會把整個代碼放在這裏,但是在一個堅果外殼中,用戶進行某些輸入,然後批處理文件運行......這是一個文件的代碼的一部分....用戶進入驅動器安裝信件和bdi服務器的名稱。我希望用戶輸入更換%驅動器%和%bdi1%......這確實....但我不希望它來代替註釋掉部分...即:感嘆號不斷在批處理文件中被刪除

<!-- Tcp message preamble and postamble are flags that mark the beginning and end of an HL7 message. turns into <-- Tcp message preamble and postamble are flags that mark the beginning and end of an HL7 message. 

通知沒有!

這是我的代碼...我需要做些什麼來讓它停止刪除!我試着在這裏看,我認爲我很好的方式與傑布的答案,但我無法得到它的工作。在此先感謝

if exist newfile.txt del newfile.txt 
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a 
set str=!str:server_name=%server%! 
echo !str! >> newfile.txt 
) 
del importer.config 
rename newfile.txt importer.config 



if exist newfile.txt del newfile.txt 
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a 
set str=!str:bdi_name=%bdi1%! 
echo !str! >> newfile.txt 
) 
del importer.config 
rename newfile.txt importer.config 



if exist newfile.txt del newfile.txt 
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a 
set str=!str:share_name=%share%$! 
echo !str! >> newfile.txt 
) 
del importer.config 
rename newfile.txt importer.config 


if exist newfile.txt del newfile.txt 
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a 
set str=!str:drive_bdi=%drive%! 
echo !str! >> newfile.txt 
) 
del importer.config 
rename newfile.txt importer.config 

回答

7

這是延遲擴展和批解析器的影響。
如果延遲擴展被禁用,則感嘆號沒有問題,但是如果啓用瞭解析器,則假定!用於擴展變量,並且只有一個標記時,它將被刪除。

所以解決方案是禁用延遲擴展,但是當你需要它時,你也必須啓用它!

這可以通過簡單地在正確的時刻切換來完成。

setlocal DisableDelayedExpansion 
(
    for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
    set "str=%%a" 
    setlocal EnableDelayedExpansion 
    set "str=!str:server_name=%server%!" 
    set "str=!str:bdi_name=%bdi1%!" 
    set "str=!str:share_name=%share%$!" 
    set "str=!str:drive_bdi=%drive%!" 
    echo(!str! 
    endlocal 
) 
) > newfile.txt 

我將重定向移動到完整的FOR塊,速度更快,不需要先刪除文件。
我嘗試將所有替換件移動到一個塊中,因此您只需要只讀取一次該文件。

+0

幫助壓縮它,但不幸的是剛剛創建了一個newfile.txt,並沒有對原始文件做任何事情......並且新的file.txt看起來不正確 –

+0

_the新的file.txt看起來不正確不幸的是一個有點模糊。多一點信息可能會有用。順便說一句。批處理不會嘗試重命名/將newfile.txt複製到original.file,因爲這不是這裏的問題 – jeb

+0

好吧,np,我可以做重命名和所有....但newfile.txt看起來像這樣(摘錄): –