Copying from network share using BAT - windows

What's the best way to copy a file from a network share to the local file system using a Windows batch file? Normally, I would use "net use *" but using this approach how can I get the drive letter?

Can you just use the full UNC path to the file?
copy \\myserver\myshare\myfolder\myfile.txt c:\myfiles

You can specify the drive letter for net use. Put this in the command prompt for more info:
net use /?

You can also use xcopy and robocopy:
xcopy "\\server\share\path" "destination"
robocopy "\\server\share\path" "destination"

Related

Copy files from One Server to Another Server using Batch Command - Windows machine

How to copy files from one server to another server(VM) using a windows batch command. ?
I have used below command
syntax : xcopy \\source_path \\serverIP\Destination_path /s /a /d
example : xcopy \\c:\repo\testproject \\10.101.101.11\C:\test\project /s /a /d
I'm getting the below error "Invalid drive specification" as of now.
Do I need to give credentials for accessing the VM ? If yes then where and how?
I have checked the destination path is correct.
Is there any other command should be used in that case ?
Do I need to give credentials for accessing the VM ? If yes then where and how?
If the passwords on the source and target machine are the same then no credentials are necessary, otherwise yes they will need to be provided. Two solutions you can consider:
Use "runas" and specify the credentials there
Use the temporary network drive solution proposed in the comment by UserPo41085. This solution uses the "net view" command which has credential parms.
I have checked the destination path is correct.
example : xcopy \\c:\repo\testproject \\10.101.101.11\C:\test\project /s /a /d
Both paths in the example provided are a mixture of standard "DOS" paths and UNC paths. UNC paths reference a share name and not a disk letter.
The example in the link below copies a file on the local machine called zz_yuv.png to a machine called "ws9" which has a share called "c9.system" and the share is mapped to the root folder of the c: drive on ws9. If you are running an account which is a member of the administrators group you can use the admin shares...(admin$, c$ etc.)
xcopy example
Is there any other command should be used in that case ?
Robocopy is built into the later versions of windows. It does come with a learning curve but is much more robust than xcopy. Just as a note, robocopy will be under the same types of credentialing restraints as xcopy - it just has more and better features for copying.

batch xcopy, robocopy copy to network path w/ credentions w/o using net use

Is there a way to copy to a network path to a folder that requires credentials using any copy commands without using net use.
Using xcopy you can achieve the goal across network path but I can not get it to take credentials
xcopy /E/R/K C:\folder\ \\XXX.XXX\folder\*.*
This is what I have tried and doesn't work along, with other similar inputs.
robocopy /e C:\folder\ "\\XXX.XXX\folder\ /user:name passwrd"
xcopy /E/R/K C:\folder\ "\\XXX.XXX\folder\*.* /user:name passwrd"
Thanks.
The utilities don't have mechanisms to handle authentication. You will have to use net use or map the drive. Not sure why you don't want to use net use, but either way the drive will need to be authenticated before using the copy utilities. Maybe try using RunAs on the xcopy and use the credentials needed.
You can also do this in an AutoIt script wrapping the RunAs function in the command line.

pushd \\network\path returns CMD does not support UNC paths as current directories

I was using a batch file to access some files from network.
I am using
pushd \\Network\path
to navigate to the networked directory to initiate some scripts
My bat file was working perfectly fine till this morning I saw "cmd returning CMD does not support UNC paths as current directories".
I have seen this error message when using cd instead of pushd to navigate to a network directory but I can figure out why I am getting error for a previously working bat file.
And I did make sure that network location was online and accessible other way round.
Be sure to also check that you haven't just run out of drive letters. If you have a rogue script that isn't using popd after it's done with the drive, or is crashing before it gets to popd, you can end up with a bunch of garbage mapped drive. Easy to check as they'll show up in net use and "My Computer". For some reason cmd will give this cryptic error (CMD does not support UNC paths as current directories.) instead of telling you it ran out of drives to map to.
From pushd /?:
If command extensions are enabled the PUSHD command accepts network
paths in addition to the normal drive letter and path. If a network
path is specified, PUSHD will create a temporary drive letter that
points to the specified network resource and then change the current
drive and directory.
Do any mapped drives show up in net use after pushd is executed?
If you're not getting some kind of network authorization error, make sure command extensions are enabled (I'm not sure why they wouldn't be.)
cmd /x will enable extensions for the current CMD session. Try that prior to executing your batch script.
The "main switch" is in HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
EnableExtensions should be (DWORD) 1

Command line to copy files from remote location

I want to write a script that will copy some files from LAN computer to my pc. I don't know what would be the syntax of this command.
Could anyone help me related to this?
One thing more, that in my case both the machines are using Windows.
xcopy /z \\myServer\myFolder c:\
/Z Copies networked files in restartable mode.
where \myServer\myFolder is a unc path , or if you have network drive ( for example o:) so use :
xcopy /z o: c:\
net.exe use to map remote share to temporary drive letter
copy or robocopy or xcopy or whever to copy the files
net.exe use to remove used mapping and free the letter
Note that Windows (95/98/ME) and WindowsNT ( NT3 / NT$ / 2000 / XP / Vista / 7) are radically different operating systems and have different DOS commands and options, especialy regarding copying.

Copy files to network computers on windows command line

I am trying to create a script on Windows which when run on an admin PC:
Copies a folder from the admin PC into a group of network PCs by specifying the ip address / range
For each destination PC, Navigate into the folder and run another script file.
Using the method described by seanyboy here:
net use \\{dest-machine}\{destfolder} {password} /user:{username}
copy {file} \\{dest-machine}\{destfolder}
I'm not sure on how i can write a 'for' loop to go through each 'dest-machine' and perform step 2. Any ideas would be greatly appreciated.
check Robocopy:
ROBOCOPY \\server-source\c$\VMExports\ C:\VMExports\ /E /COPY:DAT
make sure you check what robocopy parameter you want. this is just an example.
type robocopy /? in a comandline/powershell on your windows system.
Below command will work in command prompt:
copy c:\folder\file.ext \\dest-machine\destfolder /Z /Y
To Copy all files:
copy c:\folder\*.* \\dest-machine\destfolder /Z /Y
Why for? What do you want to iterate? Try this.
call :cpy pc-name-1
call :cpy pc-name-2
...
:cpy
net use \\%1\{destfolder} {password} /user:{username}
copy {file} \\%1\{destfolder}
goto :EOF
Regarding step 2., check manual for psexec command (sysinternal tools)

Resources