My First VB Script Error

Posted on 2013-01-03
Last Modified: 2013-01-22
I'm teaching myself VB Script using online references and thought it would help to use it in creating a practical solution that can be used on our network.
I had the idea of creating a file that is run by the Windows Task Scheduler every morning at 7 a.m., checks the current system date and uses that information
to change the playlist that is used for our telephone system's hold music. This would automatically turn on Christmas music on the day after Thanksgiving, turn
on patriotic music around the 4th of July, etc.

A Shortcut to the every day/non-holiday music playlist (StandardHoldMusic.m3u) is stored in the user "holdpc" 's Startup folder so that it automatically
opens Winamp and starts playing when the user is logged in (done automatically every 30 minutes by a thin-client machine, to automatically clear any program hangs, etc.)

The logic in the script is to first delete the current playlist shortcut from the Startup folder.
Then the script assigns the every day/non-holiday music (StandardHoldMusic.m3u) to a variable as the default.
Then it cycles through a bunch of If Then clauses reassigning a different playlist if the current date falls within a holiday date range.
Finally it will write the shortcut to the Startup folder.

When executed I am getting the error code 800A01F5, Illegal Assignment "DestinationFile" at Line 61, Char 10.
For clarification purposes, the error seems to be occurring within the TEST section in which I'm using today's date to
test that it's working.

I'm sure it's a simple typo but I need another pair of eyes to check it.
The script is attached.
Question by:slamond
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
  • 4
  • 4
LVL 54

Expert Comment

by:Bill Prew
ID: 38741385
You have the following earlier in the script:

Const DestinationFile = "c:\Documents and Settings\holdpc\Start Menu\Programs\Startup\Shortcut to StandardHoldMusic.m3u"
Const SourceFile = "m:\My Playlists\shortcuts\Shortcut to StandardHoldMusic.m3u"

and then line 61 is:

DestinationFile = "c:\Documents and Settings\holdpc\Start Menu\Programs\Startup\Shortcut to Patriotic.m3u"

You can't do that.  If you define a variable as a CONST, then it cannot be assigned a different value later.  In your case it looks like you should just remove the CONST from the first lines for any variables that you will be changing the value of later.

I didn't read your whole script since this was the focus of the question you asked, so not sure if there are any other logic issues in it.


Author Comment

ID: 38741580
I fixed that and now I'm getting an error 800A0035, File Not Found, Line 87, Char 6.

I'm also confused as to how to find line 87.
Do they count the REM lines?
Is there an easier way to find line 87 other than arrowing-down and counting all 87?

Author Comment

ID: 38741610
I forgot to mention that when I counted down 87 lines I came to an END IF statement, which makes no sense wnen considering the error message "File Not Found".
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

LVL 54

Accepted Solution

Bill Prew earned 500 total points
ID: 38741735
Yes, remark lines count, as do blank lines.  In your original script it looks like line 87 was :

fso.CopyFile SourceFile, "c:\Documents and Settings\holdpc\Start Menu\Programs\Startup\", True

I would suspect that the file referenced in SourceFile can not be found.

Get yourself a better editor than Notepad which has line numbers.  A great free editor for programming is Notepad++ at:


Author Comment

ID: 38741964
The problem was that I had been omitting the .lnk file extension in every reference to a shortcut.

The script now runs without error and the TEST loop assigns the correct file and it was successfully copied to the Startup folder.

I will leave this open for a few days in case I come across another error while making my final additions to the script.

LVL 65

Expert Comment

ID: 38742104
I would also recommend VBSEdit for VBScript editing.  It includes line numbering, and auto-complete options, and other helpful stuff!

LVL 54

Expert Comment

by:Bill Prew
ID: 38742636
VBSEdit is a great tool as well, although if you really do use it you should pay for it.  You can find it at:

Okay, I took a look at your code, and since you are starting out I figured I'd share a different approach to what you are trying to accomplish.  I apologize a bit in advance, this may be "throwing you in the deep end of the pool" a bit, but I'm hoping there are some things that you might find interesting or educational.

I try not to repeat chunks of code several times if I can avoid it, and so looking at what you were doing, the basic idea seemed to be this to me.  You want to have some playlists that are used when the current month and day fall into certain ranges.  So rather than repeat to IF statements many times, I took the from and to month, day, and weekday values and placed them in a "table" (actually an array in VBS), and indicated the special playlist name for that date range.  This is much easier to update if you want to add a new special playlist in the future, uses less code, and I hope is easier to read and understand.

