这是一种可能的方式来做你想做的事 - 仅适用于整数值(请参阅代码中的解释性注释rem
):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=.\data.csv" & rem // (path to CSV file to modify)
set "_TMPF=%TEMP%\%_FILE%.tmp" & rem // (path to temporary file)
set "_ACCT=%~1" & rem // (account number to search, taken from first argument)
rem // Write modified CSV data to temporary file:
> "%_TMPF%" (
rem // Reset flag to indicate header (first row):
set "SKIP="
rem // Read CSV file line by line and extract seven tokens (columns):
for /F "tokens= 1-7 delims=, eol=," %%A in ('type "%_FILE%"') do (
rem // Check whether line is header, skip it from modification in case:
if defined SKIP (
rem // Check whether current account number matches:
if /I "%%C"=="%_ACCT%" (
rem // Assemble first three call values (do not modify):
set "PREF=%%A,%%B,%%C"
rem /* Invert sign of remaining four (numeric) cell values;
rem instead, you could also simply write this:
rem `echo(%%A,%%B,%%C,-%%D,-%%E,-%%F,-%%G`, but this
rem would lead to `--` if a number is already negative: */
set /A "VAL1=-%%D, VAL2=-%%E, VAL3=-%%F, VAL4=-%%G"
rem // Return modified line:
setlocal EnableDelayedExpansion
echo(!PREF!,!VAL1!,!VAL2!,!VAL3!,!VAL4!
endlocal
) else (
rem // Account number does not match, so return original line:
echo(%%A,%%B,%%C,%%D,%%E,%%F,%%G
)
) else (
rem // Line is the header, so return original line:
echo(%%A,%%B,%%C,%%D,%%E,%%F,%%G
rem // Next line is certainly not a header:
set "SKIP=#"
)
)
)
rem // Replace original CSV file with temporary file:
> nul move /Y "%_TMPF%" "%_FILE%"
endlocal
exit /B
这是另一种方式——对于十进制值,它实际上被视为字符串(见备注rem
):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=.\data-dec.csv" & rem // (path to CSV file to modify)
set "_TMPF=%TEMP%\%_FILE%.tmp" & rem // (path to temporary file)
set "_ACCT=%~1" & rem // (account number to search, taken from first argument)
rem // Write modified CSV data to temporary file:
> "%_TMPF%" (
rem // Reset flag to indicate header (first row):
set "SKIP="
rem // Read CSV file line by line and extract seven tokens (columns):
for /F "tokens= 1-7 delims=, eol=," %%A in ('type "%_FILE%"') do (
rem // Check whether line is header, skip it from modification in case:
if defined SKIP (
rem // Check whether current account number matches:
if /I "%%C"=="%_ACCT%" (
rem // Assemble first three call values (do not modify):
set "PREF=%%A,%%B,%%C"
rem // Invert sign of remaining four (numeric) cell values:
set "VAL1=-%%D" & set "VAL2=-%%E" & set "VAL3=-%%F" & set "VAL4=-%%G"
rem // Return modified line, avoiding doubled minus-signs:
setlocal EnableDelayedExpansion
echo(!PREF!,!VAL1:--=!,!VAL2:--=!,!VAL3:--=!,!VAL4:--=!
endlocal
) else (
rem // Account number does not match, so return original line:
echo(%%A,%%B,%%C,%%D,%%E,%%F,%%G
)
) else (
rem // Line is the header, so return original line:
echo(%%A,%%B,%%C,%%D,%%E,%%F,%%G
rem // Next line is certainly not a header:
set "SKIP=#"
)
)
)
rem // Replace original CSV file with temporary file:
> nul move /Y "%_TMPF%" "%_FILE%"
endlocal
exit /B