2014-04-23 111 views
0

謝謝,隨着你的提示我已經調整了我的主批處理文件,但變量TARG_FILE沒有改變。 我試圖從FileList_for_Import.txt包含數據替換查找特殊字符字符串 - 批量

file1.txt 
file2.txt 

`

Cls 
DIR /o-d /b D:\Data\All_Inputfiles\*.* > D:\Data\FileList_for_Import.txt 

REM Start Loop thru the file names that are available in FileList_for_Import.txt 
REM Loop thru file 

SETLOCAL EnableDelayedExpansion 
@echo off 
Set AllInputFile= D:\Data\FileList_for_Import.txt 
SET INTEXTFILE=C:\DataLoader\Load.xml 
set OUTTEXTFILE=D:\Data\tmp_out.txt 
SET BackupPath=D:\Data\BackupLoad.xml 
Set TempFile=D:\Data\tmp.txt 
SET DbgFile=D:\Data\debuginfo.txt 
Rem Remove Debug files 
    Del !DbgFile! 
REM Del D:\Data\debugerr.txt 
ECHO " Start ">!DbgFile! 
ECHO !AllInputFile!>>!DbgFile! 

ECHO " "###########Start of MAIN OF LOOP ############" 
REM pause 
for /f "tokens=*" %%a in (!AllInputFile!) do (  
    set "TARG_FILE=%%a" 
    REM ECHO line=%%a "TARG_FILE "!TARG_FILE!>>!DbgFile! 

    REM Get the 2nd line from the debugpayload file 
    REM for /f "tokens=1*delims=:" %%G in ('findstr /n "^" C:\DataLoader\Load.xml') do if %%G equ 2 set "DbgLine=%%H" 
    REM ECHO " "DbgLine "!DbgLine!>>!DbgFile! 
    Del !TempFile! 
    REM Copy !INTEXTFILE! !BackupPath! 
    findstr /g "InFilePath" !INTEXTFILE!>!TempFile! 
    REM set /p "DbgLine="< !TempFile! 
    set /p DbgLine=< !TempFile! 
    ECHO "DbgLine "!DbgLine!>>!DbgFile! 
    SET PREVFILE_NM=!DbgLine:~75,-13! 
    ECHO "PREVFILE_NM "!PREVFILE_NM! "TARG_FILE " !TARG_FILE! "Before Replace FinalreplaceLine "!FinalreplaceLine!>>!DbgFile! 

    set FinalreplaceLine=!DbgLine! 
    REM ECHO "Before Replace FinalreplaceLine "!FinalreplaceLine!>>!DbgFile! 
    set FinalreplaceLine=!FinalreplaceLine:%PREVFILE_NM%=%TARG_FILE%! 
    ECHO "FinalreplaceLine "!FinalreplaceLine!>>!DbgFile! 

    ) 

` 輸出這批是 !TARG_FILE的文件名!隨每次迭代越來越爲

file1.txt 
file2.txt 

,但與下面的語句替換它在第一次迭代和第二次迭代它的值不會改變犯規更換FILE1.TXT。

set FinalreplaceLine=!FinalreplaceLine:%PREVFILE_NM%=%TARG_FILE%! 

在此先感謝您的幫助或寶貴建議。

+0

檢查我的編輯環路問題。 – SachaDee

回答

0

你的代碼在我的電腦上工作? 但你可以嘗試這樣的:

@echo off 
SETLOCAL EnableDelayedExpansion 
SET DbgFile=D:\Data\debuginfo.txt 

::*******REM This block doesnt work******** 
set string="  <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>" 
    set FinalreplaceLine=!string! 
    set PREVFILE_NM=oldfile.txt 
    set TARG_FILE=newfile.txt 
    ECHO "Before Replace FinalreplaceLine "!FinalreplaceLine!>!DbgFile! 
    set FinalreplaceLine=!FinalreplaceLine:%PREVFILE_NM%=%TARG_FILE%! 
    ECHO "FinalreplaceLine "!FinalreplaceLine!>>!DbgFile! 

編輯:

我做一個文件Filelist.txt中與

toto.txt 
titi.txt 
tata.txt 

這將是fileold.txt的替代名稱。

@echo off 
SETLOCAL EnableDelayedExpansion 
SET DbgFile=D:\Data\debuginfo.txt 

for /f "delims=" %%a in (FileList.txt) do call:replace "%%a" 
exit/b 

:replace 

set string="  <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>" 
    set FinalreplaceLine=!string! 
    set PREVFILE_NM=oldfile.txt 
    set TARG_FILE=%~1 
    ECHO "Before Replace FinalreplaceLine "!FinalreplaceLine!>>!DbgFile! 
    set FinalreplaceLine=!FinalreplaceLine:%PREVFILE_NM%=%TARG_FILE%! 
    ECHO "FinalreplaceLine "!FinalreplaceLine!>>!DbgFile! 

產出DebugInfo.txt:

"Before Replace FinalreplaceLine ""  <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>" 
"FinalreplaceLine ""  <InFilePath>D:\Data\All_Inputfiles\toto.txt</InFilePath>" 
"Before Replace FinalreplaceLine ""  <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>" 
"FinalreplaceLine ""  <InFilePath>D:\Data\All_Inputfiles\titi.txt</InFilePath>" 
"Before Replace FinalreplaceLine ""  <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>" 
"FinalreplaceLine ""  <InFilePath>D:\Data\All_Inputfiles\tata.txt</InFilePath>"