?
Solved

text compare fails for filename from Linux FTP server in vbscript

Posted on 2009-02-23
8
Medium Priority
?
434 Views
Last Modified: 2013-11-10
I cannot understand how using the attached code I am getting these results.
The code connects to an internal FTP server running RHEL and downloads a csv file put by our AS400.  The script connects, looks for the wanted file download then renames it on the FTP server and drops it to the %temp% folder.  It is then supposed to see if it exists then email it (which I left out for simplicity) to a user.  The problem I cannot figure out is that when it is dropped in the temp folder it cannot be found by the script.  The file is indeed there and I can even enumerate with the script but a text compare of the filenames does not match.

Captured output:
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Number of files found = 4
consumption.csv was found in Consumption.CSV
Ready to Send: C:\Users\marsha\AppData\Local\Temp\Consumption.CSV
I DO NOT see the file. WTF?

consumption.csv=bcgc16.tmp      The strings are NOT equal.
consumption.csv=consumption.csv      The strings are NOT equal. (Huh?)
consumption.csv=cvr1ad3.tmp.cvr      The strings are NOT equal.

Runtime: 2/23/2009 2:50:03 PM
Exit code: 0 , 0000h

Dim strSite,strUsername,strPassword,strRemotePath,strRemoteFile,strLocalpath,varGetFTP,strFileToSend
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
 
strSite = "ftp.somewhere.com"
strUsername = "userid"
strPassword = "password"
strRemotePath = "/somefolder"
strRemoteFile = "some.csv"
 
varGetFTP = FTPList(strSite, strUsername, strPassword, strRemotePath, strRemoteFile)
If varGetFTP <> "NoFTPFiles" Then
	strRemoteFile = varGetFTP
	varGetFTP = FTPDownAndRename(strSite,strUsername,strPassword,strRemotePath,strRemoteFile)
Else
	WScript.Quit(1)
End If
 
If varGetFTP <> True Then
	WScript.Echo "Nothing to download"
	WScript.Quit(2)
Else
	strFileToSend = objShell.ExpandEnvironmentStrings("%TEMP%") & "\" & strRemoteFile
	WScript.Echo "Ready to Send: " & strFileToSend
 
	If objFSO.FileExists(strFileToSend) Then
		WScript.Echo "I see the file."
	Else
		WScript.Echo "I do not see the file. WTF?"
		Set folder = objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%TEMP%"))
		Set files = folder.Files
		For Each file In files
			intCompare = StrComp(lcase(strRemoteFile), lcase(file.name), vbTextCompare)
			If intCompare = 0 Then
			    Wscript.Echo lcase(strRemoteFile) & " = " & lcase(file.name) & vbTab & vbTab & "The strings are equal."
			Else
			    Wscript.Echo lcase(strRemoteFile) & " <> " & lcase(file.name) & vbTab & vbTab & "The strings are NOT equal."
			End If
		Next
	End If
End If
WScript.Echo "Runtime: " & Now
Set objFSO = Nothing
Set objShell = Nothing
WScript.Quit(0)
 
