# copy a file from a url into a folder from a web script (such as an asp file)

**URL:** <https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851>\
**Category:** Factual Questions\
**Created:** [October 24, 2007, 7:10pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851 "2007-10-24T19:10:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 24, 2007, 7:10pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/1 "2007-10-24T19:10:49Z")

</div>

Hi.

Until recently I had an asp script which was copying a file from one network location to another.

Now My network admin is tightening up the security and now the script won’t work…

But I can get access to the file from an (internal) url.  
**Old**

\*source = “\fileserver1\file.txt”

Dim objFSO  
Set objFSO = Server.CreateObject(“Scripting.FileSystemObject”)

objFSO.CopyFile source, “\fileserver2\file.txt”  
\*

**New**  
\*  
source = “[http://internalserver.com/file.txt](http://internalserver.com/file.txt)”

Dim objFSO  
Set objFSO = Server.CreateObject(“Scripting.FileSystemObject”)

objFSO.CopyFile source, “\fileserver2\file.txt”  
\*

Obviously the above won’t work… so how can I achieve this? copying a file from a url to a folder.

---

<div class="post-metadata">

**Author:** ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)\
**Post date:** [October 24, 2007, 7:23pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/2 "2007-10-24T19:23:16Z")

</div>

I think you want to copy the contents of file.txt into a new file on the local server, and then copy that copy over to the network server.

Of course, you will need create, write and modify perms on the folder in which you are making the new file.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 24, 2007, 7:40pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/3 "2007-10-24T19:40:32Z")

</div>

[QUOTE=ZipperJJ]  
I think you want to copy the contents of file.txt into a new file on the local server, and then copy that copy over to the network server.

Of course, you will need create, write and modify perms on the folder in which you are making the new file.  
[/QUOTE]

Hi **ZipperJJ**!  
Yes I want to copy the contens of the source file into a new file… but the new file is going to be in the same folder as the asp file (a folder within inetpub/www)

---

<div class="post-metadata">

**Author:** ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)\
**Post date:** [October 24, 2007, 7:57pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/4 "2007-10-24T19:57:02Z")

</div>

[QUOTE=Lobsang]

**Old**

\*source = “\fileserver1\file.txt”

Dim objFSO  
Set objFSO = Server.CreateObject(“Scripting.FileSystemObject”)

objFSO.CopyFile source, “\fileserver2\file.txt”  
\*

**New**  
\*  
source = “[http://internalserver.com/file.txt](http://internalserver.com/file.txt)”

Dim objFSO  
Set objFSO = Server.CreateObject(“Scripting.FileSystemObject”)

objFSO.CopyFile source, “\fileserver2\file.txt”  
\*

Obviously the above won’t work… so how can I achieve this? copying a file from a url to a folder.  
[/QUOTE]

If http is the only way to get at that file, you will have to programmatically open an http connection from within your code. It will hit the url (the same way a browser would), read it byte by byte, and manually write to the destination file, byte by byte. If you don’t know how to do that, I’ll see what code examples I can find.

This seems like a goofy setup tho. Sometimes it’s best to work with your network admins instead of around them 🙂 Surely if you have a legitimate business reason to have the file copied they can make some kind of accommodation for you?

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 24, 2007, 8:00pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/5 "2007-10-24T20:00:38Z")

</div>

[QUOTE=arseNal]

This seems like a goofy setup tho. Sometimes it’s best to work with your network admins instead of around them 🙂 Surely if you have a legitimate business reason to have the file copied they can make some kind of accommodation for you?  
[/QUOTE]

He probably will, but while I’m letting him ponder it I am looking for alternative solutions that will save him having to compromise his intention to make everything more secure (i.e. not having the original source folder accessable by everyone - ‘annonymous user’)

---

<div class="post-metadata">

**Author:** ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)\
**Post date:** [October 24, 2007, 8:13pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/6 "2007-10-24T20:13:55Z")

</div>

I see. Well, your alternative is to read it with an http connection 😉

You may not actually have to write all the code that does it, there are probably some helper objects that can do it for you. When you say asp script, is it running as an .asp page in an IIS server?

Does something like this help?

[http://classicasp.aspfaq.com/general/how-do-i-read-the-contents-of-a-remote-web-page.html](http://classicasp.aspfaq.com/general/how-do-i-read-the-contents-of-a-remote-web-page.html)

(the first code example on that page shows how to read a URL using an “MSXML2.ServerXMLHTTP” object.)

---

<div class="post-metadata">

**Author:** ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)\
**Post date:** [October 24, 2007, 8:32pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/7 "2007-10-24T20:32:18Z")

</div>

Sorry **Lobsang** , I think you misread me 🙂

I wasn’t explaining to you what you’re trying to do, I was explaining to you what you SHOULD do… **arseNal** used better terminology.

Instead of physically copying the file you want to READ it using http and put its contents into a new file on the server you DO have access to. And then from there, copy it to the last place.

