我有一个SOURCE文本文件,我想操作在分号之前添加一个递增的字母数字,如下所示,并输出OUTPUT.txt SOURCE:
2:apple,coconut,cherry; 3:banana,mango; 4:cereals,nuts; .......... and so on
期望的输出:
2:apple,coconut,cherry,TP001; 3:banana,mango,TP002; 4:cereals,nuts,TP003; .........and so on ----------
所以我提出了下面的脚本,但它只是用TP001替换每个分号; 有人可以帮助我,我怎样才能增加TP001~等等作为我想要的输出。
@echo off
setlocal enabledelayedexpansion
set txtfile=%1
set newfile=OUTPUT.txt
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
set newline=!newline:^;=^,TP001;!
echo !newline! >> %newfile%
)
我希望你能帮助我。谢谢......
答案 0 :(得分:0)
您的数据有一个尾随空格,因此代码会从每行的末尾删除两个字符。
@echo off
setlocal enabledelayedexpansion
set "txtfile=%~1"
set "newfile=OUTPUT.txt"
set c=0
if exist "%newfile%" del /f /q "%newfile%"
for /f "usebackq delims=" %%a in ("%txtfile%") do (
set /a c+=1
set "n=000!c!"
set "n=!n:~-3!"
set "line=%%a"
set "line=!line:~0,-2!
>>"%newfile%" echo !line!,TP!n!;
)
答案 1 :(得分:0)
@echo off
setlocal enabledelayedexpansion
set "txtfile=%~1"
set "newfile=OUTPUT.txt"
break > "%newfile%"
set "count=1000"
(for /f "usebackq delims=;" %%a in ("%txtfile%") do (
set /a "count+=1"
echo(%%a,TP!count:~-3!
))>"%newfile%"
答案 2 :(得分:0)
你走了:
@echo off
setlocal enabledelayedexpansion
set txtfile=%1
set newfile=OUTPUT.txt
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
set /a "inc+=1"
set num=00!inc!
set num=!num:~-3!
set newline=!newline:^;=^,TP!
echo !newline!!num!; >> %newfile%
)