Page 1 of 1

Batch file to attach remote shared drive and copy file?

Posted: 08 Sep 2012 07:16
by adda01
I need a batch file that when run attaches a shared drive / directory on a server and copies a file from that server.

So say I want to run the batch file on my box, batch file should create a new drive letter on this local box say named "V:" and give me access to a directory on a remote server through this drive letter. Once the drive is mapped, the script should then copy a specific file from the directory on the server (the file will end in "050912.rpt.zip" but there will be an unknown string before this text in the filename, so the script needs to identify the correct file from just the last half of its file name). Once it has copied the correct file then I want it to delete the mapped drive, so delete the "V:".

This process will then repeat for another server. Im not interested in how this would loop or anything for now, all I want to know is how the code that I have come up with below (so so clever I know) can be amended so that it does what I explained above. (I know that "NET USE" is the command that I need to use here). Here's what I have so far:

======================================================

net use V: \\remoteserver/directory
IF EXIST v:\050912.rpt.zip
*this is obviously where i need a line to look for and copy the file*
net use V: /delete

======================================================

Re: Batch file to attach remote shared drive and copy file?

Posted: 08 Sep 2012 09:18
by foxidrive
Either of these should work.

Code: Select all

@echo off
md "c:\files" 2>nul
pushd "\\remoteserver\share"
if exist \*050912.rpt.zip copy \*050912.rpt.zip "c:\files"
popd


Code: Select all

@echo off
md "c:\files" 2>nul
if exist "\\remoteserver\share\*050912.rpt.zip" copy /b "\\remoteserver\share\*050912.rpt.zip" "c:\files"

Re: Batch file to attach remote shared drive and copy file?

Posted: 08 Sep 2012 09:42
by adda01
cool thanks, I will try this when I'm at work on Monday, unfortunately I am unable to now as I don't have access to the network.

Just a few questions:

1.) Can the code that you posted above can just totally replace mine? I don't need to use the "NET USE" way of doing it?

2.) Will this work on a Windows Server 2003 platform (the local and remote servers both being Win 2003).

3.) Why is "2>nul" there?

4.) I need to do this for quite a few servers, but if I just copy paste this code in the batch file for each server, and edit it to contain the specific servers name, then it should be fine? (I know some kind of loop would be better, where the servernames are read in from another file but I just want to get this working first)

5.) I think the account I will be using on the local box (where I will run the batch file) will be allowed to access the remote folder, however, just in case it isn't, can you specify a username and password with your way of doing things? If so, how would I do this? I think you can with "NET USE".

6.) Your way of doing it isn't really mapping a drive is it? because the share is just made to be the current directory, its not like another drive letter would appear on the local box and when clicked I would see the contents of the shared folder on he other server? if this is so then thats fine i think, just trying to understand it.

Below I have just put a few comments for what I think each line in your code does:

Code: Select all

@echo off
md "c:\files" 2>nul                   REM Sets "c:files" to be the directory where the files will be copied into and stored.
pushd "\\remoteserver\share"   REM makes "\\remoteservername\share" the current directory, basically it connects to it.
if exist \*050912.rpt.zip copy \*050912.rpt.zip "c:\files"  REM if the file exists then copy it
popd REM stops "\\remote\share" from being the current directory, basically disconnects from it.



I'm grateful for what you have posted, I hope you don't mind my questions, thank you :)

Re: Batch file to attach remote shared drive and copy file?

Posted: 08 Sep 2012 10:04
by adda01
Also, I forgot to mention, there will be two files in the shared directory for each serve that end with that text "050912.rpt.zip" so would your code copy both files? or would it only work for one?

Re: Batch file to attach remote shared drive and copy file?

Posted: 08 Sep 2012 20:24
by foxidrive
adda01 wrote:there will be two files in the shared directory for each serve that end with that text "050912.rpt.zip" so would your code copy both files?


yes.

2>nul sends the STDERR stream to nul so you do not see any harmless error messages on the screen.

1.) Can the code that you posted above can just totally replace mine? I don't need to use the "NET USE" way of doing it?


yes.

2.) Will this work on a Windows Server 2003 platform (the local and remote servers both being Win 2003).


yes

4.) I need to do this for quite a few servers, but if I just copy paste this code in the batch file for each server, and edit it to contain the specific servers name, then it should be fine? (I know some kind of loop would be better, where the servernames are read in from another file but I just want to get this working first)


Adding a loop is easy - if the filenames change on every server then the text file will need a format like: \\server\share\file*name.txt where * is a wildcard that matches your filespec. A FOR /F IN DO loop can then extract the filename.ext and the path.

5.) I think the account I will be using on the local box (where I will run the batch file) will be allowed to access the remote folder, however, just in case it isn't, can you specify a username and password with your way of doing things? If so, how would I do this? I think you can with "NET USE".


If you need to authenticate then you will need to use NET USE but the username and password will be in clear text in the batch file.

6.) Your way of doing it isn't really mapping a drive is it? because the share is just made to be the current directory, its not like another drive letter would appear on the local box and when clicked I would see the contents of the shared folder on he other server? if this is so then thats fine i think, just trying to understand it.


The way PUSHD works (that I have read) is that it maps the remote machine to a random free drive letter but you access the share as if it were a local folder.


Note that specifying the root as I did here may not work:

Code: Select all

if exist \*050912.rpt.zip copy \*050912.rpt.zip "c:\files"


If the files are in the share folder then this will probably work instead.

Code: Select all

if exist *050912.rpt.zip copy *050912.rpt.zip "c:\files"

Re: Batch file to attach remote shared drive and copy file?

Posted: 09 Sep 2012 03:02
by adda01
ok thanks foxidrive, I will try and add a loop myself, I 'll try a "FOR" loop, might post the code back here.

I'm really hoping that I don't need to authenticate, but in case I do, is there any chance you are familiar with NET USE? could you post the equivalent of your code above but using NET USE and with an example username and password? I have typed "NET USE ?" in the cmd prompt to get help and also googled but I just get a little confused as to what the actual format should be, once I have learned more batch I think I would be ok. But for now, that would be massively appreciated :) thank you!

also, why wouldn't you method work with the root? so I can't try and set "md c:\" but I can try say "C:\examplefoldername" put the files in there? just as long as they aren't litlerally at "C:\" and thats it...

Re: Batch file to attach remote shared drive and copy file?

Posted: 09 Sep 2012 03:48
by adda01
ok could I have some help on the loop please? Im a little stuck as to the detailed syntax. This is what I have so far, I know that its not right though or near complete:

Code: Select all

@echo off
md "c:\files" 2>nul

for p in (c:\foldername\list.txt)
do

pushd "\\remoteserver\share"
if exist \*050912.rpt.zip copy \*050912.rpt.zip "c:\files"
popd


I guess I would set the P to an integer value and have it increment, like decrease in value for aas many ties as there are lines in the "list" file. Would I do that part in the list file or in the code above though, I don't know exactly how to go about this...

Re: Batch file to attach remote shared drive and copy file?

Posted: 09 Sep 2012 07:22
by foxidrive
Wait until you find out if you have permissions and where the files are located.

Re: Batch file to attach remote shared drive and copy file?

Posted: 09 Sep 2012 11:08
by adda01
ok I will do that friend. thank you, I will post back when I know.