Change drive on remote machine after FTPing to it

Status
Not open for further replies.

Hviezdoslav

Beta member
Messages
4
I am sorry if I am in the wrong forum but I am not sure into which forum my issue/problem falls.

Please bear with me as I am not an experienced Programmer and have no experience FTPing.

Does the cd command only change directory on the remote machine within a specific drive or can the cd command change the drive on the remote machine?

If my understanding is correct, the lcd command is for changing the on the local machine.

There is an existing web app here and the man who wrote it left work before I started working here.

The web app uses VB in the code-behind pages and has aspx page.

The app resides on a server called KCM_Int. Also on this server there is a .ftp file, a response.tmp file, a ftp.exe, and a cmd.exe.

The app allows users to upload files. The code saves the file to a particular drive on the KCM_Int server, the same server on which the app resides.

The the VB code in the code-behind tries to save the file to a remote server named KCM_Ext. The code was supposed to save to E:\site\files and then depending on the type of file to further sub folders in that path. Well, the files are not going to that E drive path yet there was no error.

I searched the KCM_Ext machine's C and E drives and found that the files were being uploaded to KCM_Ext to C:\Windows\System32\LogFiles\W3SVC1649540525.

So the file is uploaded successfully to KCM_Int, the machine on which the app resides. The file is uploaded by the code-behind to KCM_Ext but for whatever reason the file goes to that C drive path. I need to move the file from that C path on KCM_Ext to the appropriate E drive path or upload the file to the E drive path after it is uploaded to the C drive path on that remote machine.

Here is some of the existing code that is uploading to the KCM_Int server on which the app resides and then to the C drive of the KCM_Ext server:

Imports System.IO
Partial Class Upload
Inherits System.Web.UI.Page
Dim currentDir As String
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
Dim thisPage As String
Dim root As String
Dim FTPDir As String
Dim AppPanel As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sRef As String
Master.TitleText = Page.Title
thisPage = Request.Path
root = Request.ServerVariables("APPL_PHYSICAL_PATH") & "files"
End Sub

Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadButton.Click
'Clear message from last page load
message.text = ""
'if we have a file then upload
Dim File1 As HtmlInputFile
Dim DirDDL As DropDownList
DirDDL = CType(Page.Form.FindControl("ContentPlaceHolder1").FindControl("dirDDL"), DropDownList)
File1 = CType(Page.Form.FindControl("ContentPlaceHolder1").FindControl("File1"), HtmlInputFile)

currentDir = root & "\" & DirDDL.selectedvalue
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.StartsWith(root) Then
currentDir = root
End If

