Link to home
Start Free TrialLog in
Avatar of Glenn M
Glenn MFlag for United States of America

asked on

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

Avatar of Bill Prew
Bill Prew

What statement is giving you and error, and what is the error?

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


»bp
Avatar of Glenn M

ASKER

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.
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
Avatar of Glenn M

ASKER

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.
Avatar of Glenn M

ASKER

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

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
Avatar of Glenn M

ASKER

Yeah, that's a copy/ paste
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Glenn M

ASKER

...you're welcome to clean that up, for the sake of your eyes. :)
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
Avatar of Glenn M

ASKER

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.
Avatar of Glenn M

ASKER

sure... that's the extra mile but would certainly be appreciated too!
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
Unless the filter you are using is always just a specific extension, then that wouldn't be too bad.  Let me know...


»bp
Avatar of Glenn M

ASKER

I'm pleased with the results. I think PROPS to you and we call it a day. :) Cool?
That's fine, glad that was useful.


»bp
Avatar of Glenn M

ASKER

Script reformed and resolved. Well done!