Batch file 需要解释“的行为”;“回声”;批处理文件中(windows)
我的问题是我可以回应我想要的,但不是我想要的方式。尽管如此,根据我编写/排列代码的方式,我会让echo表现出各种各样的行为,而不是我想要的方式。如果有人能解释一下发生了什么以及为什么会有这样的结果,这可能会帮助我理解应该如何更改代码以使其正确。以下是一些解决方案(限于5个)以及它的行为(循环涉及第二行的回音): 我要回显的矩阵元素是(2行,逗号分隔列)Batch file 需要解释“的行为”;“回声”;批处理文件中(windows),batch-file,command-line,cmd,Batch File,Command Line,Cmd,我的问题是我可以回应我想要的,但不是我想要的方式。尽管如此,根据我编写/排列代码的方式,我会让echo表现出各种各样的行为,而不是我想要的方式。如果有人能解释一下发生了什么以及为什么会有这样的结果,这可能会帮助我理解应该如何更改代码以使其正确。以下是一些解决方案(限于5个)以及它的行为(循环涉及第二行的回音): 我要回显的矩阵元素是(2行,逗号分隔列) 我想要什么 当它到达这个循环时,它只是退出并关闭命令窗口 :EchoData SET _var="" FOR /L %%H IN (0,1,%_
:EchoData
SET _var=""
FOR /L %%H IN (0,1,%_colonne%) DO (
if %%H equ 0 set _var=!_matrice[%_ligneAct%][%%H]!
set _help=!_var!
if %%H gtr 0 set _var=!_help!, !_matrice[%_ligneAct%][%%H]!
)
echo !_var!>>%_fileOut%
:EchoData
SET _var=""
FOR /L %%H IN (0,1,%_colonne%) DO (
echo|set /p=!_matrice[%_ligneAct%][%%H]!>>%_fileOut%
if not %%H equ %_colonne% echo|set /p=,>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
)
输出看起来像
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
1 , 2517 , 1000008332 , 20160831 , 201608 , NA , NA , 0 , 17 , 0
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
1 , 2517 , 1000008332 , 20160831 , 201608 , NA , NA , 0 , 17 , 0
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
, , , , , , , , ,
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
:EchoData
SET _var=""
FOR /L %%H IN (0,1,%_colonne%) DO (
if %%H equ 0 set _var=!_matrice[%_ligneAct%][%%H]!
set _help=!_var!
if %%H gtr 0 set _var=!_help!, !_matrice[%_ligneAct%][%%H]!
echo !_var!
echo|set /p=!_matrice[%_ligneAct%][%%H]!>>%_fileOut%
if not %%H equ %_colonne% echo|set /p=,>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
)
echo !_var!>>%_fileOut%
输出看起来像
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
1 , 2517 , 1000008332 , 20160831 , 201608 , NA , NA , 0 , 17 , 0
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
1 , 2517 , 1000008332 , 20160831 , 201608 , NA , NA , 0 , 17 , 0
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
, , , , , , , , ,
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
:EchoData
SET _var=""
FOR /L %%H IN (0,1,%_colonne%) DO (
if %%H equ 0 set _var=!_matrice[%_ligneAct%][%%H]!
set _help=!_var!
if %%H gtr 0 set _var=!_help!, !_matrice[%_ligneAct%][%%H]!
echo !_var!
if not %%H equ %_colonne% echo|set /p=,>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
if %%H equ %_colonne% echo|set /p=!_var!>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
)
echo !_var!>>%_fileOut%
输出看起来像
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
1 , 2517 , 1000008332 , 20160831 , 201608 , NA , NA , 0 , 17 , 0
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
1 , 2517 , 1000008332 , 20160831 , 201608 , NA , NA , 0 , 17 , 0
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
, rim_no , account_no , observation_date , observation_month , start_date , maturity_date , days_past_due , rate , spread
, , , , , , , , ,
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
1, 2517, 1000008332, 20160831, 201608, NA, NA, 0, 17, 0
:EchoData
SET _var=""
FOR /L %%H IN (0,1,%_colonne%) DO (
if %%H equ 0 set _var=!_matrice[%_ligneAct%][%%H]!
set _help=!_var!
if %%H gtr 0 set _var=!_help!, !_matrice[%_ligneAct%][%%H]!
echo !_var!
::if not %%H equ %_colonne% echo|set /p=,>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
if %%H equ %_colonne% echo|set /p=!_var!>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
)
echo !_var!>>%_fileOut%
Basically, I expected solutions 2,3,4 to behave as they do.
I would expect solutions 1 and 5 to behave differently (do not get what is wrong)...
Especially, I do not get why 3 and 4 do work but not 5 and 1. Is it something to do with the echo| set /p?
One more thing is that when echoing the slow way, element by element, it keeps returning "The system cannot find the drive specified".
为了澄清起见,我在这里添加了完整的代码(输入文件就是上面提到的文件,在我的例子中是csv):
测试代码
@echo关闭
setlocal enableextensions enabledelayedexpansion
rem填充“矩阵”
设置“行=-1”
对于%%r英寸(
“#、到期日、账号、观察日、观察月、开始日、到期日、逾期天数、利率、价差”
“1251710000833220160831201608,不适用,不适用,0,17,0”
)做(
设置/a“行+=1”,“列=-1”
对于%%c in(%%~r)do(
设置/a“列+=1”
设置“_矩阵[!行!][!列!]=%%c”
)
)
rem将“矩阵”转储到输出文件
>“outputFile.txt”(
对于(0 1%行)中的/l%%r,请执行以下操作(
设置“行=”
对于/l%%c(0.1%col%)中的do(
如果%%c==0(设置“行=!\u矩阵[%%r][%%c]!”)否则(设置“行=!行!,!\u矩阵[%%r][%%c]!”)
)
回声!线路!
)
)
对于“为什么”部分,速度问题是因为每个管道都会为管道的每一侧生成一个cmd
进程。此外,重定向每个操作将使进程变慢,因为它将打开/写入/关闭每个操作的输出文件。只需重定向整个过程
由于通过set/p
或直接使用echo
多次回显相同的信息,因此输出中存在重复项
对于5
中的情况,不能使用:
,使用rem
注释代码块内的行
已编辑更新代码以遵循(多于)原始代码
@echo关闭
setlocal enableextensions enabledelayedexpansion
设置“inputFile=input.csv”
设置“outputFile=output.csv”
rem填充“矩阵”
设置“行=-1”
对于/f“usebackq delims=“%%l in”(“%inputFile%”)do(
设置/a“行+=1”,“列=-1”
设置“行=%%l”
呼叫:splitLine
)
rem将“矩阵”转储到输出文件
>%outputFile%(
对于(0 1%行)中的/l%%r,请执行以下操作(
设置“行=”
对于/l%%c in(0 1%col%)为/f“delims=“%%x in”(“!”矩阵[%%r][%%c]!”)执行(
如果%%c==0(设置“行=%%x”)则为其他(设置“行=!行!,%%x”)
)
回声!线路!
)
)
类型“%outputFile%”
后藤:eof
:分割线
(对于/f“tokens=1*delims=,”(“!line!”)中的%%a,如果不是“%%a”=”(
设置/a“列+=1”
设置“_矩阵[!行!][!列!]=%%a”
设置“行=%%b”
))&&goto:splitLine | | goto:eof
测试代码
@echo关闭
setlocal enableextensions enabledelayedexpansion
rem填充“矩阵”
设置“行=-1”
对于%%r英寸(
“#、到期日、账号、观察日、观察月、开始日、到期日、逾期天数、利率、价差”
“1251710000833220160831201608,不适用,不适用,0,17,0”
)做(
设置/a“行+=1”,“列=-1”
对于%%c in(%%~r)do(
设置/a“列+=1”
设置“_矩阵[!行!][!列!]=%%c”
)
)
rem将“矩阵”转储到输出文件
>“outputFile.txt”(
对于(0 1%行)中的/l%%r,请执行以下操作(
设置“行=”
对于/l%%c(0.1%col%)中的do(
如果%%c==0(设置“行=!\u矩阵[%%r][%%c]!”)否则(设置“行=!行!,!\u矩阵[%%r][%%c]!”)
)
回声!线路!
)
)
对于“为什么”部分,速度问题是因为每个管道都会为管道的每一侧生成一个cmd
进程。此外,重定向每个操作将使进程变慢,因为它将打开/写入/关闭每个操作的输出文件。只需重定向整个过程
由于通过set/p
或直接使用echo
多次回显相同的信息,因此输出中存在重复项
对于5
中的情况,不能使用:
,使用rem
注释代码块内的行
已编辑更新代码以遵循(多于)原始代码
@echo关闭
setlocal enableextensions enabledelayedexpansion
设置“inputFile=input.csv”
设置“outputFile=output.csv”
rem填充“矩阵”
设置“行=-1”
对于/f“usebackq delims=“%%l in”(“%inputFile%”)do(
设置/a“行+=1”,“列=-1”
设置“行=%%l”
呼叫:splitLine
)
rem将“矩阵”转储到输出文件
>%outputFile%(
对于/l%%r英寸(0