Using xcopy to backup to networked hard drive? - batch-file

I'm wanting to create a batch file that will back up my computer. This is what I have right now:
#echo off
:: variables
net use U:\\...more stuff...
set drive=U:\
set backupcmd=xcopy /s /c /d /e /i /r /y
echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Backup\My Documents"
echo Backup Complete!
#pause
It's simple enough. It works fine for a drive that is physically attached to my computer. However, I have a networked drive that I'd like to use for this task. I've tried a few things I found on forums, but to no avail. In the above code, I started attempting to use "net use." When I run it, I get "invalid drive specification" or "network path not found."
How can I get this to work?
Any help is appreciated.

You have:
net use U:\\...more stuff...
but there needs to be a space between the drive specification and the network share:
net use U: \\...more stuff...

Related

Batch Script to copy folder from Server to user desktop in a network

My goal is to create a batch script to copy a folder with subfolders to user desktop with overwrite option and minimized of command prompt.
I am pushing the script through Group policy in user start up screen.
However I am getting an error when running the script locally. Not sure what I am missing in the script..
#echo off
#cls
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
echo Your Source Path:
set INPUT1=\\X.X.X.X\Test\TMS\
echo Your Destination Path:
set INPUT2=C:\Users\%Username%\Desktop\
xcopy %INPUT1% %INPUT2% /y /s
:minimized
You mentioned folder, so I am writing this assuming you want to create the TMS folder with content on the desktop.
I would try something like this inside of you minimized label. This is untested as I have no Network drives to test with.
for /f "tokens=2" %i in ('net use * "\\X.X.X.X\Test\TMS\" ^| findstr /i Drive') do set "tmpDr=%%i"
mkdir "%USERPROFILE%\Desktop\TMS" >nul
xcopy "%tmpDir%\*" "%USERPROFILE%\Desktop\TMS" /s /y
net use /d %tmpDir% >nul
Some things to note in your code. You have 2 minimized labels, you need to enclose path variables with double quotes to eliminate possible whitespace, you can drop the echo's seeing as you plan on running the script minimized. Last but not least, you do not need to specify full path to the user's desktop as %USERPROFILE% variable already exists as Drive:\Users\Username

create batch script file with user prompt and xcopy

I'm trying to create a batch script file to help an administrative assistant in my work group. Let's call him John.
John receives many CDs daily from clients containing architectural drawings as PDFs. Each CD is unique. John assigns it an Application Number, and creates a new folder on the network drive with the Application Number as the folder name. John then copies all the files on the CD to the newly created folder name. He repeats this process for every CD he receives.
I don't have any programming experience so I hope you can lend me a hand. Here's what I wrote below but it's not working. I get an "Insufficient memory" error. A new folder name based on the user input is correctly created on the network drive path but it's empty.
Thanks for your help! FYI, I'm running Windows 7 64-bit
#echo off
echo Welcome team member!
pause
set /p anumber=Type in the Application Number then press Enter key when done.
mkdir "S:\ARCH\Active Forms and Files\Submittal Floor Plans\%anumber%\"
:: e:\ is the CD drive path
set source=e:\
:: S:\ is a network drive
set destination="S:\ARCH\Active Forms and Files\Submittal Floor Plans\%anumber%\"
xcopy %source%\* %destination%\* /s /e /l /h /i >nul
pause
exit
You were setting the variable %source% to e:\ which was the read by the interpreter as E:\\* when used in %source%\*, leading to an invalid directory. The same was being done to the %destination% variable.
Updated Script:
#echo off
set "networkFolder=S:\ARCH\Active Forms and Files\Submittal Floor Plans"
set "CDDrive=e:"
echo Welcome team member!
:input
set "applicationNo="
set /p "applicationNo=Enter the Application Number and press ENTER: "
if not defined applicationNo goto :input
set "newLocation=%networkFolder%\%applicationNo%"
:actions
md "%newLocation%"
xcopy %CDDrive%\* "%newLocation%" /s /e /i /h>nul
echo Completed!
pause

Open Batch Questions in a separate dialogue box.

