Batch Manipulating String by Input Variable Using "=&rem." -
using server 2016 here simple batch-file programming (for now).
i have think easy problem, not seeing workable solution in front of me right now. i'm going limit request right down testing scenario keep simple. trying trim string right until hits current year in format "_%year%-" year come variable elsewhere, set static in example. running issues referring variable within code have.
this working example not using variable on rem line, , gives 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 "_2017-" hard-coded in on 10th line. looking in purely logical sense on rem line specifically:
set teststring=%teststring:%yearmodified%=&rem.%
because of way command modifies teststring in-line, makes difficult inject variable it. have tried huge combination of escapes , expansion settings variable take no success far. have tried "build" command string , attempt call , pipe output 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 such simple issue, lack of understanding of string manipulation under dos apparent. stands, rest of script running "_2017-" hard-coded. nice eliminite bit of maintenance.
any or direction appreciated.
thank you.
may show method of splitting. replace "delimiterstring" proper delimiter , use for
split (for
uses single-letter delimiters). enabling delayed expansion helps, not neccessary. explicitely disabled (you may have reasons), i'll show both:
@echo off set "teststring=machined_cam-2286_2017-09-08.slddrw - solidworks" set "cuthere=_2017" setlocal enabledelayedexpansion /f "delims=§" %%a in ("!teststring:%cuthere%=§!") set result=%%~a echo enabled: %result% endlocal setlocal disabledelayedexpansion /f "delims=§" %%a in ('call echo "%%teststring:%cuthere%=§%%"') set result=%%~a echo disabled: %result% endlocal
just sure, use delimiter (§
here) surely won't in string(s).
Comments
Post a Comment