lffit
asked on
VB Script Chokes on $ Symbol
I am working on a vbscript to do some log parsing and I am now to the point where I am trying to do some special things. I am currently getting a windows script host issue when trying to call a variable containing a UNC path with a hidden folder declaration (notated by the $). The code snipped looks as follows:
The error message says:
Object required: '[string: "\\fileserver\filelocation "]'
Code: 800A01A8
It appears like storing a string with a $ in it is breaking my script. Any ideas how to escape that behavior?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileDest = "\\fileserver\filelocation$\folder\cleanstats.csv"
if objFileDest.FileExists then
set objFileW = objFSO.OpenTextFile(objFileDest, ForAppending)
else
Set objFileW = objFSO.CreateTextFile(objFileDest, ForWriting)
end if
The error message says:
Object required: '[string: "\\fileserver\filelocation
Code: 800A01A8
It appears like storing a string with a $ in it is breaking my script. Any ideas how to escape that behavior?
I assume you had ForAppending and ForWriting defined somewhere else in the script as well. If not, those will need to be handled, as in:
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
ASKER
You are correct with your updates to my code. That makes more sense, it is still dying at the $ though. Strange.
ASKER
Perhaps having the code in full context would help:
The concept is I open one file to read from and another file to append to. If the file to append to does not exist, then open a new file at the given location to write to. This is probably WAY simpler to do then I am making it.
Const ForReading = 1
Const ForWriting = 2
Const ForAppending =8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("\\fileserver01\filelocation$\folder\perfstats01.txt", ForReading)
Set objFileDest = "\\fileserver01\filelocation$\folder\cleanstats.csv"
if objFSO.FileExists (strFileDest) then
set objFileW = objFSO2.OpenTextFile(objFileDest, ForAppending)
else
Set objFileW = objFSO2.CreateTextFile(objFileDest, ForWriting)
end if
The concept is I open one file to read from and another file to append to. If the file to append to does not exist, then open a new file at the given location to write to. This is probably WAY simpler to do then I am making it.
Gee, that's weird. I can do this just fine:
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strFileDest = "\\fileserver\folder$\test file.txt"
Set objFileW = objFSO.OpenTextFile(strFil eDest, ForAppending, True)
objFileW.Close
It works fine. FYI, you can just use OpenTextFile, which will also create the file if it doesn't exist because of the last parameter being True. Otherwise, if the file does exist, it will open it for appending.
Regards,
Rob.
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.Fi
strFileDest = "\\fileserver\folder$\test
Set objFileW = objFSO.OpenTextFile(strFil
objFileW.Close
It works fine. FYI, you can just use OpenTextFile, which will also create the file if it doesn't exist because of the last parameter being True. Otherwise, if the file does exist, it will open it for appending.
Regards,
Rob.
I ran that exact code here with the changes and it got past the original problem. I did of course get the error below since that sharename doesn't exist for me.
c:\temp\EE26645786.vbs(6, 5) Microsoft VBScript runtime error: Path not found
~bp
c:\temp\EE26645786.vbs(6, 5) Microsoft VBScript runtime error: Path not found
~bp
What error are you getting now with the changed code?
~bp
~bp
ASKER
Rob is making me think I fat fingered something along the way typing the original code. The code i am posting is cleaned with specific references knocked out. Let me start from scratch like he did and see what happens. This could be a PEBKAC issue.
One problem, you need to change:
Set objFileDest = "\\fileserver01\filelocati on$\folder \cleanstat s.csv"
to
objFileDest = "\\fileserver01\filelocati on$\folder \cleanstat s.csv"
See my changed code.
~bp
Set objFileDest = "\\fileserver01\filelocati
to
objFileDest = "\\fileserver01\filelocati
See my changed code.
~bp
Well spotted Bill. That's correct. I didin't notice that. You can't use the Set keyword with a string like that. You could use
Set objFileDest = objFSO.GetFile("\\fileserv er01\filel ocation$\f older\clea nstats.csv ")
but that would be incorrect seeing as you're doing the FileExists test anyway....
Rob.
Set objFileDest = objFSO.GetFile("\\fileserv
but that would be incorrect seeing as you're doing the FileExists test anyway....
Rob.
Perhaps something more like this?
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const cSourceFile = "\\fileserver01\filelocation$\folder\perfstats01.txt"
Const cDestFile = "\\fileserver01\filelocation$\folder\cleanstats.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFile = objFSO.OpenTextFile(cSourceFile, ForReading)
Set objDestFile = objFSO.OpenTextFile(cDestFile, ForAppending)
' Do logic here...
objSourceFile.Close
objDestFile.Close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did that makes sense?
~bp
~bp
ASKER
There's what the issue was. That set was throwing the whole thing off.
SourceFile = "\\fileserver01\filelocation$\folder\perfstats01.txt"
DestFile = "\\fileserver01\filelocation$\folder\cleanstats.csv"
That corrected everything. Thanks for the help everyone.
Open in new window