我的問題是,我可以迴應我想要的,但不是我想要的方式。儘管如此,取決於我如何編寫/安排代碼,我得到的迴應行爲不盡相同,從來沒有我想要的方式。有人可以解釋我發生了什麼,爲什麼結果如此,它可能會幫助我瞭解如何更改我的代碼以使其正確。這裏有一些解決方案(僅限於5),它然後如何表現(循環涉及所述第二線的回波):需要批處理文件(窗口)中的「回聲」的行爲
欲呼應矩陣的元素是(2行,逗號分割列)
, 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
- 我想
什麼時候達到這個循環中,它只是退出並關閉命令窗口
: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%
- 什麼工作,但速度很慢
- 奇怪的是,既相呼應我想要什麼樣的方式,什麼我不這樣做,並且是SLO w^
- 奇怪的方式2
- 變形奇怪的方式2
現在我的第一次嘗試,這是緩慢的,因爲它呼應元/逗號/元/逗號......到行前結束呼應換行符
: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
闖闖,做工作,呼應結果兩次,第一次行緩慢的方式,第二排的快捷方式
: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
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
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 off
SETLOCAL EnableDelayedExpansion
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Adress of the input and output files
GOTO InFile
:FalseIN
ECHO You need to enter a valid location for the source file.
GOTO InFile
:InFile
ECHO Please enter the location of your file in like C:\Users\VerchieD\Desktop\Command_line\example.csv
SET _fileIn
SET /P _fileIn=Type input: %=%
IF NOT EXIST "%_fileIn%" GOTO FalseIn
ECHO The selected input file to work with is %_fileIn%
:OutFile
echo Please enter the name of your output like C:\Users\VerchieD\Desktop\Command_line\output.csv
SET _fileOut
SET /P _fileOut=Type input: %=%
IF NOT DEFINED "%_fileOut%" set "_fileOut=C:\Users\VerchieD\Desktop\Command_line\Output.csv"
ECHO.>"%_fileOut%"
ECHO The selected output file to work with is %_fileOut%
ECHO stop 1
@PAUSE
SET _countParse=0
SET _countParseBis=0
SET _countNext=0
SET _countNextBis=0
SET _ligneAct=0
SET _ligne=0
SET _colonne=0
CALL :ParseHeader "%_fileIn%"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO :eof
::Parse input file header, first element
:ParseHeader
SET /a _countParse+=1
SET _list=%1
SET _list=%_list:"=%
FOR /F "tokens=1* delims=, " %%a IN ('TYPE "%_list%"') DO (
set _matrice[%_ligne%][%_colonne%]=%%a
set /a _colonne+=1
if not "%%b"=="" call :ParseHeaderBis "%%b"
)
GOTO :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO :eof
::Parse input file header, from second element onward
:ParseHeaderBis
SET /a _countParseBis+=1
SET _list=%1
SET _list=%_list:"=%
FOR /F "tokens=1* delims=, " %%a IN ("%_list%") DO (
set _matrice[%_ligne%][%_colonne%]=%%a
if not "%%b"=="" set /a _colonne+=1
if not "%%b"=="" if %_ligne% equ 0 call :ParseHeaderBis "%%b"
if "%%b"=="" set /a _ligne+=1
)
@PAUSE
IF %_ligne% gtr 0 GOTO :EchoHeader
GOTO :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Echoes one element at a time for now, will be changed
:EchoHeader
FOR /L %%H IN (0,1,%_colonne%) DO (
::echo The column %%H
::echo variable is !_matrice[0][%%H]!
echo|set /p=!_matrice[0][%%H]!>>%_fileOut%
if not %%H equ %_colonne% echo|set /p=,>>%_fileOut%
if %%H equ %_colonne% echo.>>%_fileOut%
)
@PAUSE
CALL :Next
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO :eof
::Parse input file data, from second row onward, get lines
:Next
SET /a _countNext+=1
SET /a _ligneAct=%_ligne%
SET _colonne=0
FOR /F "skip=%_ligne% tokens=* delims=" %%a IN ('type "%_fileIn%"') DO (
if not "%%a"=="" call :NextBis "%%a"
if "%%a"=="" goto :eof
)
GOTO :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO :eof
::Parse the lines from the input file
:NextBis
SET /a _countNextBis+=1
SET _list=%1
SET _list=%_list:"=%
FOR /F "tokens=1* delims=, " %%a IN ("%_list%") DO (
set _matrice[%_ligne%][%_colonne%]=%%a
if not "%%b"=="" set /a _colonne+=1
if not "%%b"=="" if %_ligne% equ %_ligneAct% call :NextBis "%%b"
if "%%b"=="" set /a _ligne+=1
)
IF %_ligne% gtr %_ligneAct% GOTO :EchoData
GOTO :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:EchoData
::SET _var=""
::FOR /L %%G IN (0,1,%_ligneAct%) DO (
::FOR /L %%H IN (0,1,%_colonne%) DO (
::echo The row %%G
::echo and column %%H
::echo value is !_matrice[%%G][%%H]!
::if %%H equ 0 set _var=!_matrice[%_ligneAct%][%%H]!
::set _help=!_var!
::if %%H gtr 0 set _var=!_help!, !_matrice[%_ligneAct%][%%H]!
::echo|set /p=!_matrice[%_ligneAct%][%%H]!>>%_fileOut%
::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%
::if %%H==0 (set "_var=!_matrice[%_ligneAct%][%%H]!") else (set "_var=!_var!, !_matrice[%_ligneAct%][%%H]!")
::echo !_var!
::)
::)
:: for /l %%c in (0 1 %col%) do (
:: if %%c==0 (set "line=!_M[%%r,%%c]!") else (set "line=!line!, !_M[%%r,%%c]!")
::)
:: echo !line!
>%_fileOut% (
::for /l %%r in (0 1 %row%) do (
set "line="
for /l %%c in (0 1 %_colonne%) do (
if %%c==0 (set "line=!_matrice[%_ligneAct%][%%c]!") else (set "line=!line!, !_matrice[%_ligneAct%][%%c]!")
)
echo !line!
::)
)
::echo !_var!>>%_fileOut%
@PAUSE
CALL :Next
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:eof
@PAUSE
你能不能解釋一下什麼是在%矩陣的計算方法%,%Colonne的%和% ligneAct%以及如何將值存儲在這些變量中?這會讓事情變得更清晰。 – Filipus
基本上,我將我的值存儲在矩陣樣式中,以便於閱讀和瀏覽。 %_matrice%[] []是我創建的矩陣,%_colonne%是我期待的列,%_ligneAct%是我期待的實際行(行)。當我回聲時,我會經歷以下幾行:[0] [0],[0] [1],[0] [2] .... [0] [n]; [1] [0],[1] [1],[1] [2] .... [1] [n]; ...; [m] [0],[m] [1],[ m] [2] .... [m] [n] – Didifr