Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB Script Chokes on $ Symbol

Posted on 2010-11-29
14
Medium Priority
?
459 Views
Last Modified: 2012-05-10
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:
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

Open in new window


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?
0
Comment
Question by:lffit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
14 Comments
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34234487
Try this, you have a few problems in that code.

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileDest = "\\fileserver\filelocation$\folder\cleanstats.csv"
if objFSO.FileExists(strFileDest) then
    set objFileW = objFSO.OpenTextFile(strFileDest, ForAppending)
  else
    Set objFileW = objFSO.CreateTextFile(strFileDest, ForWriting)
end if

Open in new window

0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34234501
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

Open in new window

0
 

Author Comment

by:lffit
ID: 34234555
You are correct with your updates to my code. That makes more sense, it is still dying at the $ though. Strange.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:lffit
ID: 34234573
Perhaps having the code in full context would help:
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

Open in new window


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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34234608
Gee, that's weird.  I can do this just fine:

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileDest = "\\fileserver\folder$\testfile.txt"
Set objFileW = objFSO.OpenTextFile(strFileDest, 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.
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34234609
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
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34234618
What error are you getting now with the changed code?

~bp
0
 

Author Comment

by:lffit
ID: 34234652
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.
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34234653
One problem, you need to change:

Set objFileDest = "\\fileserver01\filelocation$\folder\cleanstats.csv"

to

objFileDest = "\\fileserver01\filelocation$\folder\cleanstats.csv"

See my changed code.

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34234671
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("\\fileserver01\filelocation$\folder\cleanstats.csv")

but that would be incorrect seeing as you're doing the FileExists test anyway....

Rob.
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34234691
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

Open in new window

0
 
LVL 58

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 34234697
Oops, forgot the important TRUE that Rob mentioned...

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, True)
' Do logic here...
objSourceFile.Close
objDestFile.Close

Open in new window

0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34236107
Did that makes sense?

~bp
0
 

Author Comment

by:lffit
ID: 34238378
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"

Open in new window

That corrected everything. Thanks for the help everyone.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question