Basically you need fileserver1 to browse over to [Buying and selling domains by experts | Hire a broker today! | Sedo](http://yourdomain.com/file.txt), copy all of the text into a new document (using read) on fileserver1 (imagine opening notepad, copy and pasting) and then using the connection between fileserver1 and fileserver2 to physically copy the new document.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 24, 2007, 9:57pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/8 "2007-10-24T21:57:17Z")

</div>

I have the following code written… It says ‘invalid procedure call or argument’ for the write line, yet the syntax is exactly as I’ve seen in examples on the internet  
\*\*  
\<%  
url = “[Buying and selling domains by experts | Hire a broker today! | Sedo](http://yourdomain.com/file.txt)”  
set xmlhttp = CreateObject(“MSXML2.ServerXMLHTTP”)  
xmlhttp.open “GET”, url, false  
xmlhttp.send “”

```
Dim fso, MyFile

```

Set fso = CreateObject(“Scripting.FileSystemObject”)

Set MyFile = fso.CreateTextFile(“\destination estfile.txt”, True)

text = xmlhttp.responseText

MyFile.Write(text)

MyFile.Close  
set xmlhttp = nothing

%\>\*\*

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 24, 2007, 10:26pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/9 "2007-10-24T22:26:10Z")

</div>

for some reason it ‘works’ if I add a parameter to the createtextfile call - “,true)” (which creates a unicode file)

**Set MyFile = fso.CreateTextFile("\destination estfile.txt", True,True)**

but this creates a gibberish file that I can’t use.

---

<div class="post-metadata">

**Author:** ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)\
**Post date:** [October 25, 2007, 1:16am UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/10 "2007-10-25T01:16:05Z")

</div>

[QUOTE=Lobsang]  
for some reason it ‘works’ if I add a parameter to the createtextfile call - “,true)” (which creates a unicode file)

**Set MyFile = fso.CreateTextFile(“\destination estfile.txt”, True,True)**

but this creates a gibberish file that I can’t use.  
[/QUOTE]  
So what happens if you use “false” as the last parameter?

From here (funny, same site as the other one I found):

[http://classicasp.aspfaq.com/files/directories-fso/why-do-i-get-invalid-procedure-call-or-argument.html](http://classicasp.aspfaq.com/files/directories-fso/why-do-i-get-invalid-procedure-call-or-argument.html)

Last example seems similar to your problem. Also the possible nullity of your **text** variable might be something to look at.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 25, 2007, 3:42pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/11 "2007-10-25T15:42:49Z")

</div>

[QUOTE=arseNal]  
So what happens if you use “false” as the last parameter?  
[/quote]

I already mentioned… I end up with a unicode file which I can’t use for the purpose I wanted to use it for.

> [@](#):
>
> From here (funny, same site as the other one I found):
> 
> [http://classicasp.aspfaq.com/files/directories-fso/why-do-i-get-invalid-procedure-call-or-argument.html](http://classicasp.aspfaq.com/files/directories-fso/why-do-i-get-invalid-procedure-call-or-argument.html)
> 
> Last example seems similar to your problem. Also the possible nullity of your **text** variable might be something to look at.

I saw the same bit of that site… One example produces a unicode file, the other doesn’t work.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [October 25, 2007, 3:51pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/12 "2007-10-25T15:51:34Z")

</div>

Edit: I meant to say the false and the true the other way round in the last but one reply  
either way I either end up with a file I can’t use or a failed procedure call.

---

<div class="post-metadata">

**Author:** ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)\
**Post date:** [October 25, 2007, 4:09pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/13 "2007-10-25T16:09:05Z")

</div>

Well, I guess it is encountering some character that it reads from the URL that it cannot write unless the destination file is created as a unicode file. This

```auto

Set MyFile = fso.CreateTextFile("\\destination	estfile.txt", True)
MyFile.Write("just a plain string")

```

works, yes?

If so, it probably means you have to do some debugging to figure out which character coming from the ServerXMLHTTP object is making the write call barf. Other than that, I dunno, dude. I’m not an asp programmer.

---

<div class="post-metadata">

**Author:** ![galt](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@galt](https://boards.straightdope.com/u/galt)\
**Post date:** [October 25, 2007, 5:24pm UTC](https://boards.straightdope.com/t/copy-a-file-from-a-url-into-a-folder-from-a-web-script-such-as-an-asp-file/423851/14 "2007-10-25T17:24:18Z")

</div>

Alternately, if you can shell out to an external program from your script, you can just find a windows-built copy of the ‘wget’ utility, which in its simplest form just hits a URL and writes the resulting data to a file. 🙂

This page, linked to from the official GNU wget site, lists a location for windows binaries of wget: [FrequentlyAskedQuestions](http://wget.addictivecode.org/FrequentlyAskedQuestions?action=show&redirect=Faq)
