Solved split dashed names from a variable into many variables

May 9, 2012 at 04:22:29
Specs: Windows 7

If i have a variable contains names with dashes between them. I need a bat file that can split every name into a different variable depending on the dash
set variable=John-Smith-Mary-Ahmed
i want another variables with only the names
v1=John
v2=Smith
v3=Mary
v4=Ahmed
How can i do that ? thanks

See More: split dashed names from a variable into many variables

Report •


#1
May 9, 2012 at 07:30:18
✔ Best Answer

@echo off
set variable=John-Smith-Mary-Ahmed
echo %variable% > c:\variable1.txt

FOR /f "tokens=1-4 delims=-" %%G IN (c:\variable1.txt) DO (set v1=%%G && set v2=%%H && set v3=%%I && set v4=%%J)

ECHO %v1%
ECHO %v2%
ECHO %v3%
ECHO %v4%

pause

mike


Report •

#2
May 9, 2012 at 08:42:54

when i run the file i got
access denied
the system cannot find the file c:\vriable1.txt.
Echo is off
Echo is off
Echo is off
Echo is off

Report •

#3
May 9, 2012 at 09:29:49

It should create the file and then access it? you could exchange the c:\ for your working directory? I guess its possible that it doesn't work in windows 7?

edit: is it possible that the spelling on variable was changed accidentally? I see that it is a different spelling from the script i posted?
mike


Report •

Related Solutions

#4
May 9, 2012 at 09:44:31

yeah...thank you it worked when i changed the drive

Report •


Ask Question