0

I am trying to split and make a new string in a Windows batch file but I can't figure out how to do it.

I am trying to convert this:

Crysis3_2560x1440

To this:

Crysis 3 - 2560x1440

And then:

HD7970CrossFire

To this:

HD 7970 CrossFire

Any suggestions?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Ryan Shrout
  • 19
  • 1
  • 3
  • Not sure about batch files, but could you use Powershell? – Seth Moore Mar 12 '13 at 02:32
  • possible duplicate of [How to split a string in a Windows batch file?](http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file) – Ken White Mar 12 '13 at 02:32
  • I honestly haven't tried anything yet; I've been coding in perl and AutoIt all day and can't quite wrap my head around it. Not sure about Powershell or how it would integrate...? Ken: I saw that but I just can't make it apply to my goals here... TRYING! – Ryan Shrout Mar 12 '13 at 02:38
  • Do you want to split _both_ strings above with _the same_ Batch program? Could you explain the method used in the splitting with words, not with examples? For example: "separate the string at the first group of digits..." – Aacini Mar 12 '13 at 03:32
  • Aacini, sure! For the first split we'll have a string with a game name and a resolution, separated by an underscore. Splitting the game name and resolution at the _ is pretty straight forward, we also want to be able separate the game name by intercaps or numbers. So... Crysis3 = Crysis 3 FarCry3 = Far Cry 3 SleepingDogs = Sleeping Dogs For the second split, its basically a duplicate of the first, with a need to be sure you are hitting the intercaps correctly. HD7970Crossfire = HD 7970 Crossfire GTX680 = GTX 680 GTX680SLI = GTX 680 SLI HD7970 = HD 7970 – Ryan Shrout Mar 12 '13 at 03:48
  • Check my answer below and request any modifications there... – Aacini Mar 12 '13 at 03:58

2 Answers2

2

EDIT: This is the new version that manage the special cases mentioned in comments:

@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

Output:

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

Antonio

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Wow, this is awesome! I just tried it out and it works well for the most part with a couple of issues I found. 1. "Skyrim_2560x1440" and "SleepingDogs_2560x1440" result in "Skyrim- 2560 x1440" 2. Similarly, if the second string like "GTX680" doesn't have a third part I am getting: "Modified: GTX middle:=" Thanks so much for the start!!! – Ryan Shrout Mar 12 '13 at 05:28
  • @RyanShrout: Check the new version! – Aacini Mar 12 '13 at 17:14
  • @Aacini it looks better! I did notice that the modified version of "SleepingDogs" didn't separate into "Sleeping Dogs". Also, one thought to make the GPU names (gtx, hd) easier. Can you search for "GTX" or "HD" and separate there? That would allow for "GTXTitan" to modify to "GTX Titan" or "GTXTitanSLI" to "GTX Titan SLI". Also, another use case would be (and the longest one) "GTX660TiSLI" to "GTX 660 Ti SLI". This place needs a tip jar!! – Ryan Shrout Mar 12 '13 at 17:56
1

You could use these snippets as a base...

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

And...

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

Although I just don't see why you don't just go

set str=HD 7970 CrossFire
BDM
  • 3,760
  • 3
  • 19
  • 27