Batch Manipulating String by Input Variable Using "=&rem."

291 views Asked by At

Using Server 2016 here with simple batch-file programming (for now).

I have what I think is an easy problem, just that I am not seeing a workable solution in front of me right now. I'm going to limit my request right down to a testing scenario to keep it simple. What I am trying to do is trim a string from the right until it hits the current year in the format "_%year%-" where the year does come from a variable elsewhere, but is set static in my example. Where I am running into issues is referring to a variable from within the code I have.

This is a working example NOT using a variable on the rem line, and gives the desired output of "Machined_Cam-2286:"

@ECHO OFF
SETLOCAL DisableDelayedExpansion

set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"

echo "%testString%" | find "%yearModified%" >NUL || goto :EOF

set testString=%testString:_2017-=&rem.%

echo %testString%

pause

You can see that "_2017-" hard-coded in on the 10th line. What I am looking to do in a purely logical sense on the rem line specifically:

set testString=%testString:%yearModified%=&rem.%

Because of the way this command modifies testString in-line, it makes it difficult to inject a variable into it. I have tried a huge combination of escapes and expansion settings to get the variable to take with no success so far. I have also tried to "build" the command as a string and attempt to call it and pipe the output to a variable:

@ECHO OFF
SETLOCAL DisableDelayedExpansion

set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"

echo "%testString%" | find "%yearModified%" >NUL || goto :EOF

set "callCMD=%%testString:%yearModified%=^&rem.%%"

call %callCMD% > %testString%

echo %testString%

pause

This seems like such a simple issue, but my lack of understanding of string manipulation under DOS is apparent. As is stands, the rest of the script is running perfectly with the "_2017-" hard-coded. It would be nice to eliminite that bit of maintenance.

Any help or direction is appreciated.

Thank you.

4

There are 4 answers

3
Stephan On BEST ANSWER

May I show you another method of splitting. Replace your "Delimiterstring" with a proper Delimiter and use a for to split (for uses single-letter delimiters). Enabling delayed expansion helps, but is not neccessary. As you explicitely disabled it (you may have your reasons), I'll show you both:

@echo off
set "TestString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "CutHere=_2017"

setlocal enabledelayedexpansion
for /f "delims=§" %%a in ("!TestString:%CutHere%=§!") do set Result=%%~a
echo enabled:  %Result%
endlocal

setlocal disabledelayedexpansion
for /f "delims=§" %%a in ('call echo "%%TestString:%CutHere%=§%%"') do set Result=%%~a
echo disabled: %Result%
endlocal

Just be sure, you use a delimiter (§ here) that surely won't be in your string(s).

1
IRQ Conflict On

you are trying to control what you call using the contents of a variable

try using a temp file instead -

echo %%testString:%yearModified%=^&rem.%%" > temp.bat
call temp.bat > %testString%
1
AudioBubble On

You NEED delayed expansion for this type of operation, so enabe it.

  • I suggest to first get the remainder of the string
  • and remove this from the original string.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"

echo "%testString%" | find "%yearModified%" >NUL || goto :EOF

set tempString=!testString:*%yearModified%=!
set testString=!testString:%yearModified%%tempstring%=!

echo %testString%

pause

Sample output:

Machined_Cam-2286
0
kygg On
@echo off

setlocal EnableDelayedExpansion

set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"

echo "%testString%" | find "%yearModified%" >NUL || goto :EOF

set testString=!testString:%yearModified%=^&rem.!

echo %testString%

pause

You shoud use a carrot(^) in Delayed Expansion mode.