This piece of script will echo each key starting with "1", but I then need
it to loop and show "2" etc etc. I have tried adding a Loop function, with
an incremental counter i.e. n=0, n=n+1, but it's not quite doing what I need.
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "1_file.txt", "1"
objDictionary.Add "2_file.txt", "2"
objDictionary.Add "3_file.txt", "3"
colKeys = objDictionary.Keys
For Each strKey in colKeys
if Left (strKey,1) = n then Wscript.Echo mid(strkey,InStr(strKey,"_")+1)
Next
colItems = objDictionary.Items
For Each strItem in colItems
if Left (strItem,1) = "1" then Wscript.Echo strItem
Next
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "1_file.txt", "1"
objDictionary.Add "2_file.txt", "2"
objDictionary.Add "3_file.txt", "3"
colKeys = objDictionary.Keys
For Each strKey in colKeys
if Left (strKey,1) = "1" then Wscript.Echo mid(strkey,InStr(strKey,"_")+1)
Next
colItems = objDictionary.Items
For Each strItem in colItems
if Left (strItem,1) = "1" then Wscript.Echo strItem
Next
Sub EnumerateKeysStartingWith(n) 'see below
Dim colKeys
'Your code:
colKeys = objDictionary.Keys
For Each strKey in colKeys
if Left (strKey,1) = n then Wscript.Echo
mid(strkey,InStr(strKey,"_")+1)
Next
End Sub
Dim n : n = 0
Do While EnumerateKeysStartingWith(n)
Loop
You said you only wanted to examine keys, so I skipped this part:
Based on your code (absolutely untested, though, but the solution should
look something like this):
'---------------------------
Function EnumerateKeysStartingWith(n) 'Returns Boolean
'returns if there was at least one key starting with n
Dim colKeys, bKeyExist
bKeyExist = False
colKeys = objDictionary.Keys
For Each strKey in colKeys
If Left (strKey,1) = "1" then
Wscript.Echo mid(strkey,InStr(strKey,"_")+1)
bKeyExist = True
End If
Next
EnumerateKeysStartingWith = bKeyExist
End Function 'EnumerateKeysStartingWith
Dim n : n = 1
Do While EnumerateKeysStartingWith(n)
Loop
'----------------------------
A slightly different approach...
dim strKey, colKeys, colItems, strArr
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "1_file.txt", "1"
objDictionary.Add "2_file.txt", "2"
objDictionary.Add "3_file.txt", "3"
colKeys = objDictionary.Keys
colItems = objDictionary.Items
For Each strKey in colKeys
strArr = split(strKey,"_")
Wscript.Echo colItems(strArr(0)-1) & ". " & strArr(1)
next
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
The output from this script will eventually make up the body of an alerting
e-mail. What I need the script to do is write a line:
"Files Transfered from Server A to Server B"
And then list all of the file names starting with "1", then write another
line,
Files Transfered from Server C to Server D"
And then list all of the file names starting with "2" etc etc.
I'm happy that I can do all of this - apart from reading the file names from
the dictionary in an incremental way. I have been experimenting with n=1,
n=n+1 with a loop, but it is only recognising the initial n=1. Even though I
can echo n with a value of 2, it is not picking up the dictionary items that
relate to it.
Will this help?
Sorting a Scripting Dictionary Populated with String Data
http://support.microsoft.com/support/kb/articles/Q246/0/67.ASP
: Thanks for your post, but it's still not quite doing what I need.
:
: The output from this script will eventually make up the body of an
alerting
: e-mail. What I need the script to do is write a line:
: "Files Transferred from Server A to Server B"
: And then list all of the file names starting with "1", then write another
: line,
: Files Transfered from Server C to Server D"
: And then list all of the file names starting with "2" etc etc.
: I'm happy that I can do all of this - apart from reading the file names
from
: the dictionary in an incremental way. I have been experimenting with n=1,
: n=n+1 with a loop, but it is only recognising the initial n=1. Even
though I
: can echo n with a value of 2, it is not picking up the dictionary items
that
: relate to it.
Joe...
First, the conversation is to be read from top to bottom.
Second, if you're going to change the rules in the middle of the game, I'm
going to get more confused. This is the first I have read about "Files
transferred from Server A to Server B", etc. There is no connectivity in
this script to any remote server.
You are using a dictionary which appears you do not need to do. You have
filenames with begin with numbers, used as keys, that match item numbers.
That's redundant.
Perhaps you should state your FULL goal and someone can direct you to where
you need to be and then perhaps you can obtain help with your scripting.
Currently I have no way to associate servers and filenames with preceding
numbers.
"Jo Winchester" <JoWinc...@discussions.microsoft.com> wrote in message
news:A25184A1-0F0E-4F2F...@microsoft.com...