我想創建一個批處理檔案,我可以在其中查看我在游戲中收集的內容。游戲將此資訊保存在 .txt 檔案中。輸出看起來像這樣。
70x Silver.
Back Pearl.
41x Copper.
Amethyst.
Amethyst.
12x Silver.
Back Pearl.
21x Copper.
5x Silver.
Back Pearl.
Back Pearl.
Amethyst.
我現在要做的是將具有相同名稱的專案添加在一起,如下所示:
128x Silver.
4x Back Pearl.
62x Copper.
3x Amethyst.
有數百個不同名稱的專案,而不僅僅是這 4 個。這可能嗎?任何幫助,將不勝感激。謝謝!
uj5u.com熱心網友回復:
Would that be possible?- 是的。
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (t.txt) do (
set " z=%%a"
set " z=!z:x =@!"
set " z=!z: =_!"
for /f "tokens=1,2 delims=@" %%b in ("!z!") do (
if "%%c" == "" (
set "x=1"
set "y=%%b
) else (
set "x=%%b"
set "y=%%c"
)
set /a #!y! =!x!
)
)
(for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
set "x=%%a"
echo %%bx !x:_= !
))>summary.txt
type summary.txt
使用您的示例資料輸出(我希望您可以按字母排序):
3x Amethyst.
4x Back Pearl.
62x Copper.
87x Silver.
(對于給定的輸入資料,您對 120 銀的計算可能有點樂觀)
uj5u.com熱心網友回復:
另一個!
@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%l in (test.txt) do for /F "tokens=1*" %%a in ("%%l") do (
set "first=%%a"
if "!first:~-1!" equ "x" (set /A "num=!first:~0,-1!") else set "num=0"
if !num! equ 0 (
set "rest=%%l"
set /A "count[!rest: =_!] =1"
) else (
set "rest=%%b"
set /A "count[!rest: =_!] =num"
)
)
(for /F "tokens=2* delims=[]=" %%a in ('set count[') do (
set "item=%%a"
if %%b equ 1 (
echo !item:_= !
) else (
echo %%bx !item:_= !
)
)) > summary.txt
uj5u.com熱心網友回復:
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q72672485.txt"
:: remove variables starting #
FOR /F "delims==" %%b In ('set # 2^>Nul') DO SET "%%b="
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
CALL :sub %%b
)
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /F "tokens=1,2delims==" %%b In ('set # 2^>Nul') DO (
SET "line=%%cx%%b"
ECHO !line:#= !
)
)>summary.txt
endlocal
type summary.txt
GOTO :EOF
:sub
SET "quantity=%1"
SET "line=%*"
IF /i "%quantity:~-1%"=="x" (SET /a quantity=%quantity:~0,-1%&SET "line=%line:* =%") ELSE SET quantity=1
IF %quantity%==0 SET /a quantity=1&SET "line=%*"
SET /a #%line: =#% =quantity
GOTO :eof
不同的做法...
uj5u.com熱心網友回復:
另一種方法(將檔案拆分為有數量和無數量的專案)(也在Pollux Infusion我的第一個答案中解決了這個問題):
@echo off
setlocal enabledelayedexpansion
REM process lines without quantity:
for /f "delims=" %%a in ('type test.txt^|findstr /vrc:"[0123456789][0123456789]*x "') do call :count 1 "%%a"
REM process lines with quantity:
for /f "tokens=1*delims=x " %%a in ('type test.txt^|findstr /rc:"[0123456789][0123456789]*x "') do call :count %%a "%%b"
REM reformat:
(for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
set "line=%%bx %%a" &REM including '1x'
REM if %%b==1 set "line=%%a" &REM supressing '1x'
echo !line:_= !
))>summary.txt
type summary.txt
goto :eof
:count
set item=%~2
set "item=%item: =_%"
set /a #%item% =%1
包括有和沒有1x。洗掉線,你不想要。
uj5u.com熱心網友回復:
這是一種使用“檔案合并”方法的不同方法。整體代碼比其他方法要簡單一些...
@echo off
setlocal EnableDelayedExpansion
set "lineNum=0"
(for /F "tokens=1,2* delims=:x " %%a in ('(type test.txt ^& echo 0x^) ^| findstr /N "[0-9][0-9]*x"') do (
if !lineNum! lss %%a call :locateLine %%a
set "line=%%c"
set /A "count[!line: =_!] =%%b"
)) < test.txt
set "count[="
(for /F "tokens=2* delims=[]=" %%a in ('set count[') do (
set "item=%%a"
if %%b equ 1 (echo !item:_= !) else echo %%bx !item:_= !
)) > summary.txt
goto :EOF
:locateLine num
set /A "lineNum =1" & set /P "line=" & if errorlevel 1 exit /B
if %lineNum% lss %1 set /A "count[%line: =_%] =1" & goto locateLine
exit /B
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/494648.html
標籤:批处理文件
下一篇:從cmd函式回傳值
