0

我正在尝试在 Windows 批处理文件中拆分并创建一个新字符串,但我不知道该怎么做。

我正在尝试将其转换为:

Crysis3_2560x1440

对此:

Crysis 3 - 2560x1440

接着:

HD7970CrossFire

对此:

HD 7970 CrossFire

有什么建议么?

4

2 回答 2

2

编辑:这是管理评论中提到的特殊情况的新版本:

@echo off
setlocal EnableDelayedExpansion

for /F %%a in (inputFile.txt) do (
   set string=%%a
   echo Original: !string!
   call :Splint string
   echo Modified: !string!
)
goto :EOF

:Splint string
set string=!%1!
rem Change underscore by space-dash-space
set string=%string:_= - %
rem Separate the string at first group of digits or space
for /F "tokens=1* delims=0123456789 " %%a in ("%string%") do (
   set first=%%a
   set last=%%b
)
rem Eliminate first part from middle
set middle=!string:%first%=!
rem Eliminate last part from middle, if exists
if defined last set middle=!middle:%last%=!
rem Eliminate spaces from middle
set middle=%middle: =%
rem Assemble the result
if defined middle (
   set %1=%first% %middle% %last%
) else (
   set %1=%first% %last%
)
exit /B

输出:

Original: Crysis3_2560x1440
Modified: Crysis 3 - 2560x1440
Original: HD7970CrossFire
Modified: HD 7970 CrossFire
Original: Skyrim_2560x1440
Modified: Skyrim - 2560x1440
Original: SleepingDogs_2560x1440
Modified: SleepingDogs - 2560x1440
Original: GTX680
Modified: GTX 680

安东尼奥

于 2013-03-12T03:56:50.423 回答
1

您可以使用这些片段作为基础...

set in=Crysis3_2560x1440
set out=!in:_= - !
set in
set out

和...

set in=HD7970CrossFire
set out=!in:HD7970CrossFire=HD 7970 CrossFire!
set in
set out

虽然我只是不明白你为什么不去

set str=HD 7970 CrossFire
于 2013-03-12T05:26:27.310 回答