Link to home
Start Free TrialLog in
Avatar of Scott Lamond
Scott LamondFlag for United States of America

asked on

My First VB Script Error

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.
Avatar of Bill Prew
Bill Prew

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.

Avatar of Scott Lamond


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?
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".
Avatar of Bill Prew
Bill Prew

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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

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

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.