1.

Solve : VB script copying across networks?

Answer»

hey all,

I'm trying to write a vb script that will copy a batch file to another Pc across my network.

This is what i have so far but it doesn't work and keeps pointing the copyfile line as a reason for the failer, but tbh i can't see where it's going wrong.

any ideas???


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim fso
CONST OverwriteExisting = True
fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile("C:\batch\remote.bat", "\\10.1.0.100\c$\remote.bat", OverwriteExisting)
MessageBox.Show("Transfer Complete")
VBScript or VB.Net?

Try something like this for VBScript:

Code: [Select]Dim fso
Const OverwriteExisting = True
SET fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\batch\remote.bat", "\\10.1.0.100\c$\remote.bat", OverwriteExisting
MessageBox.Show("Transfer Complete")

I'm guessing input and output paths are correct. cheers for the reply,

in answer to UR question vb script.

i get a (ctl_e_permissiondenied) error.

I have no idea as to how to GAIN access of the remote machine.

If this where dos i'd use

net use \\10.1.0.100 /user:me spottydog
copy c:\batch\remote.bat \\10.1.0.100\c$\remote.bat
pause

but since i moved over to vbscript today i'm finding it hard to get the commands i require in.

any help is very much appreciated. You can duplicate the batch code, but you'll need a network object exposed so you can use the MapNetworkDrive method:

Code: [Select]Dim fso, WshNetwork
Const OverwriteExisting = True
Set WshNetwork = CreateObject("Wscript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")

WshNetwork.MapNetworkDrive "X:", "\\10.1.0.100\c$\"

fso.CopyFile "C:\batch\remote.bat", "X:\remote.bat", OverwriteExisting
MessageBox.Show("Transfer Complete")

I used drive X: arbitrarily, change it if you wish. Note that the remote machine must be up and running, presumably with File & Printer Sharing For Microsoft Networks activated.

Good luck.

Normally I'd ping the other machine to see it's availability, but that requires the Windows Management Interface which will only complicate things right now.thanks.

Have done as requested but i now get "unauthorizedAccessException was unhandled" error.

It seems to be having problems with the permissions on both machines, i'm logged on as admin on both so i can't see why.

can i add a /user:me spottydog line in somewhere here?

WshNetwork.MapNetworkDrive "X:", "\\10.1.0.100\c$\"

any more ideas?Forgot about the name and password:

WshNetwork.MapNetworkDrive "X:", "\\10.1.0.100\c$\" ,, me, spottydog

Questions:

Are you using wscript or cscript?

Which line in the script threw the error?

Can you ping 10.1.0.100 from the command prompt?

The reason I ask is that "unauthorizedAccessException was unhandled" is a rather odd error coming from VBScript. Might be permissions. Can you run a directory listing on X: if you map it manually with net use?

Let us know. thanks again for replying,

I have put the;

WshNetwork.MapNetworkDrive "X:", "\\10.1.0.100\c$\" ,, me, spottydog - line in, but

,, me, spottydog become's , , me, spottydog and it tell me that it not valid.

