Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB scripting help from win9X/2K to VAX via FTP

Status
Not open for further replies.

DougPrice

Technical User
Nov 11, 2003
1
US
Hello all,

We here have a delemma hopefully some of you all can help with.

On a daily basis from our win9X/2K workstations, we have the need to access our VAX server utilizing a VB Script to logon on to the server, authenticate, change working directory, access the newest file and download it to the local machine and delete the file from the VAX, then log off.

ANY and ALL help at this point would be GREATLY appreciated as we have a temp fix, but by NO means a stable working situation here. Here is the script we use with what we think are the problem areas affected in the beginning of it separate from the whole script after it. Please feel free to copy any help or questions to my email here as well at dprice69@visteon.com for faster access to the fact help or questions are waiting.

Thank you all in advance

*********************************************************










******************************************************************************************
(suspected problem area #1)

Function OpenSession()

'Open the FTP Session
set FTPApplication = CreateObject("FTPSFtp.Application")
FTPApplication.TransferType = "ASCII"
WScript.Echo "Transfer Type Set To: " & FTPApplication.TransferType
WScript.Echo "DefaultSessionFolder = " & FTPApplication.DefaultSessionFolder
Set FTPSession = FTPApplication.OpenSession(sSessionPath)
******************************************************************************************
(suspected problem area #2)

Function OpenSession()

'Open the FTP Session
set FTPApplication = CreateObject("FTPSFtp.Application")
FTPApplication.TransferType = "ASCII"
WScript.Echo "Transfer Type Set To: " & FTPApplication.TransferType
WScript.Echo "DefaultSessionFolder = " & FTPApplication.DefaultSessionFolder
Set FTPSession = FTPApplication.OpenSession(sSessionPath)

'Check the session to see if created
WScript.Echo "If the session name appears next, the session created correctly:"
WScript.Echo FTPSession.SessionName
WScript.Echo "Session Created!"
OpenSession = True

'Change to the default directory
WScript.Echo "Setting default directory to: " & sHostDirectory
FTPSession.DefaultDirectory = sHostDirectory
WScript.Echo "Directory changed to: " & FTPSession.DefaultDirectory
******************************************************************************************
(Temporary fix to make work and be functional)

Function GetFCISFileName(sFileName)

Dim sTemp

'Get the starting file name
'sTemp = "FCIS_BDF_EX_" & sFCISDevice & "."
sTemp = sHostDirectory & "FCIS_BDF_EX_" & sFCISDevice & "."

'Add the year
******************************************************************************************



Program script in total (above errors highlighted in script below)


Dim FTPSession
Dim FTPApplication
Dim sSessionPath
Dim sHostDirectory
Dim sFCISDevice
Dim sInputDirectory

Call Main()
WScript.Quit

Sub Main()

Dim oInputFolder
Dim oDataFolders
Dim oDataFolder
Dim oDataFiles
Dim oDataFile
Dim nResult

'See if arguments were passed
if not GetArguments then
Wscript.Echo "Command Line Syntax: filaname.vbs SessionName&Path HostDirectory FCISDeviceName LocalInputDirectory"
exit sub
Else
Wscript.echo "Session Path: " & sSessionPath
Wscript.echo "Host Directory: " & sHostDirectory
WScript.echo "FCIS Device: " & sFCISDevice
WScript.echo "Local Input: " & sInputDirectory
end if

'Open the session
if Not OpenSession then
WScript.Echo "Cannot open FTP Session: " & sSessionPath
WScript.Quit
End If

'Get a folder reference for the desired input directory
if not GetFolderReference(oInputFolder) then
WScript.Echo "Could not obtain input folder reference: " & sInputFolder
WScript.Quit
End If

'Get a Reference to the Data Folders within the Input Folder
set oDataFolders = oInputFolder.SubFolders

'Loop through each folder - uploading all files within that folder
For Each oDataFolder in oDataFolders

Set oDataFiles = oDataFolder.Files
WScript.Echo "Uploading File(s) In: " & oDataFolder.Path & ", " & oDataFiles.Count & " file(s)."
For Each oDataFile in oDataFiles

'Check the file size
if oDataFile.Size <> 0 then

'Check the file name
if CheckFileName(oDataFile.Name) Then

'Upload the file
WScript.Echo &quot;Uploading &quot; & oDataFile.Name & &quot; as &quot; & GetFCISFileName(oDataFile.Name)
nResult = FTPSession.SendFile(oDataFolder.Path & &quot;\&quot; & oDataFile.Name,GetFCISFileName(oDataFile.Name))
if nResult <> 0 and nResult <> -1 then
WScript.Echo &quot;ERROR Error while uploading file.&quot;
WScript.Quit
End If

'Delete the file
oDataFile.Delete True

End IF
Else
WScript.Echo &quot;Zero Length File: &quot; & oDataFile.Name
oDataFile.Delete True
End If
Next

'Delete the data folder if no files exist
if oDataFiles.Count = 0 then
oDataFolder.Delete True
End If

Next

End Sub

function GetArguments

Dim WshShell
Dim oArgs

'Get a reference to the script
Set WshShell = WScript.CreateObject( &quot;WScript.Shell&quot;)

'See if arguments were passed
Set oArgs = WScript.Arguments
If (oArgs.Count = 0) then
GetArguments = False
exit function
Else
If (oArgs.Count = 4) then
sSessionPath = oArgs(0)
sHostDirectory = oArgs(1)
sFCISDevice = oArgs(2)
sInputDirectory = oArgs(3)
GetArguments = True
exit function
End If
End If
end function

Function OpenSession()

'Open the FTP Session
set FTPApplication = CreateObject(&quot;FTPSFtp.Application&quot;)
FTPApplication.TransferType = &quot;ASCII&quot;
WScript.Echo &quot;Transfer Type Set To: &quot; & FTPApplication.TransferType
WScript.Echo &quot;DefaultSessionFolder = &quot; & FTPApplication.DefaultSessionFolder
Set FTPSession = FTPApplication.OpenSession(sSessionPath)

'Check the session to see if created
WScript.Echo &quot;If the session name appears next, the session created correctly:&quot;
WScript.Echo FTPSession.SessionName
WScript.Echo &quot;Session Created!&quot;
OpenSession = True

'Change to the default directory
WScript.Echo &quot;Setting default directory to: &quot; & sHostDirectory
FTPSession.DefaultDirectory = sHostDirectory
WScript.Echo &quot;Directory changed to: &quot; & FTPSession.DefaultDirectory

End Function

Function CheckFileName(sFileName)

Dim I

'First Character Needs to be a number
if not (Asc(left(sFileName,1)) >= 48 and Asc(left(sFileName,1)) <=57) then
CheckFileName = False
Exit Function
end if

'Second Character Needs to be 1-9 or A-C
if not (Asc(Mid(sFileName,2,1)) >= 49 and Asc(Mid(sFileName,1,1)) <=57) and not (Asc(Mid(sFileName,1,1)) >= 65 and Asc(Mid(sFileName,1,1)) <=67) then
CheckFileName = False
Exit Function
end if

'Third Character Needs to be between 0 and 3
if not (Asc(Mid(sFileName,3,1)) >= 48 and Asc(Mid(sFileName,3,1)) <=51) then
CheckFileName = False
Exit Function
end if

'4th through 11th character must be a number (Skip 9)
For I = 4 to 9
if I <> 9 then
if not (Asc(Mid(sFileName,I,1)) >= 48 and Asc(Mid(sFileName,I,1)) <=57) then
CheckFileName = False
Exit Function
end if
end if
next

'12th Character must be a D
if Right(sFileName,1) <> &quot;D&quot; then
CheckFileName = False
Exit Function
end if

CheckFileName = True
Exit Function

End Function

Function GetFolderReference(oFolder)

Dim oFileSystem

Set oFileSystem = CreateObject(&quot;Scripting.FileSystemObject&quot;)

if oFileSystem.FolderExists(sInputDirectory) then
Set oFolder = oFileSystem.GetFolder(sInputDirectory)
GetFolderReference = True
Else
GetFolderReference = False
End If

End Function

Function GetFCISFileName(sFileName)

Dim sTemp

'Get the starting file name
'sTemp = &quot;FCIS_BDF_EX_&quot; & sFCISDevice & &quot;.&quot;
sTemp = sHostDirectory & &quot;FCIS_BDF_EX_&quot; & sFCISDevice & &quot;.&quot;

'Add the year
if left(sFileName,1) = &quot;9&quot; then
sTemp = sTemp & &quot;1999&quot;
Else
sTemp = sTemp & &quot;200&quot; & left(sFileName,1)
End If

'Add the month
if mid(sFileName,2,1) >= &quot;1&quot; and mid(sFileName,2,1) <= &quot;9&quot; Then
sTemp = sTemp & &quot;0&quot; & mid(sFileName,2,1)
ElseIf mid(sFileName,2,1) = &quot;A&quot; Then
sTemp = sTemp & &quot;10&quot;
ElseIf mid(sFileName,2,1) = &quot;B&quot; Then
sTemp = sTemp & &quot;11&quot;
ElseIf mid(sFileName,2,1) = &quot;C&quot; Then
sTemp = sTemp & &quot;12&quot;
End IF

'Add the rest
sTemp = sTemp & mid(sFileName,3,6)
sTemp = sTemp & mid(sFileName,10,2)

GetFCISFileName = sTemp

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top