net use command: delete fails due to files in use - windows

I have a .bat file that runs overnight on our Windows server. It maps 2 network drives and does some file copying and zipping. At the end of the script, it removes the network drives. However, on occasion, the job is not completing. I review the logs and it shows that my .bat file is prompting for a confirmation on the following line due to the drive in use.
net use F: /d
It was suggested to add /y to the end of the script to handle the prompt. However, I would like to confirm that the drive truly is still not in use.
Is there a way to keep attempting to remove the drive until it is successful without prompting?

Related

Copy File to Another Server Using Batch File [duplicate]

I have a bat file copying files from current machine to mapped network drive (one line, xcopy command).
It works when I RDP to server. However, when I run as a scheduled task, and configure it to run under the same user I'm logged in, it doesn't work and give error 0x4.
Is there a way I can achieve this?
I also try dsynchronize and it works when I click synchronized. When I run it as service same issue.
I was able to figure it out. Following batch files works under scheduler, even as local system account:
net use m: \\server\share /U:server\user password
xcopy C:\source m: /E /Y
It maps a network drive every time and then copy to that drive
It's possible to copy files to a UNC path without mapping as a network drive.
Just try to set the UNC path in quotes.
copy * "\\server\share"
Without the quotes, i got a "syntax error" running on Windows7 command line.
I had similar issue where I wanted to copy file(s) from a server to hundreds of other servers without mapping a remote drive to my local PC. I didn't have enough drive letters to map hundreds of remote machines to my local PC! I couldn't just map the remote drive and copy.
I thought I could use copy, xcopy, or robocopy, and specify my creds to the copy command. But none of the copy commands had any options to provide credentials to remote system.
Thanks to the post above, I was able to create a small batch file where I just loop through my hosts, and keep re-using just one drive mapping for all my hosts.
Here is my batch file...
echo #off
for /F %%j in (pchostslist1.txt) do (
net use z:\\%%j\c$ /user:domain\myusername mypassword
mkdir \\%%j\c$\tmp\mynewdir
xcopy c:\anyfile.txt \\%%j\c$\tmp\mynewdir
net use z: /delete
)
I had a similar issue and instead of using net use I simply needed to store the password as part of the scheduled task. You'll notice that it says it only has access to local resources if it's ticked.
Who maps the network drive? And are you using the mapped name, instead of the underlying UNC native path? Because it sounds like the mapped drive is setup in your login script, which doesn't run if you're not logged in. So, in a scheduled task, you do have the correct credentials for the UNC path, but no mapped drive letter.

File Move Scheduled Tasks not running bat file

