Hello everyone, i am trying to convert data (codes with a user defined seperation) to a variable.
Here is a example
Data.bat:
129,42,1,32,5
Batch:
set i1=129
set i2=42
set i3=1
set i4=32
set i5=5
:Save
(
echo %i1%,%i2%,%i3%,%i4%,%i5%)>data.dat
I am not to famaliar with for command, so could someone write this out?
Converting data to variables
Moderator: DosItHelp
Re: Converting data to variables
Code: Select all
@echo off
for /F "tokens=1-5 delims=," %%a in ("129,42,1,32,5") do (
set i1=%%a
set i2=%%b
set i3=%%c
set i4=%%d
set i5=%%e
)
echo %i1%,%i2%,%i3%,%i4%,%i5%
-
- Posts: 54
- Joined: 14 Aug 2015 05:59
Re: Converting data to variables
This helps ALOT. Thanks
Re: Converting data to variables
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set n=0
for %%a in (129,42,1,32,5) do (
set /A n+=1
set i!n!=%%a
)
echo %i1%,%i2%,%i3%,%i4%,%i5%
Antonio
Re: Converting data to variables
If you really want to start from an input data file the above suggestions can be applied:
John.
Code: Select all
@echo off
Setlocal EnableDelayedExpansion
::OK as hardcoded vars
for /F "tokens=1-5 delims=," %%a in ("129,42,1,32,5") do (
set i1=%%a
set i2=%%b
set i3=%%c
set i4=%%d
set i5=%%e
)
echo %i1%,%i2%,%i3%,%i4%,%i5%
::OK as hardcoded string
set n=0
for %%a in (129,42,2,32,5) do (
set /A n+=1
set j!n!=%%a
)
echo %i1%,%j2%,%j3%,%j4%,%j5%
::Set up and verify a test file
>data.txt echo.129,42,3,32,5
type data.txt
::adapt first approach directly from test file
for /F "tokens=1-5 delims=," %%a in (Data.txt) do (
set k1=%%a
set k2=%%b
set k3=%%c
set k4=%%d
set k5=%%e
)
echo %k1%,%k2%,%k3%,%k4%,%k5%
::adapt second approach from test file using intermediate variable
<data.txt Set /P DataVar=
set DataVar
set n=0
for %%a in (%DataVar%) do (
set /A n+=1
set l!n!=%%a
)
echo %l1%,%l2%,%l3%,%l4%,%l5%
John.
Re: Converting data to variables
TheHunterManX wrote:Here is a example
Data.bat:
129,42,1,32,5
Batch:
set i1=129
set i2=42
set i3=1
set i4=32
set i5=5
:Save
(
echo %i1%,%i2%,%i3%,%i4%,%i5%)>data.dat
This should do what your example does.
Code: Select all
@echo off
for /f "usebackq delims=" %%a in ("data.bat") do >data.dat echo(%%a
I'm merely pointing out that fake questions and fake data often gives you ineffective or broken solutions.