Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I use VB:s string.StartsWith in WScript? How?

Posted on 2009-05-14
2
Medium Priority
?
2,993 Views
Last Modified: 2012-05-07
Hi.

I have a script that I use to check folders for files using scheduled tasks.
If it finds files, it moves them to a sub folder and then alerts a user with an email.

I now have a few macs that access this file system and thus I have a lot of files starting with a "."

I'd like this script to automatically remove them.

As a java programmer, the logical choice was to search for a starts with function.
I found one in VB script, but I can not seem to get it to work.

Code below. The currentFileName echoes correctly, but when I try to use:
currentFileName.StartsWith(".") - I get a 800A01A8 error "object required".

Code below. (Commented out the actual moving and deleting of files + emailing for testing)

Many thanks in advance, and best regards,
David


Option Explicit
 
Dim Fso, args, num, folderPath, utrPath, strEmailFrom, strEmailTo, strEmailSubject, strSMTP, sendMail, strFilesInBody, currentFileName, myFolder, filecount, colFiles, foundFile
 
Set Fso = CreateObject("Scripting.FileSystemObject") 
 
Set args = WScript.Arguments
num = args.Count
 
if num < 3 then
   WScript.Echo "Du glömde ange parameter för mapp att kolla och/eller mottagarens mejladress och/eller meddelande." & vbCrLf & "Används: scriptnamn.vbs C:\Temp kalle@gp.se Meddelande" & vbCrLf & "Eventuella extra e-postadresser, lägg till ; mellan." & vbCrLf & "Ex: kalle@gp.se;nisse@gp.se"
   WScript.Quit 1
end if
 
folderPath = args.Item(0)
utrPath = folderPath & "\utredning\"
strEmailFrom = "automailer@gp.se"
strEmailTo = args.Item(1)
strEmailSubject = "*** LARM *** " & args.Item(2)
strSMTP = "mailhost.gp.se"
sendMail = 0
strFilesInBody = ""
currentFileName = ""
 
Set myFolder = Fso.GetFolder(folderPath)
filecount = myFolder.Files.Count
 
Set colFiles = myFolder.Files
For Each foundFile In colFiles
	Set currentFileName = Fso.GetFileName(foundFile)
	If currentFileName.StartsWith(".") Then
		WScript.echo "yes"
	End If
	
    If Len(Fso.GetExtensionName(foundFile)) <= 3 Then
		WScript.echo currentFileName
		'strFilesInBody = strFilesInBody +  currentFileName & vbCrLf
		'sendMail = 1
		'Fso.CopyFile foundFile, utrPath, true
		'Fso.DeleteFile(foundFile)
	Else
		WScript.echo currentFileName
		'Rensa mac-filer
		'filecount = filecount -1
		'Fso.DeleteFile(foundFile)
	End If
Next
strEmailBody = "Nu ligger det " & filecount & " filer i " & utrPath & vbCrLf & vbCrLf & strFilesInBody
 
Set objEmail = CreateObject("CDO.Message")
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        strSMTP
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
 
If sendMail > 0 Then
	objEmail.Send
End If
 
WScript.Quit 0

Open in new window

0
Comment
Question by:larsan
[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
2 Comments
 
LVL 10

Accepted Solution

by:
Marcjev earned 2000 total points
ID: 24392906
Remark: The GetFileName method returns a string and no object. Hence it has no methods in vbscript
You should write something like:
var currentFileName, firstchar
...
currentFileName = Fso.GetFileName(foundFile)
firstchar = left(currentfilename,1)

Or use some other string functions (search for vbscript string functions on the web)

Marc.
0
 

Author Closing Comment

by:larsan
ID: 31581814
Thanks!

In java-lingo a string is an object so I assumed that it was so in vb.

The code below works nicely:

...
currentFileName = Fso.GetFileName(foundFile)
firstChar = Left(currentFileName,1)
If StrComp(firstChar,".",1) = 0 Then ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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