Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am new to Windows batch programming. I have a requirement of extracting a string data from a line.I have a file let say (doctype.txt) containing the following values in separate lines.
VESPRO/B
CHPOI13A/B
CHSAE01A/F
SAEHO01/F
CHPPOI06/BI want to get the String data before the forward slash (/) into a variable one by one.
Like: VESPRO, CHPOI13A etc...
Please help me to resolve the above requirement?
Thanks in Advance.
Deviprasad

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set csvar=
for /f "tokens=1 delims=/" %%a in (doctype.txt) do set csvar=!csvar!%%a,
echo %csvar%
endlocal
pauseJust bear in mind there is a variable string length limit, and this sort of thing can max it out.
The below are taken from this page
"The maximum individual environment variable size is 8192bytes.The maximum total environment variable size for all variables,
which includes variable names and the equal sign, is 65,536KB."It is possibly to direct the output to file to circumvent this limit, but you asked for variables so I can only assume these files aren't too large.

@echo off & setlocal EnableDelayedExpansion
type nul > filename.txt
for /f "tokens=* delims=" %%A in (doctype.txt) do (
set line=%%A
set line=!line:~0,-2!
echo.!line!>> temp.txt)
start temp.txt

Ok, I think I read wrong, my result will give you:
VESPRO
CHPOI13A
CHSAE01A
SAEHO01
CHPPOI06and not:
VESPRO,CHPOI13A,CHSAE01A,SAEHO01,CHPPOI06

I think deviprasad wants
var1=VESPRO
var2=CHOPI13a
var3=...
etc.In that case,
@echo off
setlocal EnableDelayedExpansion
set varcount=1
for /f "tokens=1 delims=/" %%a in (doctype.txt) do (
set var!varcount!=%%a
:Uncomment below line if you want the variables outside the batch file.
:echo set var!varcount!=%%a >> setvarslater.bat
set /a varcount+=1
)--
Holla.

Hi All,
Thanks for all your suggestion.I tried the way suggested by Lee and got the solution to my problem.
I have done the below:
for /f %%a in (docType.txt) do (
call :CreateScript %%a
):CreateScript
set fn=%1
set docType=%fn:~0,-2%
Thanks for all your support.
Deviprasad

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |