FOR cmd命令修改文件

时间:2014-01-16 15:38:00

标签: batch-file cmd

我要做的是以下内容:

_我有多个文本INPUT文件,例如:

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

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

_ OUTPUT应该是:

                lista.txt

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

我尝试使用以下代码来实现结果: (我记得“FOR”二次,一次是GRUPPI = auditors.txt,一次是GRUPPI =备份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

有人可以给我一个暗示吗?

克里斯蒂安

2 个答案:

答案 0 :(得分:1)

仔细阅读FOR文档(help forfor /?)。您将看到USEBACKQ选项,它可以更改IN()子句的解释方式。以下内容可以帮助您:

for /F "usebackq tokens=* delims=," %%a in ("%GRUPPI%.txt") do( ...

答案 1 :(得分: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