Home » Questions » Computers [ Ask a new question ]

Subscript out of range error in VBScript script

Subscript out of range error in VBScript script

I'm trying to move my entire User folder in Windows Vista to a non-system partition. To do so with a minimum hassle I'm following the directions provided at Ben's Blog, specifically the VBScript he provides.

Asked by: Guest | Views: 386
Total answers/comments: 1
Guest [Entry]

"Except for the Jab at a beginner, I agree with Nilpo. Your issue is the 'arr(1)' is failing because the script is expecting this the second position in the array and it isn't there, which 99% of the time is an 'out of bounds' error. It may not always be in your control but that is typically a reference to an array bounds issue. Array positions start with zero. Ex. Array(0, 1, 2, 3, ...). So (1) is the second position. He is also right, it could be a lot cleaner but you wouldn't know if just starting with VBScript.

I think this is what you are looking for and it is cleaner. This will pull out the path between the brackets []. You never want to assume both brackets are there, even if it is a generated file, so I have an If/Then check for both before executing, otherwise it will skip to the next line and the end.

I did run this and it worked in capturing between the brackets. You may need to tweak some since I'm not sure exactly what you may want to see in the output. :)

Option Explicit

Dim fso : set fso = CreateObject(""Scripting.FileSystemObject"")
Dim fileIn : set fileIn = fso.OpenTextFile(""c:\users\MyFolder\desktop\input.txt"")
Dim fileOut : set fileOut = fso.OpenTextFile(""c:\users\MyFolder\desktop\output.txt"", 2, true) ' for writing/create
Dim line
Dim arr

Do Until fileIn.AtEndOfStream
line = fileIn.ReadLine
if InStr(line, ""["") > 0 And InStr(line, ""]"") > 0 then
arr = Split(Split(line, ""["")(1), ""]"")

fileOut.WriteLine arr(0) & ""\""
end if
Loop

fileIn.Close
fileOut.Close

Set fso = Nothing
Set fileIn = Nothing
Set fileOut = Nothing
Set line = Nothing
Set arr = Nothing"