2014-01-16 99 views
1

我有什麼做的是後續:FOR cmd命令來修改文件

_I有多個文本輸入文件例如:

Auditors.txt made like that   Backup users.txt made like taht 

    "Auditors"       "Backup Users", 
    "Auditor","3"      "Backup","1" 
    "pippo","20" 

_The輸出應該是:

   lista.txt 

       Auditors,Auditor,3,pippo,20 
       Backup Users,Backup,1 

我試着用下面的代碼來實現結果: (我記得2次「FOR」cicle,一次GRUPPI = auditors.txt,一次GRUPPI = Backup USERS.txt)

... 
    for /F "tokens=* delims=," %%a in ("%GRUPPI%.txt") do(
    echo|set /p=%%a >> lista.txt 
    echo|set /p=, >> lista.txt) 
    pause 
    ... 

它沒有打開輸入文件來讀取線路! (考慮也「備份用戶」有問題創造一個空間)

結果: 如果FOR參數設置爲(%GRUPPI%.TXT) - >它甚至不產生輸出文件LISTA。 txt

如果FOR參數設置爲(「%GRUPPI%.txt」) - >它會生成文件lista.txt,但在其中我只找到輸入文件名!

如果我用 「有usebackq」 和( 「%GRUPPI%.TXT」) - >它甚至不產生輸出文件lista.txt

有人可以給我一個提示嗎?

克里斯蒂安

回答

1

閱讀FOR仔細documenation(help forfor /?)。您將看到USEBACKQ選項改變了IN()子句的解釋方式。下面應該讓你對你的方式:

for /F "usebackq tokens=* delims=," %%a in ("%GRUPPI%.txt") do(... 
0
@ECHO OFF 
SETLOCAL 
DEL newfile.txt 2>nul 
FOR /f "delims=" %%a IN (q21166181.txt) DO (
SET "gruppi=%%a" 
CALL :process 
) 

GOTO :EOF 
:process 
SET "line=" 
FOR /f "usebackqdelims=" %%c IN ("%gruppi%") DO (
CALL :build %%c 
) 
>>newfile.txt ECHO(%line:~1% 
GOTO :eof 

:build 
SET "$1=%~1" 
IF NOT DEFINED $1 GOTO :EOF 
SET "line=%line%,%$1%" 
shift 
GOTO build 

如果我有你想要的東西的權利,這可能適合。

q21166181.txt包含

auditors.txt 
backup users.txt 

auditors.txt包含

"Auditors"  
    "Auditor","3" 
    "pippo","20" 

backup users.txt包含

"Backup Users", 
    "Backup","1" 

結果在newfile.txt包含

Auditors,Auditor,3,pippo,20 
Backup Users,Backup,1