Eu tenho um arquivo .CAD onde eu preciso acrescentar dados estáticos "NH, NHNOKYCDATAONRECORD" depois de verificar o arquivo .NAK.
O arquivo NAK contém números de linhas que indicam em qual número de linha NH, o texto NHNOKYCDATAONRECORD a ser anexado aparece no arquivo .CAD.
O código está funcionando perfeitamente quando menciono os números de linha no arquivo de lote e o executo, no entanto, não funcionaria se eu não mencionasse os números de linha no arquivo de lote. Alguma ideia de qual seria o problema?
Formato de arquivo CAD:
099,3,IRE,100040148,,TEN,RENNY,1100/,EI,6637032796,6700.00,B
099,3,IRE,100042714,,TEN,SABAM,1100/,EI,8449185754,4700.00,B
099,3,IRE,100044249,,TEN,ELVERTHY,1100/,EI,8890949716,1300.00,A
099,3,IRE,100063520,,TEN,PAUL,1100/,EI,8579431077,2000.00,A
099,3,IRE,100065012,,TEN,BAL INVRA,1100/,EI,9087430395,5900.00,A
099,3,IRE,100065137,,TEN,YANA AMIER,1100/,EI,6651639385,5000.00,A
099,3,IRE,100065343,,TEN,SHAWN,1100/,EI,9087430395,5900.00,A
099,3,IRE,100066754,,TEN,NEIL,1100/,NH,NHNOKYCDATAONRECORD,5000.00,A
099,3,IRE,100066820,,TEN,PAT JOJO,1100/,EI,8715858324,1000.00,A
Formato de arquivo NAK:
AHR,ACCPWEXP,ADCAD099100001180706102746.CAD,CAD1807060000129
DER,001145,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001195,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001197,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001233,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001260,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001262,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001322,CAD-SEC-10,10-NUMBER IS NOT VALID
DER,001351,CAD-SEC-10,10-NUMBER IS NOT VALID
Arquivo em lote:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\Documents\CAD"
SET "destdir=C:\Users\Documents\CAD"
SET "filename1=%sourcedir%\*.CAD"
SET "filename2=%sourcedir%\*.NAK"
SET "outfile=%destdir%\ADCAD099100001%date:~-2%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%.CAD"
:: remove variables starting #
FOR /F "delims==" %%a In ('set # 2^>Nul') DO SET "%%a="
:: Retrieve line numbers from file2.
FOR /f "usebackqtokens=1,2delims=," %%a IN ("%filename2%") DO (
REM token 1 (%%a) must be "DER", token 2 (%%b) contains line number
REM set 'line' to zero-suppressed line number, then #linenumber to 'Y'
ECHO "%%a" "%%b"
IF "%%a"=="DER" SET /a line=1%%b %% 100000&SET "#!line!=Y"
)
REM The next line should list all of the lines-required-to-be-changed
REM in the format "#linenumber=Y"
SET #1145=Y
SET #1195=Y
SET #1197=Y
SET #1233=Y
SET #1260=Y
SET #1262=Y
SET #1322=Y
SET #1351=Y
(
REM number each line in [] with 'find'
REM then tokenise - %%a gets the number, %%b the line-contents
FOR /f "tokens=1*delims=[]" %%a IN ('type "%filename1%"^|find /n /v ""') DO (
IF DEFINED #%%a (
REM process if selected
ECHO Line %%a found for analysis>con
FOR /f "tokens=1-9,*delims=," %%g IN ("%%b") DO (
REM Tokenise to %%g..%%p.
REM if %%n is "EI" then replace else report
IF "%%n"=="EI" (
ECHO Line %%a has EI in required column and should be changed>con
ECHO %%g,%%h,%%i,%%j,,%%k,%%l,%%m,NH,NHNOKYCDATAONRECORD,%%p
) ELSE (
ECHO Line %%a does not have EI in required column>con
ECHO ERROR - line %%a does NOT contain EI IN required column LINE OMITTED
)
)
) ELSE IF "%%b" neq "" (
REM IF NOT selected, simply regurgitate it unless empty
ECHO %%b
)
)
)>"%outfile%"
GOTO :EOF