변수에 값을 저장하고 배치 스크립트를 사용하여 변수를 파일의 새 열로로드하는 방법은 무엇입니까?
위의 링크는 날짜 필드를 읽고 csv 파일의 모든 레코드에 기록하는 데 사용됩니다. 폴더의 csv 파일 집합에 대해이 작업을 수행하기를 원합니다. 아래는 요구 사항에 대한 스크립트입니다. 아래 스크립트에서 실수가 무엇인지 알고 싶습니다.
@echo off
setlocal if exist three.txt del three.txt
set cnt=1 rem get the date "01/12/15"
for %%i in (*.csv) do (
if cnt>=1 (
for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (type "%%i") do (
set _date=%%d
goto :next
)
:next
rem add the date to the end of every line and output to "three.txt"
for /f "tokens=* usebackq" %%a in (type "%%i") do (
echo %%a,%_date%>> three.txt
)
)
set /a cnt+=1
)
endlocal
답변
폴더에있는 CSV 파일 집합에 대해이 작업을 수행하고 싶습니다.
다시 말하지만 배치 파일은 여러 가지 방법으로 손상되었습니다.
자신의 배치 파일을 디버깅하는 방법을 배워야합니다.
- 만나다 배치 파일 디버깅 몇 가지 힌트.
- 디버그하려면 사용중인 명령의 구문을 검사해야합니다. 만나다 Windows CMD 명령 줄의 A-Z 색인 .
배치 파일을 디버깅하는 데 시간을 낭비하지 않고 배치 파일을 수정하는 것이 더 빠릅니다. 연결된 질문에 대답하다 새 질문에 대답 해주세요.
다음 배치 파일 (example.cmd)을 사용하십시오.
@echo off
setlocal
if exist three.txt del three.txt
for /r %%i in (*.csv) do (
call :process_file %%i
)
goto :eof
:process_file
echo processing %1
for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (`type %1`) do (
set _date=%%d
goto :next
)
:next
rem add the date to the end of every line and output to "three.txt"
for /f "tokens=* usebackq" %%a in (`type %1`) do (
echo %%a,%_date%>> three.txt
)
)
endlocal
three.txt 대신 동일한 CSV 파일에 결과를 어떻게 써야합니까?
다음 배치 파일 (example.cmd)을 사용하십시오.
@echo off
setlocal
for /r %%i in (*.csv) do (
call :process_file %%i
)
goto :eof
:process_file
echo processing %1
for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (`type %1`) do (
set _date=%%d
goto :next
)
:next
rem add the date to the end of every line and output to "three.txt"
for /f "tokens=* usebackq" %%a in (`type %1`) do (
echo %%a,%_date%>> three.txt
)
)
rem rename the file
del %~nx1
ren three.txt %~nx1
endlocal
더 읽을 거리
- Windows CMD 명령 줄의 A-Z 색인 – Windows cmd line과 관련된 모든 것에 대한 훌륭한 참고서.
- 요구 – 다른 프로그램에서 하나의 배치 프로그램을 호출하거나 서브 루틴을 호출하십시오.
- 델 – 하나 이상의 파일을 삭제합니다.
- / f 용 – 다른 명령의 결과에 대한 명령을 반복하십시오.
- for / r – 루프 스루 파일 (하위 폴더 되풀이).
- 고토 – 배치 된 프로그램이 분류 된 라인으로 점프하도록 지시하십시오.
- 매개 변수들 – 명령 행 인수 (또는 매개 변수)는 일} 처리 스크립트로 전달되는 모든 값입니다.
- 렌 – 파일 이름을 바꿉니다.
- 유형 – 하나 이상의 텍스트 파일의 내용을 표시합니다.