问题
I have a little problem, I would like to edit data with the following batch script.
@echo off
set "txt=C:\Users\Desktop\test-batch\input.txt"
set "temp=C:\Users\Desktop\test-batch\output.txt"
for /f "tokens=1-43 delims=; " %%a in (%txt%) do echo %%a;%%ac;%%ad;%%ae;%%af;%%ag;%%ah;%%ai;%%aj;%%ak;%%al;%%am;%%an;%%ao;%%ap;%%aq; > %temp%
input.txt:
1;2;2;1;1;1;1;1;1;1;;;;;;;;;;;;;;;;1;;;1-Trackingnummer;2-Trackingnummer;3-Trackingnummer;4-Trackingnummer;5-Trackingnummer;6-Trackingnummer;7-Trackingnummer;8-Trackingnummer;9-Trackingnummer;10-Trackingnummer;11-Trackingnummer;12-Trackingnummer;13-Trackingnummer;14-Trackingnummer;15-Trackingnummer;
output.txt (column 1
and columns 29-43
):
1;1-Trackingnummer;2-Trackingnummer;3-Trackingnummer;4-Trackingnummer;5-Trackingnummer;6-Trackingnummer;7-Trackingnummer;8-Trackingnummer;9-Trackingnummer;10-Trackingnummer;11-Trackingnummer;12-Trackingnummer;13-Trackingnummer;14-Trackingnummer;15-Trackingnummer;
Can someone tell me where the problem is and why it doesn't work?
回答1:
What follows is a potential solution which doesn't use tokens
and delimiters
in the same way as your for /f
loop. The idea of this is to output tokens 1
and 29-43
of your semicolon
delimited lines, (as per your example).
Please don't forget to correct the file paths in lines 2
and 3
before testing it.
@Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
Set "source=C:\Users\Desktop\test-batch\input.txt"
Set "target=C:\Users\Desktop\test-batch\output.txt"
If Exist "%source%" (For %%G In ("%source%")Do If "%%~aG" GEq "d" GoTo :EOF)Else GoTo :EOF
For %%G In ("%target%\..")Do If "%%~aG" Lss "d" GoTo :EOF
For /F "Delims==" %%G In ('Set f[ 2^>NUL')Do Set "%%G="
(For /F Delims^=^ EOL^= %%G In ('Type "%source%"')Do Call :Sub "%%G") 1> "%target%"
GoTo :EOF
:Sub
SetLocal EnableDelayedExpansion
If "%~1" == "" Exit /B
Set "line=%~1"
Set "i=1"
Set "f[!i!]=%line:;="&Set /A i+=1&Set "f[!i!]=%"
Set "out=!f[1]!;"
For /L %%I In (29,1,43)Do Set "out=!out!!f[%%I]!;"
Echo(%out%
EndLocal
Exit /B
Line 5
and 6
are just checks that your source and target are valid. If the source is not an existing file, or target file directory does not exist, the script is closed.
Line 8
ensures that we have no pre-defined variables with names beginning with f[
.
Line 10
reads from the source file and passes each line to the :Sub
label which performs the token parsing.
The code in the :Sub
label splits each token using the delimiter, setting each to an independent variable, (f[#]
, where #
is the token number). The required variables are then joined in a holding variable, (%out%
), which is finally echo
ed to %target%
.
In your case the majority of your tokens were a complete range, so I was able to propagate %out%
with token 1
, (line 19
), and all of the range 29..45
on line 20
. If your tokens change then you may replace lines 19
and 20
to Set "out="
, and e.g. For %%I In (1 29 32 34 35 37 39 43)Do Set "out=!out!!f[%%I]!;"
respectively.
Please note that this idea was written to be able to perform the task you laid out in your question. It has a line character length limit and the number of tokens is limited to the allowed size of the environment.
来源:https://stackoverflow.com/questions/62018681/batch-txt-processing-with-multiple-columns