VB Script can't seem to read long file names or names with spaces in it.

VB Script is choking on the long file name format we are using. The portion "1. Awaiting Director Signature" hangs up the script, as it has too many spaces and obviously exceeds formating. How can overcome this naming issue, without renaming the folder?


arrFolders = Array( "\\company\Shares\Departments\Information_Technology\Public\Purchasing\1. Awaiting Director Signature" )

Open in new window

LVL 2
Glenn MSystems Engineer / ManagerAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Okay, I see what the spaces would cause a problem, try this, it should take care of those...

arrFolders = Array( "\\company\Shares\Departments\Information_Technology\Public\Purchasing\1. Awaiting Director Signature\" )

 
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBlatPath = "Blat.exe"
strServer = "192.168.0.1"
strTo = "me@domain.com"
strFrom = "PO.Notifications@domain.com"
strSubject = "ATTENTION: PO(s) pending, that require your attention." 
intFolder = 1
For Each strFolder In arrFolders
	strBody = "See below:"
    intCount = -1
    strFolderSpec = ""
    strFileSpec = ""
    If InStr(strFolder, ";") > 0 Then
        strFolderSpec = Split(strFolder, ";")(0)
        strFileSpec = Split(strFolder, ";")(1)
    Else
        strFolderSpec = strFolder
        strFileSpec = Null
    End If
    intCount = CountFiles(strFolderSpec, strFileSpec)
    If strFileSpec <> "" Then
        strBody = strBody & vbCrLf & strFolderSpec & " has " & intCount & " " & strFileSpec & " PO(s) that require your attention."
    Else
        strBody = strBody & vbCrLf & strFolderSpec & " has " & intCount & " PO(s) that require your attention."
    End If
    If intCount > 0 Then
		strDateTime = Right("0" & Day(Date), 2) & Right("0" & Month(Date), 2) & Year(Date) & "_" & Right("0" & Hour(Time), 2) & Right("0" & Minute(Time), 2)
	    strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & strDateTime & "_" & intFolder & ".txt"
	    Set objBody = objFSO.CreateTextFile(strBodyFile, True)
	    objBody.Write strBody
	    objBody.Close
	    Set objBody = Nothing
	    strCommand = "cmd /c " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
        objShell.Run strCommand, 0, True
	    'objFSO.DeleteFile strBodyFile, True
	    intFolder = intFolder + 1
	Else
		strDateTime = Right("0" & Day(Date), 2) & Right("0" & Month(Date), 2) & Year(Date) & "_" & Right("0" & Hour(Time), 2) & Right("0" & Minute(Time), 2)
	    strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & strDateTime & "_" & intFolder & ".txt"
	    Set objBody = objFSO.CreateTextFile(strBodyFile, True)
	    objBody.Write strBody
	    objBody.Close
	    Set objBody = Nothing
	    intFolder = intFolder + 1
    End If
Next
 
Set objFSO = Nothing
Set objShell = Nothing
WScript.Echo "Finished"
 
Function CountFiles(ByVal strTheFolder, ByVal strFileType)
        Set objShell = CreateObject("WScript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Const intForReading = 1
        strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
        If IsNull(strFileType) = False Then
                strCommand = "C:\windows\system32\cmd.exe /c dir /a-d /s /b " & Quote(objFSO.GetFolder(strTheFolder).ShortPath & "\" & strFileType) & " > " & Quote(strTempFile)
        Else
                strCommand = "C:\windows\system32\cmd.exe /c dir /a-d /s /b " & Quote(objFSO.GetFolder(strTheFolder).ShortPath) & " > " & Quote(strTempFile)
        End If
        objShell.Run strCommand, 0, True
        Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
        intCount = 0
        While Not objFile.AtEndOfStream
                objFile.SkipLine
                intCount = intCount + 1
        Wend
        objFile.Close
        objFSO.DeleteFile strTempFile, True
        CountFiles = intCount
End Function

' Add surrounding double quotes to a string
Function Quote(s)
   Quote = Chr(34) & s & Chr(34)
End Function

Open in new window


»bp
1
 
Bill PrewCommented:
What statement is giving you and error, and what is the error?

As it stands that doesn't look like an invalid filename.


»bp
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
No Errors
The entire script is a file count for each DIR in the array. In this case, there just one location in the array. If I back up the statement to this, it reads all files and counts perfectly:

arrFolders = Array( "\\company\Shares\Departments\Information_Technology\Public\Purchasing\" )

Open in new window


IF I include the subDIR of "1. Awaiting Director Signature", the count is ZERO files.

arrFolders = Array( "\\company\Shares\Departments\Information_Technology\Public\Purchasing\1. Awaiting Director Signature" )

Open in new window


I have narrowed the problem down to the NAME of the DIR. unfortunately, I am stuck with the naming convention. If you need to see the entire script, I'm happy to share. All I know is that it is definitely in the naming convention of these folders.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Bill PrewCommented:
Yes, seeing the script would be helpful.  

How long are the file names under that folder?

And are you chasing subfolders of it, or just files?


»bp
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
At the moment, all files in the DIR are 8.3 format.
BUT...File names could be long and with spaces too. The DIR names are long and with spaces.
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
arrFolders = Array( "\\company\Shares\Departments\Information_Technology\Public\Purchasing\1. Awaiting Director Signature\" )

 
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBlatPath = "Blat.exe"
strServer = "192.168.0.1"
strTo = "me@domain.com"
strFrom = "PO.Notifications@domain.com"
strSubject = "ATTENTION: PO(s) pending, that require your attention." 
intFolder = 1
For Each strFolder In arrFolders
	strBody = "See below:"
    intCount = -1
    strFolderSpec = ""
    strFileSpec = ""
    If InStr(strFolder, ";") > 0 Then
        strFolderSpec = Split(strFolder, ";")(0)
        strFileSpec = Split(strFolder, ";")(1)
    Else
        strFolderSpec = strFolder
        strFileSpec = Null
    End If
    intCount = CountFiles(strFolderSpec, strFileSpec)
    If strFileSpec <> "" Then
        strBody = strBody & vbCrLf & strFolderSpec & " has " & intCount & " " & strFileSpec & " PO(s) that require your 

attention."
    Else
        strBody = strBody & vbCrLf & strFolderSpec & " has " & intCount & " PO(s) that require your attention."
    End If
    If intCount > 0 Then
		strDateTime = Right("0" & Day(Date), 2) & Right("0" & Month(Date), 2) & Year(Date) & "_" & Right("0" & 

Hour(Time), 2) & Right("0" & Minute(Time), 2)
	    strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & strDateTime & "_" & intFolder & 

".txt"
	    Set objBody = objFSO.CreateTextFile(strBodyFile, True)
	    objBody.Write strBody
	    objBody.Close
	    Set objBody = Nothing
	    strCommand = "cmd /c " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & 

" -f " & strFrom & " -s """ & strSubject & """"
        objShell.Run strCommand, 0, True
	    'objFSO.DeleteFile strBodyFile, True
	    intFolder = intFolder + 1
	Else
		strDateTime = Right("0" & Day(Date), 2) & Right("0" & Month(Date), 2) & Year(Date) & "_" & Right("0" & 

Hour(Time), 2) & Right("0" & Minute(Time), 2)
	    strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & strDateTime & "_" & intFolder & 

".txt"
	    Set objBody = objFSO.CreateTextFile(strBodyFile, True)
	    objBody.Write strBody
	    objBody.Close
	    Set objBody = Nothing
	    intFolder = intFolder + 1
    End If
Next
 
Set objFSO = Nothing
Set objShell = Nothing
WScript.Echo "Finished"
 
Function CountFiles(ByVal strTheFolder, ByVal strFileType)
        Set objShell = CreateObject("WScript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Const intForReading = 1
        strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
        If IsNull(strFileType) = False Then
                strCommand = "C:\windows\system32\cmd.exe /c dir /a-d /s /b " & objFSO.GetFolder

(strTheFolder).ShortPath & "\" & strFileType & " > """ & strTempFile & """"
        Else
                strCommand = "C:\windows\system32\cmd.exe /c dir /a-d /s /b " & objFSO.GetFolder

(strTheFolder).ShortPath & " > """ & strTempFile & """"
        End If
        objShell.Run strCommand, 0, True
        Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
        intCount = 0
        While Not objFile.AtEndOfStream
                objFile.SkipLine
                intCount = intCount + 1
        Wend
        objFile.Close
        objFSO.DeleteFile strTempFile, True
        CountFiles = intCount
End Function

Open in new window

0
 
Bill PrewCommented:
You have some funny line breaks in that code, is that just the way you pasted it here?

        strBody = strBody & vbCrLf & strFolderSpec & " has " & intCount & " " & strFileSpec & " PO(s) that require your 

attention."

Open in new window



»bp
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Yeah, that's a copy/ paste
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
...you're welcome to clean that up, for the sake of your eyes. :)
0
 
Bill PrewCommented:
And actually, if it were me, I would get the file count in VBS, not by shelling to DOS and then reading the test output.  Is that something you would be interested in?


»bp
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Perfect! The output is correct:

See below:
\\company\Shares\Departments\Information_Technology\Public\Purchasing\1. Awaiting Director Signature\ has 5 PO(s) that require your attention.
1
 
Glenn MSystems Engineer / ManagerAuthor Commented:
sure... that's the extra mile but would certainly be appreciated too!
0
 
Bill PrewCommented:
Actually, I take that back.  Getting the total of all files in the subfolder tree is very easy in VBS.  But I noticed you also want to be able to filter that and perhaps get the count of say just the *.TXT files.  That would be a little more work in VBS.  Doable, just more work, and the "filter" syntax would likely need to be slightly different.

As you have a solution to your posted problem I'll stop at that and be happy we got that worked out.

Also, note the Quote() function, it's how I prefer to stay sane when I need to quote things in quoted strings, etc.


»bp
0
 
Bill PrewCommented:
Unless the filter you are using is always just a specific extension, then that wouldn't be too bad.  Let me know...


»bp
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
I'm pleased with the results. I think PROPS to you and we call it a day. :) Cool?
0
 
Bill PrewCommented:
That's fine, glad that was useful.


»bp
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Script reformed and resolved. Well done!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.