If Len(File1.Value) > 0 Then
'get the filename and catch spaces
Dim sFile As String = File1.PostedFile.FileName
'get the destination
Dim sFolderRoot As String = Server.MapPath("/") & "files\"
sFolderRoot &= DirDDL.SelectedValue
Dim arFileName As Array
arFileName = Split(sFile, "\")
sFile = arFileName(UBound(arFileName))
If Instr(sFile, " ") > 0 Then
lblError.Text = "<font size=+3><center>No spaces allowed in file names! </center></font>"
Exit Sub
End If
'upload the file
DoUpload()
arFileName = Nothing
Else
Response.Write("<h1>blank file</h1>")
End If
End Sub

Sub DoRedirect(ByVal pFileName)
'Response.Write("<h5 color='red'><a href='" & thisPage & "?dir=" & currentDir & "'>move along...</a></h5>")
Message.text = pFileName & " has been successfully uploaded"
End Sub

#Region "LOCAL FILES"
Sub DoUpload()
Dim filename As String = ""
If Not (File1.PostedFile Is Nothing) Then
Try
Dim postedFile = File1.PostedFile
filename = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & "\" & filename)
Catch ex As Exception
'Response.Write("<br>" & ex.Message & "<br>")
message.Text = "Failed uploading file"
End Try
End If

If message.Text.Length = 0 Then
Response.Write(message.text)
'///////////////
CopyFileToSite(filename, "KCM_Ext")
'///////////////
DoRedirect(fileName)
End If
End Sub

#End Region

#Region "REMOTE FUNCTIONS"
Sub CopyFileToSite(ByVal TheFile As String, ByVal TheSite As String)
'***********************
Dim FtpDir As String = Request.ServerVariables("APPL_PHYSICAL_PATH") & "ftp\"
Dim arDirList As Array = Split(Replace(currentDir, root, ""), "\")
'---- write FTP command text to text file -----
Dim objWriter As TextWriter = New StreamWriter(FtpDir & "wwwupload.ftp")
objWriter.WriteLine("This is coming from www")
objWriter.WriteLine("lcd " & currentDir)
objWriter.WriteLine("open " & TheSite)
objWriter.WriteLine(ConfigurationManager.AppSettings.Item("FTP_user"))

objWriter.WriteLine(ConfigurationManager.AppSettings.Item("FTP_pass"))

'Check to see if we need to issue a 'cd' command
If UBound(arDirList) > 0 Then
Dim i As Integer = 1
While i <= UBound(arDirList)
objWriter.WriteLine("cd " & arDirList(i))
i = i + 1
End While
End If
'finish the command
objWriter.WriteLine("prompt")
objWriter.WriteLine("put " & TheFile)
objWriter.WriteLine("bye")
'cleanup
objWriter.Flush()
objWriter.Close()
objWriter = Nothing
'build command string
Dim strCMD As String = FtpDir & "ftp.exe -s:" & FtpDir & "wwwupload.ftp"
Dim strTempFile As String = FtpDir & "response2.tmp"
'Use cmd.exe to run ftp.exe, parsing our newly created command string/file
strCMD = FtpDir & "cmd.exe /c " & strCMD & " > " & strTempFile
Dim oScript As Object = CreateObject("Wscript.Shell")
Call oScript.Run(strCMD, 0, True)
oScript = Nothing
' *************** BEGIN DEBUG ***************
'With Response
'.Write("root " & root & "<br>")
'.Write("FtpDir " & FtpDir & "<br>")
'.Write("currentDir: " & currentDir & "<br>")
'.Write("TheFile: " & TheFile & "<br>")
'.Write("TheDir: " & Replace(currentDir, root, "") & "<br>")
'.Write("arDirList Count: " & UBound(arDirList) & "<br>")
'.Write("dirDDL: " & DirDDL.SelectedValue & "<br>")
'If UBound(arDirList) > 0 Then
'Dim i As Integer = 1
'While i <= UBound(arDirList)
'.Write("arDirList(" & i & "): " & arDirList(i) & "<br>")
'i = i + 1
'End While
'End If
'.Write("cmd: " & strCMD & "<br>")
'End With
' *************** END DEBUG ***************
End Sub

#End Region
End Class


So between the existing objWriter.WriteLine("put " & TheFile) line of code that uploads to C drive's path and the existing objWriter.WriteLine("bye") line of code, I thought that I would try the following:

objWriter.WriteLine("prompt")
objWriter.WriteLine("cd " & "E:\site\files\files")
objWriter.WriteLine("prompt")
objWriter.WriteLine("put " & TheFile)

But I get in the response2.tmp file the following error:

The filename, directory name, or volume label syntax is incorrect.

Here is everything from the response2.tmp:

ftp> Invalid command.

ftp> This is coming from www
Local directory now D:\WebsiteAdmin\files\files.

ftp> lcd D:\WebsiteAdmin\files\files
Connected to KCM_Ext.Our-Firm.com.

open KCM_Ext
220 Microsoft FTP Service
User (KCM_Ext.OurFirmDOTcom:(none)):
331 Password required for ftp_user.

230 User ftp_user logged in.
ftp> cd files
550 files: The system cannot find the file specified.

ftp> Interactive mode Off .

ftp> prompt
put justTesting_files.pdf
200 PORT command successful.
150 Opening ASCII mode data connection for justTesting_files.pdf.
226 Transfer complete.
ftp: 1295724 bytes sent in 0.03Seconds 41797.55Kbytes/sec.

ftp> Interactive mode On .

ftp> prompt
cd E:\site\files\files
550 E:\site\files\files: The filename, directory name, or volume label syntax is incorrect.

ftp> Interactive mode Off .

ftp> prompt
put justTesting_files.pdf
200 PORT command successful.
150 Opening ASCII mode data connection for justTesting_files.pdf.
226 Transfer complete.
ftp: 1295724 bytes sent in 0.03Seconds 41797.55Kbytes/sec.

ftp> bye
221


I do not know why the uploading goes to KCM_Ext's C:Windows\System32\LogFiles\W3SVC1649540525, but it does. After that, can I change to E drive path on KCM_Ext and then upload to it? Is it not possible to change drives on the remote machine after FTPing to it and then put/upload the file?

I have spent much time trying another approach. I put onto KCM_Ext in C:\Windows\System32\LogFiles\W3SVC1649540525 a files.bat and cmd.exe. In the files.bat I have the following:

@ECHO OFF
copy %1 %2 /v /y
CLS
EXIT

In the code-behind page on KCM_Int, after the existing line of code Call oScript.Run(strCMD, 0, True), I put the line of code:
Call moveFileOnKCM_Ext (TheFile, TheSite).

Then in the code-behind I added the following:

Sub moveFileOnKCM_Ext (ByVal TheFile As String, ByVal TheSite As String)

Dim fileDir As String = Replace(currentDir, root, "")
Dim pathToWhichToCopyOnKCM_Ext As String = ""
If InStr(fileDir, "file") Then
pathToWhichToCopyOnKCM_Ext = "E:\site\files\files"
End If
If InStr(fileDir, "e-Alerts") Then
pathToWhichToCopyOnKCM_Ext = "E:\site\files\e-Alerts"
End If
If InStr(fileDir, "graphics") Then
pathToWhichToCopyOnKCM_Ext = "E:\site\files\graphics"
End If
If InStr(fileDir, "vCards") Then
pathToWhichToCopyOnKCM_Ext = "E:\site\files\attorneys\vCards"
End If
If InStr(fileDir, "picture") Then
pathToWhichToCopyOnKCM_Ext = "E:\site\files\attorneys\picture"
End If
Dim pathFromWhichToDeleteOnKCM_Ext As String = "C:\WINDOWS\system32\LogFiles\W3SVC1649540525"
Dim pathOfBatchFile As String = "C:\WINDOWS\system32\LogFiles\W3SVC1649540525"

Dim myRoot As String = ""
myRoot = Request.ServerVariables("APPL_PHYSICAL_PATH") & "files"
Dim myCurrentDir As String = ""
myCurrentDir = myRoot & "\" & DirDDL.selectedvalue
If myCurrentDir Is Nothing Then
myCurrentDir = myRoot
End If
If Not myCurrentDir.StartsWith(myRoot) Then
myCurrentDir = myRoot
End If

Dim FtpDirectory As String = Request.ServerVariables("APPL_PHYSICAL_PATH") & "ftp\"
Dim my_arDirList As Array = Split(Replace(myCurrentDir, myRoot, ""), "\")

Dim myObjWriter As StreamWriter = New StreamWriter(FtpDirectory & "wwwupload3.ftp")
myObjWriter.WriteLine("open " & TheSite)
myObjWriter.WriteLine(ConfigurationManager.AppSettings.Item("FTP_user"))
myObjWriter.WriteLine(ConfigurationManager.AppSettings.Item("FTP_pass"))
'myObjWriter.Write("The date is: ") 'This was just for testing purposes.
'myObjWriter.WriteLine(DateTime.Now) 'This was just for testing purposes.

'This shows in KCM_Int's response3.tmp the files
'within C:\Windows\System32\LogFiles\W3SVC1649540525 on KCM_Ext:
'myObjWriter.WriteLine("dir")

'This successfully renames the file in the C drive path of KCM_Ext:
'myObjWriter.WriteLine("rename " & TheFile & " " & "justTesting_files_NewName.pdf")

'This did not work:
myObjWriter.WriteLine("rename " & TheFile & " " & pathToWhichToCopyOnKCM_Ext & "\" & TheFile)

'It seems that I cannot rename the file AND move it to a different drive on remote machine.



'I wanted to pass parameters to the files.bat file.
'I tried many things at this point, and the attempts produced
'no errors but did not move the file from the C drive's path on KCM_Ext
'to the E drive's path on KCM_Ext remote server:
myObjWriter.WriteLine("files.bat" & " " & TheFile & " " & pathToWhichToCopyOnKCM_Ext)

myObjWriter.WriteLine("cmd.exe /c files.bat" & " " & TheFile & " " & pathToWhichToCopyOnKCM_Ext)

myObjWriter.WriteLine("cmd.exe -s:files.bat " & TheFile.ToString() & " " & pathToWhichToCopyOnKCM_Ext.ToString())

myObjWriter.WriteLine("cmd.exe /k files.bat " & TheFile.ToString() & " " & pathToWhichToCopyOnKCM_Ext.ToString())

myObjWriter.WriteLine("CMD CALL files.bat justTesting_files.pdf E:\site\files\files")

myObjWriter.WriteLine("START cmd.exe CALL files.bat justTesting_files.pdf E:\site\files\files") '

'But none of my attempts worked. I can of course FTP successfully to KCM_Ext, but I cannot figure out how to pass parameters from the code-behind to run the batch file OR somehow pass parameters from the code-behind to run KCM_Ext's cmd.exe to run then the batch file after cmd.exe passes to it the parameters.




myObjWriter.WriteLine("bye")
myObjWriter.Flush()
myObjWriter.Close()
myObjWriter = Nothing
Dim myStrCMD As String = FtpDirectory & "ftp.exe -s:" & FtpDirectory & "wwwupload3.ftp"
Dim myStrTempFile As String = FtpDirectory & "response3.tmp"
myStrCMD = FtpDirectory & "cmd.exe /c " & myStrCMD & " > " & myStrTempFile
Dim myScript As Object = CreateObject("Wscript.Shell")
Call myScript.Run(myStrCMD, 0, True)
myScript = Nothing


So anyway, if anybody has any ideas or suggestions, I would be very grateful if he or she would share them with me.

I just need from KCM_Int's code-behind to upload the file to the E drive's path of KCM_Ext OR to FTP to KCM_Ext and then copy the file from the C drive's path to the E drive's path on KCM_Ext.

I have spent days researching this on the Internet. It seems that FTP was meant for only file transfers and does not have a command to "run" or "start" a batch file or cmd.exe. Also, it seems that there is no way after FTPing to a machine to use some FTP command to change the DRIVE on the remote machine and not just change the directory on the current drive. Maybe I am not understanding correctly though.

Again, I do not know why the file is uploaded to KCM_Ext to the C:\Windows\System32\LogFiles\W3SVC1649540525. I am told here at work that I cannot change the set up of the drives on KCM_Ext.

I know that I am FTPing successfully because I have added code out of curiosity to rename the file in the C:\Windows\System32\LogFiles\W3SVC1649540525 and my code worked. But I need to copy OR move the file from that C drive path to the appropriate path on the E drive of that remote machine.

Thanks very much in advance for your consideration of my question/thread. I apologize if I have provided TOO MUCH info. I do not want to provide too little info either though.
 
Greetings kmote,

Thanks for the reply.

Currently the web app (VB in code-behind) is uploading successfully a file to the remotel server's C:Windows\System32\LogFiles\W3SVC1649540525. I do not know why the file goes to that path on the remote server. The existing code just does a PUT and the file is uploaded for whatever reason to that C drive path.

I need to upload to an E drive path on that remote machine. When I use the CD command after FTPing to that remote machine, I can change only the directory within that C drive and it seems that I cannot use the CD command to change drives, to change to upload to an E drive path. So I can FTP to the remote machine and use the CD command to change the directory within that C drive, but I need to change to the E drive after FTPing to that remote server so that I can PUT to upload a file to a particular path on the E drive. In the code-behind, I can figure out to which path on the E drive the file should be uploaded.
 
Status
Not open for further replies.
Back
Top Bottom