Function FTPList(strSite,strUsername,strPassword,strRemotePath,strRemoteFile)
	Const OpenAsDefault = -2
	Const FailIfNotExist = 0
	Const ForReading = 1
	Const ForWriting = 2
	Dim strFileReturn : strFileReturn = ""
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	Set objDict = CreateObject("Scripting.Dictionary")
	objDict.Comparemode = Textmode	
	strRemotePath = Trim(strRemotePath)
	If InStr(strRemotePath, " ") > 0 Then
		If Left(strRemotePath, 1) <> """" And Right(strRemotePath, 1) <> """" Then
		  strRemotePath = """" & strRemotePath & """"
		End If
	End If
	
	If Len(strRemotePath) = 0 Then
		strRemotePath = "\"
	End If
	If Len(strLocalPath) = 0 Then
		strLocalpath = objShell.CurrentDirectory
	End If
	
	If Not objFSO.FolderExists(strLocalPath) Then
		FTPDownAndRename = "Error: Local Folder Not Found."
		Exit Function
	End If
	
	sOriginalWorkingDirectory = objShell.CurrentDirectory
	objShell.CurrentDirectory = strLocalPath
	
	'build input file for ftp command
	strFTPScript = strFTPScript & "USER " & strUsername & vbCRLF
	strFTPScript = strFTPScript & strPassword & vbCRLF
	strFTPScript = strFTPScript & "cd " & strRemotePath & vbCRLF
	strFTPScript = strFTPScript & "binary" & vbCRLF
	strFTPScript = strFTPScript & "prompt n" & VbCrLf
	strFTPScript = strFTPScript & "ls " & VbCrLf
	strFTPScript = strFTPScript & "quit" & VbCrLf & "quit" & VbCrLf & "quit" & VbCrLf
	strFTPTemp = objShell.ExpandEnvironmentStrings("%TEMP%")
	strFTPTempFile = strFTPTemp & "\" & objFSO.GetTempName
	strFTPResults = strFTPTemp & "\" & objFSO.GetTempName
	
	'Write the input file for the ftp command to a temporary file.
	Set fFTPScript = objFSO.CreateTextFile(strFTPTempFile, True)
	fFTPScript.WriteLine(strFTPScript)
	fFTPScript.Close
	Set fFTPScript = Nothing 
	objShell.Run "%comspec% /c FTP -n -s:" & strFTPTempFile & " " & strSite & " > " & strFTPResults, 0, True
	Wscript.Sleep 1000
	
	'Check results of transfer.
	Set fFTPResults = objFSO.OpenTextFile(strFTPResults, ForReading, FailIfNotExist, OpenAsDefault)
	i=0
	While Not fFTPResults.AtEndOfStream
		i=i+1
		strLine = fFTPResults.ReadLine
		'WScript.Echo fFTPResults.ReadLine
		objDict.Add "Line"& i, strLine
	Wend
	colKeys = objDict.Keys
	colItems = objDict.Items
	j=0
	For Each strKey In colKeys
		j=j+1
		'WScript.Echo strKey & " >>" & objDict.Item(strKey)
		If objDict.Item(strKey) = "150 Here comes the directory listing." Then
			varFileBegin = j
		End If
		If objDict.Item(strKey) = "226 Directory send OK." Then
			varFileEnd = j
		End If	
	Next
	If varFileEnd = varFileBegin + 1 Then 
		varFiles = 0
	Else varFiles = (varFileEnd - (varFileBegin+1))
	End If
	WScript.Echo "Number of files found = " & varFiles
	varfilecount = 0
	If varFiles > 0 Then
		Do Until varfilecount = varFiles
			varfilecount = varfilecount + 1
			strFileMatch = objDict.Item("Line"& varFileBegin + varfilecount)
			Set RegularExpressionObject = New RegExp
			With RegularExpressionObject
			.Pattern = strRemoteFile
			.IgnoreCase = True
			.Global = False
			End With
			expressionmatch = RegularExpressionObject.Test(strFileMatch)
			If expressionmatch Then
				WScript.echo RegularExpressionObject.Pattern & " was found in " & strFileMatch
				strFileReturn = strFileMatch
			End If
			Set RegularExpressionObject = Nothing
		Loop
	End If	
		
	objFSO.DeleteFile(strFTPTempFile)
	fFTPResults.Close
 
	If strFileReturn <> "" Then
		FTPList = strFileReturn
	Else
		FTPList = "NoFTPFiles"
	End If
End Function
 
Function FTPDownAndRename(strSite,strUsername,strPassword,strRemotePath,strRemoteFile)
	Const OpenAsDefault = -2
	Const FailIfNotExist = 0
	Const ForReading = 1
	Const ForWriting = 2
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	strRemotePath = Trim(strRemotePath)
  	strLocalpath = Trim(strLocalPath)
 
	If InStr(strRemotePath, " ") > 0 Then
		If Left(strRemotePath, 1) <> """" And Right(strRemotePath, 1) <> """" Then
		  strRemotePath = """" & strRemotePath & """"
		End If
	End If
	
	If Len(strRemotePath) = 0 Then
		strRemotePath = "\"
	End If
	
	If Not objFSO.FolderExists(strLocalPath) Then
		FTPDownAndRename = "Error: Local Folder Not Found."
		Exit Function
	End If
	
	sOriginalWorkingDirectory = objShell.CurrentDirectory
	objShell.CurrentDirectory = strLocalPath
	
	'build input file for ftp command
	strFTPScript = strFTPScript & "USER " & strUsername & vbCRLF
	strFTPScript = strFTPScript & strPassword & VbCrLf
	strFTPScript = strFTPScript & "cd " & strRemotePath & vbCRLF
	strFTPScript = strFTPScript & "binary" & vbCRLF
	strFTPScript = strFTPScript & "prompt n" & VbCrLf
	strFTPScript = strFTPScript & "lcd " & objShell.ExpandEnvironmentStrings("%TEMP%") & VbCrLf
	strFTPScript = strFTPScript & "mget " & strRemoteFile & VbCrLf
	strFTPScript = strFTPScript & "rename " & strRemoteFile & " processed.csv"  & VbCrLf
	strFTPScript = strFTPScript & "quit" & VbCrLf & "quit" & VbCrLf & "quit" & VbCrLf
	strFTPTemp = objShell.ExpandEnvironmentStrings("%TEMP%")
	strFTPTempFile = strFTPTemp & "\" & objFSO.GetTempName
	strFTPResults = strFTPTemp & "\" & objFSO.GetTempName
	
	'Write the input file for the ftp command to a temporary file.
	Set fFTPScript = objFSO.CreateTextFile(strFTPTempFile, True)
	fFTPScript.WriteLine(strFTPScript)
	fFTPScript.Close
	Set fFTPScript = Nothing 
	objShell.Run "%comspec% /c FTP -n -s:" & strFTPTempFile & " " & strSite & " > " & strFTPResults, 0, True
	Wscript.Sleep 1000
	
	'Check results of transfer.
	Set fFTPResults = objFSO.OpenTextFile(strFTPResults, ForReading, FailIfNotExist, OpenAsDefault)
	strResults = fFTPResults.ReadAll
	fFTPResults.Close
	
	objFSO.DeleteFile(strFTPTempFile)
	objFSO.DeleteFile(strFTPResults)
	'WScript.Echo strResults
	
	If InStr(strResults, "250 Rename successful.") > 0 Then
		FTPDownAndRename = True
	ElseIf InStr(strResults, "File not found") > 0 Then
		FTPDownAndRename = "Error: File Not Found"
	ElseIf InStr(strResults, "cannot log in.") > 0 Then
		FTPDownAndRename = "Error: Login Failed."
	Else
		FTPDownAndRename = "Error: Unknown."
	End If
