Jump to content

Loop through an array


tes5884
 Share

Recommended Posts

The script is looping through a csv file with old pc names. It's then supposed to rename the pc's base on the service tag.

It seems to broken at the section where it loops through the array.

What am I doing wrong?

Thanks!!

#include <Constants.au3>
#include <Array.au3>
#include <CSV.au3>


$pid = Run("wmic bios get serialnumber", "", "", $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($pid)
$std = StdoutRead($pid)
$tag = StringRight(StringStripWS($std, 8), 7)


Local $dpt = "DPT"
Local $nPcName = $dpt & "-" & $tag


$oPcName = _ParseCSV("test.csv")

For $i = 0 To UBound($oPcName)
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i] & " /NewName:" & $nPcName & " /Force")
Next
Link to comment
Share on other sites

I'm assuming when you say broken it means you're getting an array subscript error? If so, use For $i = 0 to Ubound($oPCName) - 1

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Tried that, still got error below.

ServiceTag.au3 (19) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i] & " /NewName:" & $nPcName & " /Force")
Run(@ComSpec & " /k netdom renamecomputer " & ^ ERROR

The CSV file contains the following values;

tzvi-pc
test-pc

Thanks!

Link to comment
Share on other sites

  • Developers

Well,

With the provided info that should fix it since your array only has 0 & 1 so shown in the arraydisplay.

Post the latest version of your script with the arraydisplay before the ForNext loop so we can see what you are testing with.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Add this after the line "For $i = 0 To UBound($oPcName) - 1"

ConsoleWrite("$i = " & $i & @CRLF)

Run your script from Scite and report the output from the console window at the bottom of the scite screen.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You have a 2-dimensional array, so you have to reference the elements differently.

UBound($oPcName) returns 2 because the array is 2-dimensional. (Not the row count, as you were expecting)

UBound($oPcName,1) also returns 2 because you have two lines in the CSV file.

Try this:

For $i = 0 To UBound($oPcName,1) -1
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i][0] & " /NewName:" & $nPcName & " /Force")
Next

Edit: Missed the -1

Edited by Skruge

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

This is the exact and entire script at this time.

#include <Constants.au3>
#include <Array.au3>
#include <CSV.au3>


$pid = Run("wmic bios get serialnumber", "", "", $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($pid)
$std = StdoutRead($pid)
$tag = StringRight(StringStripWS($std, 8), 7)


Local $dpt = "DPT"
Local $nPcName = $dpt & "-" & $tag


$oPcName = _ParseCSV("test.csv")
_ArrayDisplay($oPcName)

For $i = 0 To UBound($oPcName) -1
ConsoleWrite("$i = " & $i & @CRLF)
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i] & " /NewName:" & $nPcName & " /Force")
Next
Link to comment
Share on other sites

@Skruge it worked! Thank you all for helping!

This is the working edition if anyone else has the same problem..

#include <Constants.au3>
#include <Array.au3>
#include <CSV.au3>


$pid = Run("wmic bios get serialnumber", "", "", $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($pid)
$std = StdoutRead($pid)
$tag = StringRight(StringStripWS($std, 8), 7)


Local $dpt = "DPT"
Local $nPcName = $dpt & "-" & $tag


$oPcName = _ParseCSV("test.csv")
_ArrayDisplay($oPcName)

For $i = 0 To UBound($oPcName, 1)
ConsoleWrite("$i = " & $i & @CRLF)
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i][0] & " /NewName:" & $nPcName & " /Force")
Next
Edited by tes5884
Link to comment
Share on other sites

The wicked two-headed array strikes again...

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

I missed the Col1 in the array display, good catch.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This is the working edition if has the same problem..

I'm not sure from your phrasing if you're still having issues, but the code you posted is missing the -1 after UBound. (I edited my post but it looks like you got the original)

If you still need help, let us know.

Cheers!

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

I'm not sure from your phrasing if you're still having issues, but the code you posted is missing the -1 after UBound. (I edited my post but it looks like you got the original)

If you still need help, let us know.

Cheers!

Fixed. It works perfectly thanks..

This is the working edition if anyone else has the same problem..

Link to comment
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
 Share

×
×
  • Create New...