I tried to replicate the date ranges in your original code in the table version, but it seemed like there were some odd thing there, so you will likely want to review and adjust the table, but that will be easy once you grasp the approach.

I also separated out the folder paths to only be coded in one place as a Const, that way if/when they change the code is a lot easier to update.  Take a read and let me know what questions you have, the comments should give you the basic idea, but feel free to ask about what isn't clear.

I do use an "array of arrays" but that isn't really that hard a thing to grasp.  Just think of Excel worksheet, where there are several columns of data (my inner array), and then there can be many rows of that same data (my outer array). That being said, don't feel bad if it's not easy or obvious to grasp right off, it is a bit more on the advanced side.

' Delete shortcut in holdpc's Startup folder
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("c:\Documents and Settings\holdpc\Start Menu\Programs\Startup\Shortcut*"), DeleteReadOnly

' Define folder locations for files
Const DestFileDir = "c:\Documents and Settings\holdpc\Start Menu\Programs\Startup\"
Const SourceFileDir = "m:\My Playlists\shortcuts\"

' Define list of dates to use 'special' playlists
' Sub-array elements are (from-month, to-month, from-day, to-day, from-weekday, to-weekday, file-name)
arrSeasonal = Array( Array( 1,  1,  2,  4, 0, 9, "Shortcut to Testing"), _
                     Array( 3,  3, 13, 17, 0, 9, "Shortcut to StPatricksDay"), _
                     Array( 7,  7,  2,  4, 0, 9, "Shortcut to Patriotic"), _
                     Array(11, 11, 21, 25, 5, 9, "Shortcut to ChristmasHoldMusic"), _
                     Array(11, 11, 26, 99, 0, 9, "Shortcut to ChristmasHoldMusic"), _
                     Array(12, 12,  1, 25, 0, 9, "Shortcut to ChristmasHoldMusic"), _
                     Array(12, 12, 26, 99, 0, 9, "Shortcut to NewYearsHoldMusic") )

' Assume the default playlist
FileName = "Shortcut to StandardHoldMusic"

' Get current date values
CurrMonth = Month(Date())
CurrDay = Day(Date())
CurrWeekday = Weekday(Date())

' Search list of special playlists, if found get file name
For Each arrRange in arrSeasonal
   If CurrMonth   >= arrRange(0) And CurrMonth   <= arrRange(1) And _
      CurrDay     >= arrRange(2) And CurrDay     <= arrRange(3) And _
      CurrWeekday >= arrRange(4) And CurrWeekday <= arrRange(5) Then
      FileName = arrRange(6)
   End If

Wscript.Echo FileName

' Copy shortcut to holdpc's Startup folder

' Set full paths for files involved
SourceFile = SourceFileDir & FileName & ".m3u"
DestFile = DestFileDir & FileName & ".m3u"

' Check to see if the file already exists in the destination folder
If objFSO.FileExists(DestFile) Then
   'Check to see if the file is read-only
   If Not objFSO.GetFile(DestFile).Attributes And 1 Then 
      'The file exists and is not read-only.  Safe to replace the file.
      objFSO.CopyFile SourceFile, DestFileDir, True
      'The file exists and is read-only.
      'Remove the read-only attribute
      objFSO.GetFile(DestFile).Attributes = objFSO.GetFile(DestFile).Attributes - 1
      'Replace the file
      objFSO.CopyFile SourceFile, DestFileDir, True
      'Reapply the read-only attribute
      objFSO.GetFile(DestFile).Attributes = objFSO.GetFile(DestFile).Attributes + 1
   End IF
   'The file does not exist in the destination folder.  Safe to copy file to this folder.
   objFSO.CopyFile SourceFile, DestFileDir, True
End IF

Set objFSO = Nothing

Open in new window


Author Comment

ID: 38743735
Great, thanks. I'll digest and ask questions if needed ASAP.
I've had lots of experience with arrays in Fortran, Datatrieve, and much more recently with Pearl and other languages in college courses only. So I understand the multidimensional concept.
LVL 54

Expert Comment

by:Bill Prew
ID: 38808230


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

740 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