End Function

Open in new window

0
Comment
Question by:dspradling
  • 4
  • 4
8 Comments
 
LVL 10

Expert Comment

by:kyleb84
ID: 23717390
I'm thinking maybe an encoding / conversion issue...

Please place the following tests in your script at the appropriate place and let me know what the results are...



' Test 1: Known Literal
MsgBox objFSO.FileExists("C:\boot.ini")
 
' Test 2: Conversion?
MsgBox objFSO.FileExists(CStr(strFileToSend))
 
' Test 3: Literal
MsgBox objFSO.FileExists("C:\Users\marsha\AppData\Local\Temp\Consumption.CSV")

Open in new window

0
 

Author Comment

by:dspradling
ID: 23721192
I did as suggested and received theses results.

MsgBox objFSO.FileExists("C:\pagefile.sys") = True (no boot.ini)
MsgBox objFSO.FileExists(CStr(strFileToSend)) = False
MsgBox objFSO.FileExists("C:\Users\marsha\AppData\Local\Temp\Consumption.CSV") = True



0
 
LVL 10

Expert Comment

by:kyleb84
ID: 23725263
HMM WTF?

A couple more tests.

... and what if your try i different directory?

' strFileToSend = objShell.ExpandEnvironmentStrings("%TEMP%") & "\" & strRemoteFile
strFileToSend = "c:\" & strRemoteFile

Does that work?
' Assuming the file isn't zero bytes
Dim objFile
Set objFile = objFSO.GetFile(strFileToSend)
msgbox objFile.Size
 
' I wrote a quick one and tested:
 
Dim strFileToSend
Dim objFile
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strFileToSend = objShell.ExpandEnvironmentStrings("%TEMP%") & "\test.csv"
Set objFile = objFSO.CreateTextFile(strFileToSend, True)
objFile.WriteLine("This is a test.")
objFile.Close
msgbox strFileToSend
msgbox objFSO.FileExists(strFileToSend)
 
' Returns "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\test.csv" and True.
' I noticed by \Users your probably running Vista,
' so I'm upgrading to WSH 4.7
' I try run the same script again with Vista's version of the WSH.

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:kyleb84
ID: 23725290
Hmm WinXPx64 isn't supported... can't install the update
0
 

Author Comment

by:dspradling
ID: 23725766
I have tried downloading to a different folder (c:\HPS_temp) with the same results.  If I run a similar script with out the FTP commands to get and email the file it works.  This is really confusing me.  I have also tried sending the strFileToSend variable as an argument in a shell command to this other script and it fails.

I added these lines to call the SendMailFile.vbs:

      strCmdLine = "cscript.exe C:\Users\marsha\Documents\Scripts\SendMailFile.vbs " & strFileToSend
      WScript.Echo strCmdLine
      Dim objExec
      Set objExec = objShell.Exec(strCmdLine)
      Do While objExec.Status = 0
           WScript.Sleep 100
      Loop
      wscript.Echo "Sent email..."

When I go to Run and paste in 'cscript.exe C:\Users\marsha\Documents\Scripts\SendMailFile.vbs C:\HPS_Temp\Consumption.CSV' it works as expected but not when called from the FTP script.  See snippet for SendMailFile.vbs.

Since I am running on Vista I thought I would try it on XP and got the same results.  I am baffled.

I assume the comment aboce about WinXP64 was from another post?







Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
 
