Solved

Copy file from one server to another via asp vbs

Posted on 2009-07-14
8
515 Views
Last Modified: 2012-05-07
Hello, I have a form that uses fso to pull text file names into a form on an ASP page.  I'd like the user to click an export button to send the chosen file over to another server.  I have an export.vbs script but I don't now how to pass the value from the asp page to the script.  Is there a way to do that?  Script below.  Hardcoded the script runs great.  I don't know how to get the value from the form into the script.  This is on an inhouse server.

strFilePath = "\\server1\jobs\source.txt" I'd like source.txt to be the value of my form.

Is there a way to do this?
The script runs from the asp page like this: 

<%if request.form("jobno")>"" then%>

<script>  

window.open("export.vbs")

</script>

<%end if%>
 

export.vbs below
 

Option Explicit
 

Dim objFSO, objFileCopy, objGuyFile, objFileDelete

Dim strFilePath, strDestination, WinHttpReq

Dim strFileText, strFileText2, strFileText3

dim fso
 

strFilePath = "\\server1\jobs\source.txt"

strDestination ="\\server2\export\destination"
 
 

Set fso = CreateObject("Scripting.FileSystemObject")
 

If fso.FileExists(strDestination) then

' Delete file if it alread exists in the destination.

fso.DeleteFile strDestination,True

wscript.echo "File deleted from Server 2"

else

wscript.echo "File wasn't deleted."

end if
 
 

Set objFSO = CreateObject("Scripting.FileSystemObject")
 

if objFSO.fileexists(strfilePath) then

Set objFileCopy = objFSO.GetFile(strFilePath)

' copy the file to destination
 

objFileCopy.copy (strDestination)
 

WSCript.Echo "The new file has been Copied to " & strDestination

Wscript.Quit

else

wscript.echo "This didn't work. New file WAS NOT copied."

Wscript.Quit

end if

' End of Script

Open in new window

0
Comment
Question by:meldraper
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:deathtospam
ID: 24859630
I think you can do it all on one page.  I haven't tested this (very busy ATM), but give this a shot:
<%	

	Option Explicit
 

	Dim objFSO, objFileCopy, objGuyFile, objFileDelete

	Dim WinHttpReq, strFileText, strFileText2, strFileText3

	Dim strFilePath
 

	const strDestination = "\\server2\export\destination"
 

	

	if request.form("jobno")>"" then

		'// Get the path of the source file.

		strFilePath = Request("THE_FORM_FIELD_NAME_THAT_YOU_WANT_TO_USE")
 

		Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

		

		If objFSO.FileExists(strDestination) then

			' Delete file if it alread exists in the destination.

			Call objFSO.DeleteFile(strDestination, True)

			Call Response.Write("File deleted from Server 2.<br>" & vbNewLine)

		Else

			Call Response.Write("File wasn't deleted.<br>" & vbNewLine)

		End If

 

 

		if objFSO.fileexists(strfilePath) then

			' copy the file to destination

			Set objFileCopy = objFSO.GetFile(strFilePath)

			Call objFileCopy.Copy(strDestination)

			Set objFileCopy = Nothing

 

			Call Response.Write("The new file has been Copied to " & strDestination & ".<br>" & vbNewLine)

		else

			Call Response.Write("This didn't work. New file WAS NOT copied".<br>" & vbNewLine)

		end if
 

		Set objFSO = Nothing

	end if

%>

Open in new window

0
 

Author Comment

by:meldraper
ID: 24865492
No luck.  No error though, but the file did not get copied.  I don't really understand why either.  I made sure to response.write my paths to make sure they were correct and they are.  Thanks a million for this help, though.  On paper and in theory, it should work perfect.  I was pretty excited about it.
0
 
LVL 9

Accepted Solution

by:
deathtospam earned 500 total points
ID: 24871950
Ok, I did a proper test.  Copy this new script to a new ASP page and run it.  I fudged the "jobno" form field -- all I want to do is make sure file copying works.  I tried it out and worked for me -- let me know if any other problems occur.
<%

	Option Explicit
 

	Dim strFilePath, objFSO, objSourceFile, sFileName, sTargetPath
 

	const C_S_DESTINATION_FOLDER = "\\server2\export\destination"
 

	

	if request.form("jobno")>"" then

		'// Get the path of the source file.

		strFilePath = Trim(Request("THE_FORM_FIELD_NAME_THAT_YOU_WANT_TO_USE") & "")
 

		Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
 

		If NOT objFSO.FolderExists(C_S_DESTINATION_FOLDER) Then

			Call Response.Write("ERROR: The target folder <u>" & Server.HTMLEncode(C_S_DESTINATION_FOLDER) & "</u> was not found.<br>" & vbNewLine)

		ElseIf NOT objFSO.FileExists(strFilePath) Then

			Call Response.Write("ERROR: The source file <u>" & Server.HTMLEncode(strFilePath) & "</u> was not found.<br>" & vbNewLine)

		Else

			Set objSourceFile = objFSO.GetFile(strFilePath)
 

			'// Get the name of the file you're copying over.

			sFileName = objFSO.GetFile(strFilePath).Name
 

			'// Create the fully-qualified path to the new file's location (if copying were to succeed).

			sTargetPath = C_S_DESTINATION_FOLDER & "\" & sFileName
 

			If objFSO.FileExists(sTargetPath) then

				'// Delete the target file, if it alread exists in the destination.

				Call objFSO.DeleteFile(sTargetPath, True)

				Call Response.Write("Target file already existed on Server 2 and was deleted.<br>" & vbNewLine)

			Else

				Call Response.Write("Target file does not yet exist on Server 2.<br>" & vbNewLine)

			End If

			

			'// Copy the file to the destination

			Call objSourceFile.Copy(sTargetPath)

			

			If objFSO.FileExists(sTargetPath) then

				'// Confirm that the file was copied properly.

				Call Response.Write("The new file has been Copied to " & sTargetPath & ".<br>" & vbNewLine)

			else

				Call Response.Write("The file copy didn't work, for some reason.<br>" & vbNewLine)

			end if

		End If
 

		Set objFSO = Nothing

	end if

%>

<form action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="post">

<input type="hidden" name="jobno" value="ASD">

<input type="input" name="THE_FORM_FIELD_NAME_THAT_YOU_WANT_TO_USE" value="">

<input type="submit">

</form>

Open in new window

0
 

Author Comment

by:meldraper
ID: 24872155
Hi Death, thanks again but no luck.  The error message says the target destination can't be found.  Just for fun I ran the vbs script by itself and it works fine.  Is there not a way to pass a value to that script and run it?  Thanks again.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:deathtospam
ID: 24872979
I don't understand: you said you got an error message, but that it ran fine.  What worked, what didn't, and what was the exact error message you got?
0
 

Author Comment

by:meldraper
ID: 24873083
No, my separate vbs script I have in my original post runs fine.  It runs fine separately, outside of the asp page.  What won't run is if that code is in the asp.
0
 

Author Closing Comment

by:meldraper
ID: 31603571
Hey DTS, I'm going to do everything I can to make this form work.  The destination server is a linux server and very sensitive.  If you have any other ideas to get around the target destination error or why a standalone export.vbs script would run when called from the ASP but not inside of it, let me know.  Many many thanks for your help.
0
 
LVL 9

Expert Comment

by:deathtospam
ID: 24883850
It could be a permissions issue, but then I'd expect to see some kind of timeout error message.  But that's really as much as I can help out with, without being there in person.  Sorry, and good luck!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now