2013-01-23 148 views
0

我有一個批處理腳本,它應該處理多個輸入文件,併爲每個輸入文件創建一個單獨的輸出文件。不過,我的問題是:處理多個.txt文件的批處理腳本只處理一個

  1. 它僅讀取第一個文本文件
  2. 它會進入一個無限循環創造了第一個輸出文件之後。

我需要的腳本做出單獨的輸出文件每個輸入文件。我怎樣才能做到這一點?

代碼當前如下:

@echo off 
setlocal enabledelayedexpansion 
set line=0 
for /r %%x in (*.txt) do (     //SUPPOSED to read all input files 
for /f "tokens=4 delims=|" %%a in (%%x) do (... goto: GETLINE)) 

:GETLINE 
if not %line%==0 set skip=skip=%line% 
for %%x in (*.txt) do (... 
echo %%b >>"Output_%%x.txt"       //writing into output 
goto :BREAK 
)) 
:BREAK 

回答

1

我認爲這個問題是在:GETLINE功能。

如果這被稱爲你正在循環所有文件,並再次讀取每一行。

嘗試使用這個代替

@echo off 
setlocal enabledelayedexpansion 
set line=0 
for /r %%x in (*.txt) do (
for /f "tokens=4 delims=|" %%a in (%%x) do (
set num=%%a 
set num=!num: =! 
if !num!==589 set bool=true 
if !num!==581 set bool=true 
if !num!==580 set bool=true 
if !num!==027 set bool=true 
if !num!==582 set bool=true 
if !num!==585 set bool=true 
if "!bool!"=="true" call :GETLINE "%%x" 
set /a line+=1 
set bool=false 
) 
) 

:GETLINE 
if not %line%==0 set skip=skip=%line% 
for /f "usebackq %skip% tokens=* delims=" %%b in ("%~1") do (
echo %%b >>"Output_%%x.txt" 
goto :BREAK 
) 
) 
:BREAK 

它將調用:GETLINE和傳遞當前文件中的循環,這將再次循環都停止。

+0

這沒有工作巴厘島..目錄是不正確的或東西。嘗試了'set file_list = C:\ BatchScripting \ *。txt for %% x in(%file_list%)do('但是然後沒有輸出。 – Shalapolia

+0

啊,我刪除了我的答案中的另一個for循環,輸出行'%% x'不再存在,試圖將其更改爲'echo %% b >>「Output_%1'.txt」'而不是。 –