Converting data to variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
TheHunterManX
Posts: 54
Joined: 14 Aug 2015 05:59

Converting data to variables

#1 Post by TheHunterManX » 29 Sep 2015 08:43

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?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Converting data to variables

#2 Post by Squashman » 29 Sep 2015 08:56

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%

TheHunterManX
Posts: 54
Joined: 14 Aug 2015 05:59

Re: Converting data to variables

#3 Post by TheHunterManX » 29 Sep 2015 09:34

This helps ALOT. Thanks

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Converting data to variables

#4 Post by Aacini » 29 Sep 2015 11:13

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

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Converting data to variables

#5 Post by thefeduke » 29 Sep 2015 16:19

If you really want to start from an input data file the above suggestions can be applied:

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Converting data to variables

#6 Post by foxidrive » 30 Sep 2015 08:29

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.

Post Reply