I am able to ping it from a prompt (actual pc i'm post from now)

The line that threw the error was this very line (before the ,, me, spottydog bit)

?Sometimes I amaze myself with my carelessness. Literals need to be quoted. I promise to write that on the board 100 times.

WshNetwork.MapNetworkDrive "X:", "\\10.1.0.100\c$\" ,, "me", "spottydog"

With any luck that should work. right,

I've added the quotes but it still wont copy anything.

This is what i have;

Dim fso, WshNetwork
Const OverwriteExisting = True
WshNetwork = CreateObject("wscript.network")
fso = CreateObject("scripting.filesystemobject")
WshNetwork.mapnetworkdrive("x:", "\\10.1.0.100\c$", , "username", "password")
fso.CopyFile("C:\batch\remote.bat", "x:\remote.bat", OverwriteExisting)
MessageBox.Show("Transfer Complete")

The red line brings up an error saying "access is denied", but i know that the username and password to be right.

have i got them the wrong way round???

I have checked and i can map to 10.1.0.100\c$ from a run box, (windows does ask for the guest password - which i don't know!)

cheers again for ur helpQuote

The red line brings up an error saying "access is denied", but i know that the username and password to be right.

Quote
I have checked and i can map to 10.1.0.100\c$ from a run box, (windows does ask for the guest password - which i don't know!)

These statements seem contradictory. Do you know the user & password? Is there a user & password?

Quote
have i got them the wrong way round???
UserID and password are in the correct order. Whether they have the correct values is the question.

Do not use parenthesis for MapNetworkDrive or Copyfile. When I tried your code, I got a syntax error. Also it's set fso.....you are creating an instance of the FileSystemObject not equating two variables.

Code: [Select]Dim fso, WshNetwork
Const OverwriteExisting = True
WshNetwork = CreateObject("wscript.network")
Set fso = CreateObject("scripting.filesystemobject")
WshNetwork.mapnetworkdrive "x:", "\\10.1.0.100\c$", , "username", "password"
fso.CopyFile "C:\batch\remote.bat", "x:\remote.bat", OverwriteExisting
MessageBox.Show("Transfer Complete")

You never told us whether you're using wscript or cscript.



That "unauthorizedAccessException was unhandled" message still has me puzzled. Yes, the user name and password are right.

I can map to the path in the script (\\10.1.0.100\c$) but when the authentication windows pops up it has the \\computer name\guest inserted in the username and it's grayed out so i can't change it.

Since there is no password on the guest account and you can't leave the authentication password field empty when mapping, i can't open the c:\. Hence why i use an admin account, both of the machines that I'm using are mine, so i have full access to all accounts.

As to your question about weather i'm using wscript or cscript, thb i don't know!!!!

I have installed Microsoft visual basic 2005 SP1, I opened it up and then opened a new project. (if there's a way of telling let me know and I'll check for you.)

I have this code inserted under a handle of button4 (click)

Dim fso, WshNetwork
Const OverwriteExisting = True
WshNetwork = CreateObject("wscript.network")
Set fso = CreateObject("scripting.filesystemobject")
WshNetwork.mapnetworkdrive "x:", "\\10.1.0.100\c$", , "username", "password"
fso.CopyFile "C:\batch\remote.bat", "x:\remote.bat", OverwriteExisting
MessageBox.Show("Transfer Complete")

The set that you refer to keeps disappearing after i put it in. (i have copied your code and replaced it for mine, But i still get strange errors like, "access denied" and "path not found".

I have tried subbing the c:\ on the remote machine for a newly made e:\ thats shared, but it made no difference. (also tried to map to the e:\ but still got the guest username inserted in the user field)

I have also tried to use the computer name instead of the ip but as I'm on a LAN VPN extension from work, (where there is another Pc with the same name) it keeps saying "access denied"

As i said yesturday I have only just started using vb so i'm a complete newbee, (but i'm willing to try to work it out for my self)

If there is anything else that you can suggest, (i might start a new project and just have that simple code to get to work first before making the form menu.) that would help i would forever be in your debt.

Thanks again




Quote
I have installed Microsoft visual basic 2005 SP1
I suspected as much. You are using VB.Net which is a full fledged programming language. You can certainly use VB.Net for your project, but if all you need is to copy a file(s) across the network, VBScript is much easier and a LOT less overhead. Plus there is none of that pesky compiling and linking.

To use VBScript, copy & paste the code that has been posted into your editor (Notepad works fine). Save the script with a VBS extension. Open up a command prompt and run the script as:

cscript scriptname.vbs

You can learn all about VBScript at the Microsoft Script Center.

Good luck. cheers,

I'm gonna to do that for this, but also have a larger program in mind.

I currently have about 7/8 batch files that transfer files and run batch files on remote machines. I then have a menu.bat that simply points to the files needed for the operation that you've requested.

Due to it getting bigger and bigger and because of my staff needing to use it as well, I require a windows based grafical interface. I think i'll just start by having bguttons on the from and then when clicked openning the batch file that deals with it. once i've read up some more i move to converting the batch files them-selfs to vb script.

Thanks very much for your time and effort. (if i manage to accomplish this any time soon I'll post on this thread.)


Discussion

No Comment Found