I'm trying to make a Back Up.bat file using the RoboCopy command and am having some issues.
What I need is to back up a folder on to an external hard drive, and back up the same folder on to my laptop. My only issue is that the drive letter for my Hard Drive changes sometimes, I would just go to disk management and have a permanent drive letter, but I plan on having the folder backed up to multiple devices and don't want to have to repeat this again and again.
What I've tried so far is:
#echo off
robocopy "C:\Users\Zac\Desktop\Downloading" "%~d0\Backup\File Backups" /E /XF
robocopy "%~d0\Backup\File Backups" "C:\Users\Zac\Desktop\Downloading" /E /XF
I've also tried swapping the "%~d0" for a variety of drive letters, but got no where.
The easy way out is to make the drive letter permenent as shown in the Link.
you can look up the drive letter if you have the volume name:
set "drive=undefined"
for /f "tokens=2 delims=," %%i in ('wmic logicaldisk where (VolumeName ^= "ExternalDrive"^) get caption^,status /format:csv') do set drive=%%i
echo %drive%
where "ExternalDrive" is the volume name of your disk.
(Note: the property status is not used, it's just one of several ways to get rid of the ugly wmic line ending with the desired token caption)

Deleting Desktop Shortcuts Associated With Network Drives?

I've been working to clean up a messy Active Directory as well as a network file system in the same state and I understand the concept of mapping users network drives and currently use a combination of batch and vbs files to do so. However, I need to start fresh and was wondering if there was any way to detect and delete the users shortcuts on their desktop associated with the previous network drives. (Yes - I understand how to delete all of the network drives, but: How do I detect and delete the shortcuts on the desktop associated with them?)
I've already written and custom tailored my own scripts to map drives and place shortcuts. I just need to get rid of any old shortcuts. I can't afford to delete all of the .ink files on the desktop, either. Only any associated with preexisting network drives.
I'm working in an XP / Server 2003 client/server environment.
Another question: If a script runs every time a user logs on through the domain and adds the same network shares over and over again without first deleting them, (even though it would be the same script every time) would it / does it - do any harm? <- I didn't research this one a whole lot yet because i've been crawling through Google and took a peak see through Stack to try and find a solution for the first question/issue.
Any help / advice would be greatly appreciated. Thanks!
Let's say there was before in logon batch
%SystemRoot%\System32\net.exe use Z: \\OldServer\OldShare
and the users created shortcuts to files / directories on this share on their desktop or in a subdirectory of the desktop.
A shortcut file contains always both – the path to the file with drive letter as well as the UNC path with server and share names.
A simple batch file to find in the desktop directory and all its subdirectories *.lnk files containing \\OldServer\OldShare and delete all found shortcut files is:
#echo off
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "\\\\OldServer\\OldShare" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
echo Deleting shortcut file "%%I"
del "%%I"
)
For details about /I /M /S run in a command prompt window findstr /?
As it can be seen each backslash in the find string must be escaped with one more backslash.
It is also possible to search for "Z:\\" instead of "\\\\OldServer\\OldShare"
But be careful with deletion of a *.lnk file just based on drive letter because users could have mapped a different share to this drive letter. Those users would not be happy if their shortcuts are deleted, too.
The batch file could ask the user for confirmation before deleting every found shortcut containing the drive letter of not anymore existing drive:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Searching for shortcuts to drive Z: ...
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "Z:\\" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
echo/
echo Shortcut "%%~nI" might be not valid anymore.
echo/
setlocal EnableDelayedExpansion
set Confirm=
set /P "Confirm=Delete the shortcut (y/n)? "
if /I "!Confirm!" == "y" (endlocal & del /F "%%I") else endlocal
)
endlocal
It is no problem if a network drive mapping using a command like
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
is done in logon batch file on every logon. An error message is output by net use if drive letter Z: is used already, for example if the network drive mapping was done persistent and the user started the computer first without a network connection, then plugged-in the network cable and next after a few seconds entered user name and password to logon to Windows and on domain server of the company.
It is possible to use
%SystemRoot%\System32\net.exe use Z: /delete 2>nul
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
to first delete an already existing network drive mapping before mapping the share to drive letter Z:. I do not recommend to use wildcard * instead of Z: as that would delete also all network drive mappings created by the user.
For computers not only used in the company network, it is often better to make the drive mapping not persistent by using
%SystemRoot%\System32\net.exe use /persistent:no
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
%SystemRoot%\System32\net.exe use /persistent:yes
Windows does not save in this case in Windows registry under key HKEY_CURRENT_USER\Network that \\MyServer\MyShare should be mapped to Z: and therefore the network drive mapping exists only for current user session. The network drive mapping is removed automatically once Windows is restarted or the user logs off.

Batch - Loop through folders and run command

I am trying to remove Dropbox from around 500+ Windows 7 computers using a batch script. Dropbox is installed on a per-user basis under "%APPDATA%\Dropbox".
To uninstall silently you have to run "%APPDATA%\Dropbox\bin\DropboxUninstaller.exe /S". I have tested this command and it works fine, but the problem is it only uninstalls it from the user directory you run it from. I have tried taking the DropboxUninstaller.exe file and running it from the root of C:, but it fails.
I will be pushing this script out via SCCM, so it will run under the SYSTEM account. I need to find a way to loop between all of the user direcories, find which profiles have the Dropbox\bin\DropboxUninstaller.exe path/file and run it in that location.
I have tested logging in as another user (local admin) and running the Dropbox uninstaller from another users directory, and it uninstalls fine for the user whos directory I run it in, so I know this will work.
If anyone could help me out with the correct command, that would be great! I have googled around for an answer, but it doesn't seem to be out there.
Thanks in advance,
Search user profiles, and for each if unistaller exists, execute it (asumming an "standard" installation, maybe this needs to be adjusted)
#echo off
setlocal enableextensions
for /F "tokens=2,*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfileImagePath /s ^| find "REG_EXPAND_SZ" ^| findstr /v /i "\\windows\\ \\system32\\"') do (
call :doUninstall "%%b"
)
endlocal
exit /b
:doUninstall
set "_uninstaller=%~1\AppData\Roaming\Dropbox\bin\DropboxUninstaller.exe"
if not exist "%_uninstaller%" goto :EOF
start "" /wait "%_uninstaller%" /S
goto :EOF
for /r c:\windows %A in (notepad.exe) do "%A" %windir%\win.ini
Remember %%A in a batch and %A at command prompt.

Resources