批量自动化更改文本

批量自动化更改文本

我有一个 .CAD 文件,在检查 .NAK 文件后,我需要附加静态数据“NH,NHNOKYCDATAONRECORD”。

NAK 文件包含行号,指示要附加的 NH,NHNOKYCDATAONRECORD 文本出现在 .CAD 文件中的哪一行。

当我在批处理文件中提到行号并执行它时,代码绝对可以正常工作,但是如果我不在批处理文件中提到行号,它就不会工作。知道问题是什么吗?

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

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 

批处理文件:

@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

答案1

您的批处理这一部分无法正常工作
(您在打开的 cmd 窗口中执行它并插入暂停命令时自己发现了这一点)。

:: 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"
)

原因是

FOR /f "usebackqtokens=1,2delims=," %%a IN ("%filename2%") DO (  

需要打开单个文件,但%filename2%包含通配符C:\Users\Documents\CAD\*.NAK

解决方案是在这个部分周围放置另一个(简单的)来迭代文件

:: Retrieve line numbers from file2.
For %%F in ("%filename2%") Do (
    FOR /f "usebackqtokens=1,2delims=," %%a IN ("%%F") 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"
    )
)
Set #

示例输出:

> SU_1337246.cmd
"AHR" "ACCPWEXP"
"DER" "001145"
"DER" "001195"
"DER" "001197"
"DER" "001233"
"DER" "001260"
"DER" "001262"
"DER" "001322"
"DER" "001351"
#1145=Y
#1195=Y
#1197=Y
#1233=Y
#1260=Y
#1262=Y
#1322=Y
#1351=Y

相关内容