Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

making arrays in batch

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
i would like to create arrays in a batch program, i added all the data as:
arr[1]
arr[2]
arr[3]
...

but the output is the tough part, i used:
set n=2
echo %arr[%n%]%

but that didnt work. :no:
is there anything i can do?

my idea is to write a file list into that array, then pointing to one of the files.
i really dont want to create temp files for this job. :(


How to remove advertisement from MSFN

#2
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,243 posts
  • Joined 02-August 03
Cmd promt I believe cannot handle arrays VBS does handle arrarys.
Note : Arrays always start at Zero

Example = Array("Some Message", "Some Text", "More Of Whatever","Test Array")
Msgbox Example(0) & vbcrlf & Example(1) & vbcrlf & Example(2), 0 + 32, Example(3)




GunSmokingMan



#3
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
cant i write a variable name using a variable? like in PHP?

#4
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,243 posts
  • Joined 02-August 03
I do not know about php, but you can use varibles in cmd.

Echo Off && CLS && Mode 55,5 && Color 5e
Set Var1=Hello World
Set Var2=ping -n 3 127.0.0.1
Set Var3=Good Bye
Echo.
echo %Var1%
%Var2%>nul
echo.
echo %Var3%
%Var2%>nul
exit




GunSmokingMan



#5
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
for %%G in (%location%\*.txt) do (
set /a count+=1
call :exec1 %%~nG
echo %%~nG
)
goto :continue1

:exec1
echo %count%
set mapArray[%count%]=%1
goto :end

:continue1
:end

this is what i have so far, the idea is to point to an array number (mapArray[1]) and return the file name.
i will ask for a number input, and return the file name.

#6
Martin Zugec

Martin Zugec

    MSFN Expert

  • Member
  • PipPipPipPipPipPip
  • 1,368 posts
  • Joined 24-January 04
Mine is similar :)

Set strArrayNumber=0

For /f "usebackq" %%i IN (`type test.txt`) DO (
	Set strArrayValue=%%i
	call:functionArray
	)
	
Goto :EOF

:functionArray
set /a strArrayNumber=%strArrayNumber% + 1
set strArrayName.%strArrayNumber%=%strArrayValue%
Goto :EOF

Like my opinions? Read Deployment/scripting blog
Like my scripts? They are under DrinkWare license :)

#7
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
looks good! :)
but how do i work with it next?
echo %strArrayName.%strArrayNumber%%
how do i output strArrayName.strArrayNumber?

#8
Martin Zugec

Martin Zugec

    MSFN Expert

  • Member
  • PipPipPipPipPipPip
  • 1,368 posts
  • Joined 24-January 04
echo %strArrayName.1%
will show first entry in array...

Variable %strArrayNumber% shows total number of array entries
Like my opinions? Read Deployment/scripting blog
Like my scripts? They are under DrinkWare license :)

#9
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
yes, but is there a way to write the "1" as a variable, so it can be processed in a loop?

#10
Martin Zugec

Martin Zugec

    MSFN Expert

  • Member
  • PipPipPipPipPipPip
  • 1,368 posts
  • Joined 24-January 04
I use it like

For /f "usebackq delims==. tokens=1-3" %%i IN (`set strArrayName`) DO Echo Array field number %%j have value %%k

Like my opinions? Read Deployment/scripting blog
Like my scripts? They are under DrinkWare license :)

#11
someuser77

someuser77

    Newbie

  • Member
  • 27 posts
  • Joined 11-May 05
thanks soulin! :thumbup
works like magic!

#12
Martin Zugec

Martin Zugec

    MSFN Expert

  • Member
  • PipPipPipPipPipPip
  • 1,368 posts
  • Joined 24-January 04
I am proudly serving my masters :D

BTW If you are intrested in batches, sometimes you can find something useful in my blog.
Like my opinions? Read Deployment/scripting blog
Like my scripts? They are under DrinkWare license :)

#13
JoeMSFN

JoeMSFN

    Member

  • Member
  • PipPip
  • 205 posts
  • Joined 28-September 04
WOW!!! I just stumbled upon this thread quite accidentally (forgot the original search.. more of a tangent.. funny how the net works like that :) )
:thumbup
@Martin Zugec
Thank you for this snippet:
set /a strArrayNumber=%strArrayNumber% + 1
:thumbup
@someuser77
Thank you for this snippet:
set /a count+=1
I've been doing the "just google it, you'll find it" and havent found squat... my interest was how to "increment an environment variable". You guys have answered a question I've had for about half a week. I didn't even realize there was more to this place than Unattended CDs!

Edited by JoeMSFN, 08 June 2005 - 12:44 AM.


#14
Noise

Noise

    Windows Guru

  • Member
  • PipPipPip
  • 419 posts
  • Joined 27-February 04
I have to agree, this is great stuff... Hell, why did I ever bother taking that vbs scripting class, I still use batch files :)

/runs off to check Martin's blog...