• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
Sign in to follow this  
Followers 0
someuser77

making arrays in batch

14 posts in this topic

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. :(

0

Share this post


Link to post
Share on other sites

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)

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

looks good! :)

but how do i work with it next?

echo %strArrayName.%strArrayNumber%%

how do i output strArrayName.strArrayNumber?

0

Share this post


Link to post
Share on other sites

echo %strArrayName.1%

will show first entry in array...

Variable %strArrayNumber% shows total number of array entries

0

Share this post


Link to post
Share on other sites

yes, but is there a way to write the "1" as a variable, so it can be processed in a loop?

0

Share this post


Link to post
Share on other sites

I use it like

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

0

Share this post


Link to post
Share on other sites

I am proudly serving my masters :D

BTW If you are intrested in batches, sometimes you can find something useful in my blog.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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...

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.