I know this question might have been asked in other ways, but all the things I have read and tried had not yet fixed my problem, so I am hoping to get some help here with context to my issue.
The problem:
I need to move files from a local drive to a network drive (the network drive is a SharePoint mapped library) on my server in Windows Azure (don't think the azure part matters, but it provides context).
My thought was to schedule a task that will run a bat file to move the files I need moved and do so frequently (every 5 to 10 minutes). The batch file I have created does what I need it to when I manually run the batch file but not when the task runs it.
Here is the batch file:
echo Write log file > LogStart.txt
C:\Windows\System32\robocopy.exe "\\PCICSWKS001\D$\ToBeMoved" "V:" /s /e /MOV /r:0 /W:0
echo Write log file > LogEnd.txt
You can see that it writes a log file before and after running robocopy.exe.
When the task runs it does write both of these log files so I know that the batch file is at least running.
You can also see that I have tried using the UNC path for the drive in the source, that is because I was reading that the task scheduler might not be able to pick up properly on the drive letters. When I put the machine name in for the destination and run the batch file myself or with the scheduler it errors.
Here is the action taken by the task:
Here is the task general tab
Any assistance would be much appreciated.
I had that same error with Robocopy after a few times of running it:
ERROR 3 (0x00000003) Getting File System Type of Destination
I think it has to do with how robocopy scans the destination when it has a fair amount of files on it. It worked fine when I started the copy job and the Azure destination was empty.
Anyway, I think you should try to use the AzCopy command as that should be less error prone and faster since it was designed for this kind of thing. It's command line switches are similar to robocopy so it should feel pretty familiar.

Script to remap current network drive?

We need to disconnect and re-map a network drive on Windows 7, using a set of scripts (or an app) that runs off the same network path.
That is, I need something that loads itself into RAM before it runs, so it continues to run after the drive is disconnected.
Any ideas?
Please note that 16-bit apps are NOT supported in 64 bit systems (this explains why the Novell utility failed).
You would need a vbs file running throughout a logon session to remap drives if it's disconnected by user. Need to make this script to run when domain user logs on - e.g. Logon Script in AD or GPO. There are many ways to do it.
You could even disable "Remove Network drives" feature from Explorer GUI via GPO or Reg key (net use command still works).
Or you can tweak solution by Julius for this SO question to fit your need. But consider performance impact of the vbs - only check every n minute(s) in an infinite loop.
We do something similar. We have a batch file on the network that maps the drives a user needs. We update the batch file from time to time, and users run it from a shortcut that we've placed on their desktop:
C:\WINDOWS\system32\cmd.exe /c (#echo off&if not exist \\172.x.x.x\Login (echo Unable to access server&pause) else (md c:\TMP > NUL 2>&1 &copy \\172.x.x.x\Login\MapDrives.bat C:\TMP /y > NUL 2>&1 &call C:\TMP\MapDrives.bat&del C:\TMP\MapDrives.bat&rd c:\TMP))
You can see that it checks to see if they can access the server, and if they can, it creates a folder C:\TMP, copies the MapDrives.bat file locally, then runs it. Since it is running locally, it can remap network drives without terminating it own execution. And we can update the batch file on the server without pushing it to each user's computer.
If you don't want to create a shortcut with the long command line above, it might work to create a second batch file on the server (e.g., RunMe.bat) that users run from the server. You could place all of the code from the shortcut in the RunMe.bat and accomplish the same thing. Of course, you'd want to add one more line of code to change to the local drive (so Windows doesn't hold open a handle to the network drive). Something like this:
#echo off
C:
if not exist \\172.x.x.x\Login\MapDrives.bat (
echo Unable to access server
pause
) else (
md c:\TMP > NUL 2>&1
copy \\172.x.x.x1\Login\MapDrives.bat C:\TMP /y > NUL 2>&1
C:\TMP\MapDrives.bat
)
I kept the if not exist ... because you might place the RunMe.bat in a different location than the MapDrives.bat, so it still makes sense to verify the user can access the file. Because I didn't use call C:\TMP\MapDrives.bat, it transfers control to the local batch file and any handles to the server should be closed so the drive can be remapped. This means however, that you cannot place more commands after the C:\TMP\MapDrives.bat command.

Net use works in batch file, fails in script

We are a small office with a Linux appliance server (SAMBA) and Win7HP on the desktops. We have one machine that acts as a "backup server" and backs up the server nightly.
Writing what was supposed to be a quick & dirty addition to the backup script that would loop like this:
a. net use * /d /y
b. net use u: \\ourserver\userNN /u:userNN password
c. run sync util to sync u: to this user's storage space on backup server
d. repeat
If I manually create the above in a batch file, everything works fine. (However, for many reasons the procedure must be automated and tied into our existing backup scripts.)
If a script creates the identical batch file and runs it, the first user is logged-in and syncronized, but all subsequent net use attempts fail.
Similarly, if the script attempts to map the drive through code, this message is received:
Unrecognised network error #183
The above error suggests that the network drive/path is unavailable -- but a net use command typed at the command line using identical parameters is successful.
The "backup server" is a Win7HP box with UAC disabled. When cmd prompt is launched, all windows are preceded by the word: Administrator: -- however, when the script launches a command window to net use the drive, the cmd titlebar lacks that prefix.
Any thoughts / ideas?
In order for users to execute this code properly, they will need to have the proper credentials to do so. Also to automate this task, just use Task Scheduler and it should work fine.

Can't copy files to UNC Destinations if BAT file is called via scheduled task

I have a bat file copying files from current machine to mapped network drive (one line, xcopy command).
It works when I RDP to server. However, when I run as a scheduled task, and configure it to run under the same user I'm logged in, it doesn't work and give error 0x4.
Is there a way I can achieve this?
I also try dsynchronize and it works when I click synchronized. When I run it as service same issue.
I was able to figure it out. Following batch files works under scheduler, even as local system account:
net use m: \\server\share /U:server\user password
xcopy C:\source m: /E /Y
It maps a network drive every time and then copy to that drive
It's possible to copy files to a UNC path without mapping as a network drive.
Just try to set the UNC path in quotes.
copy * "\\server\share"
Without the quotes, i got a "syntax error" running on Windows7 command line.
I had similar issue where I wanted to copy file(s) from a server to hundreds of other servers without mapping a remote drive to my local PC. I didn't have enough drive letters to map hundreds of remote machines to my local PC! I couldn't just map the remote drive and copy.
I thought I could use copy, xcopy, or robocopy, and specify my creds to the copy command. But none of the copy commands had any options to provide credentials to remote system.
Thanks to the post above, I was able to create a small batch file where I just loop through my hosts, and keep re-using just one drive mapping for all my hosts.
Here is my batch file...
echo #off
for /F %%j in (pchostslist1.txt) do (
net use z:\\%%j\c$ /user:domain\myusername mypassword
mkdir \\%%j\c$\tmp\mynewdir
xcopy c:\anyfile.txt \\%%j\c$\tmp\mynewdir
net use z: /delete
)
I had a similar issue and instead of using net use I simply needed to store the password as part of the scheduled task. You'll notice that it says it only has access to local resources if it's ticked.
Who maps the network drive? And are you using the mapped name, instead of the underlying UNC native path? Because it sounds like the mapped drive is setup in your login script, which doesn't run if you're not logged in. So, in a scheduled task, you do have the correct credentials for the UNC path, but no mapped drive letter.

Resources