'Dim strFileToSend : strFileToSend = objShell.ExpandEnvironmentStrings("%TEMP%") & "\" & "Consumption.CSV"
Dim strFileToSend : strFileToSend = WScript.Arguments.Item(0)
 
'WScript.Sleep 10000
 
If objFSO.FileExists(strFileToSend) Then
	Call SendEmailAttach(strFileToSend)
Else
	strError = "The Zip file does not seem to exist at " & strFileToSend
	MsgBox strError
End If
 
WScript.Quit(0)
 
 
 
Sub SendEmailAttach(strFileToSend)
	Const OverwriteExisting = True
	Const cdoSendUsingPickup = 1 
	Const cdoSendUsingPort = 2 'Must use this to use Delivery Notification
	Const cdoAnonymous = 0
	Const cdoBasic = 1 ' clear text
	Const cdoNTLM = 2 'NTLM
	Const cdoDSNDefault = 0 'None
	Const cdoDSNNever = 1 'None
	Const cdoDSNFailure = 2 'Failure
	Const cdoDSNSuccess = 4 'Success
	Const cdoDSNDelay = 8 'Delay
	Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
	set objMsg = CreateObject("CDO.Message")
	Set objConf = CreateObject("CDO.Configuration")
	Set objFlds = objConf.Fields
	objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
	objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.watlow.com"
	objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
	objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "WATLOW\coladmin"
	objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "col+admin"
	objFlds.Update
	Set objMsg.Configuration = objConf
	objMsg.To = "Someone <someone@somewhere.com>"
  	'objMsg.To = "Someone <someone@somewhere.com>;" _
 	'& "Someoen2 <someone2@somewhere.com>"
	objMsg.From = "test@somewhere.com"
	objMsg.Subject = "Did it blend?"
	objMsg.TextBody = VbCrLf & "See attached file." _
	& VbCrLf & VbCrLf & "Blah blah blah."
	If objFSO.FileExists(strFileToSend) Then
		objMsg.Addattachment strFileToSend
	Else
		strError = "The Zip file does not seem to exist at " & strFileToSend
		MsgBox strError
	End If
	objMsg.DSNOptions = cdoDSNFailure
	objMsg.Fields.update
	objMsg.Send
End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:kyleb84
ID: 23726709
"I assume the comment aboce about WinXP64 was from another post?"

No I wrote down the bottom of the previous post (In the code section) that I'd try updating my WSH to the same version as Vista... But I run WinXPx64 and the update refused to install...

" If I run a similar script with out the FTP commands to get and email the file it works."

So we can safely say now that the FTP commands are returning a corrupt string - corrupt in a way that isn't openly visible.

What is the FTP program?
- If it's just a simple vbs activex add-on then consider trying a different one?
- Or is it part of a larger application?

0
 

Author Comment

by:dspradling
ID: 23728339
The functions generate and build temporary files then call a shell command to run the standard MS FTP client.  I agree I think something funky is happening from the FTP server but I have no idea what.  Filezilla has had no problem copying and deleting test files from the server.  

	strFTPScript = strFTPScript & "USER " & strUsername & vbCRLF
	strFTPScript = strFTPScript & strPassword & VbCrLf
	strFTPScript = strFTPScript & "cd " & strRemotePath & vbCRLF
	strFTPScript = strFTPScript & "binary" & vbCRLF
	strFTPScript = strFTPScript & "prompt n" & VbCrLf
	strFTPScript = strFTPScript & "lcd " & objShell.ExpandEnvironmentStrings("%TEMP%") & VbCrLf
	strFTPScript = strFTPScript & "mget " & strRemoteFile & VbCrLf
	strFTPScript = strFTPScript & "rename " & strRemoteFile & " processed.csv"  & VbCrLf
	strFTPScript = strFTPScript & "quit" & VbCrLf & "quit" & VbCrLf & "quit" & VbCrLf
	strFTPTemp = objShell.ExpandEnvironmentStrings("%TEMP%")
	strFTPTempFile = strFTPTemp & "\" & objFSO.GetTempName
	strFTPResults = strFTPTemp & "\" & objFSO.GetTempName
	
	'Write the input file for the ftp command to a temporary file.
	Set fFTPScript = objFSO.CreateTextFile(strFTPTempFile, True)
	fFTPScript.WriteLine(strFTPScript)
	fFTPScript.Close
	Set fFTPScript = Nothing 
	objShell.Run "%comspec% /c FTP -n -s:" & strFTPTempFile & " " & strSite & " > " & strFTPResults, 0, True

Open in new window

0
 

Accepted Solution

by:
dspradling earned 0 total points
ID: 23739302
I gave up and used the COM transfer engine in cuteftppro.  Forgot I purchased a copy several years ago.  I think it was a problem with the encoding but I don't have time to get into the messy details.  
0

Featured Post

Industry Leaders: 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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Loops Section Overview

850 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