Solved

My First VB Script Error

Posted on 2013-01-03
9
388 Views
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.
holdmusic.vbs
0
Comment
Question by:slamond
  • 4
  • 4
9 Comments
 
LVL 51

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.

~bp
0
 

Author Comment

by:slamond
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?
0
 

Author Comment

by:slamond
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".
0
 
LVL 51

Accepted Solution

by:
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:

http://notepad-plus-plus.org/

~bp
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:slamond
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.

Thanks.
0
 
LVL 65

Expert Comment

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

Rob.
0
 
LVL 51

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:

http://www.vbsedit.com/

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
Next

Wscript.Echo FileName
Wscript.Quit

' 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
   Else 
      '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
Else
   '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

~bp
0
 

Author Comment

by:slamond
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.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38808230
Thanks.

~bp
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now