Link to home
Create AccountLog in
Avatar of DataDesignIT
DataDesignIT

asked on

How to dynamically create variables with string trim

Experts-

I currently have a list of items. For each, I want to dynamically create variables for each, and use just the suffix (trim off the \\blade prefix).

   SET SLIST=\\blade3c \\blade3d \\blade3f
   FOR %%s IN (%SLIST%) DO [...]

I want to trim the string so I get three new variables, the same as if I did the following:
SET 3c=\\blade3c
SET 3d=\\blade3d
SET 3f=\\blade3f

How do I accomplish this?
Thanks.




Avatar of SteveGTR
SteveGTR
Flag of United States of America image

The batch processing appears to have problems with environment variables that start with a digit. I appended an underscore to the dynamic variables to overcome this problem:

@echo off

setlocal

set sList=\\blade3c \\blade3d \\blade3f

for /f "tokens=*" %%a in ('echo %sList%') do call :PROCESS %%a

echo 3c %_3c%
echo 3d %_3d%
echo 3f %_3f%

goto :EOF

:PROCESS

if "%~1"=="" goto :EOF

set _t=%~1
set _t=%_t:~-2%
set _%_t%=%~1

shift

goto PROCESS

Good Luck,
Steve
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Good luck on your future questions...