Question

Hello guys I have an issue or issues with my code above I'm trying to get "sExtension" to be search in a different folder other that the one I'm using to save my script since this script will be use as a Startup Script on many computers

(It works only if I run the script in the same folder "sExtension", "ExtAssign.txt" and sComputername are otherwise it wont find the path)

This is what it should do Read a file called "ExtAssign.txt" (There is a full list of computer names in that file) and if it find the computer name on that file then it should copy a file with the with the extension number assigned to that computer name from a file server to "C:\" Drive

For this example I'm trying to do this locally, If I can make it then I'll try it from my File Server

Set objFSO    = CreateObject("Scripting.FileSystemObject")
set oFso       = CreateObject("Scripting.FileSystemObject")
Set objFS      = CreateObject("Scripting.FileSystemObject")
Set fso        = CreateObject("Scripting.FileSystemObject")
set oShell     = WScript.CreateObject("WScript.Shell")
set oShellEnv  = oShell.Environment("Process")
Set folder     = Fso.GetFolder("C:\Users\XXXXX\Desktop\Test\Extensions\")
Set wshshell   = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set ObjEnv     = WshShell.Environment("Process")
Set objFso     = WScript.CreateObject("Scripting.FileSystemObject")
Scomputername  = ObjEnv("COMPUTERNAME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")

Dim strFile
'File to scan
strFile = "C:\Users\XXXXX\Desktop\Test\Extensions\Extassign\ExtAssign.txt"

Dim strPattern
'Look for computer name in file
strPattern = scomputername

Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
 Dim strLine
    'Read each line and store it in strLine
    strLine = objFile.ReadLine
    'If the line matches the computer name, save the line to ExtArray
    If InStr(strLine,strPattern)>0 Then
        Dim ExtArray
        'Split the line and separate the extension
        ExtArray = Split(strLine,"|", -1, 1)
    Dim sExtension
        'Save the extension to sExtension
        sExtension=ExtArray(1)
    End If
Loop
'If the sExtension is empty, computer was not found, send message and terminate script.
If sExtension="" Then
WScript.Echo "ERROR: Computer "& scomputername &" not found in Extension Assignment List, so no extension has been set.  Avaya will not be launched.  Please contact your IT department for assistance."
Else
    'If the sExtension contains a number, Copy that file to C:\ and rename it to Config.xml
    fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
End If

at the end it if it finds the file sExtension it will rename it to Config.xml but it wont do it unless I run the script in the same folder sExtension and sComputername.

I get File not found error

Thank you in advance and Happy new year!

Was it helpful?

Solution

The culprit is most likely this line:

fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True

There is a trailing space after the last backslash in the path, so you're creating a path

C:\Users\XXXXX\Desktop\Test\Extensions\ 12345
                                       ^

when you actually want a path

C:\Users\XXXXX\Desktop\Test\Extensions\12345

On a more general note: why are you creating 7(!) FileSystemObject instances (replacing one of them three times on top of that)? And 3(!) WScript.Shell instances? You don't even use most of them, not to mention that you don't need the Shell object in the first place. You only use it for determining the computer name, which could be done just fine using the WScript.Network object (that you don't use at all).

Also, please don't ever use comments like this:

'Read each line and store it in strLine
strLine = objFile.ReadLine

It's quite obvious that you read each line and assign it to the variable strLine. Comments shouldn't rephrase what you're doing (the code already does that, at least when you're using speaking variable and function names), but why you're doing it, i.e. what the purpose of a particular code section is.

Your code could be reduced to something as simple as this:

Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")

computername = net.ComputerName
foldername   = "C:\Users\XXXXX\Desktop\Test\Extensions"
filename     = fso.BuildPath(foldername, "Extassign\ExtAssign.txt")

Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
  line = f.ReadLine
  If InStr(line, computername) > 0 Then
    arr = Split(line, "|", -1, 1)
    If UBound(arr) >= 1 Then extension = arr(1)
  End If
Loop
f.Close

If IsEmpty(extension) Then
  WScript.Echo "ERROR: Computer "& computername &" not found in ..."
Else
  fso.CopyFile fso.BuildPath(foldername, extension), "C:\Config.xml", True
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top