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)
Related
I need help with a batch script.
I want one folder to sync with external usb device that has specific name (not drive letter; e.g. john's usb).
I found formula to sync folders:
robocopy "source folder" "destination folder" /e /purge
Source folder is not problem because it's specific folder on my drive but destination folder should be external usb with specific name (e.g. John's usb).
I can't do this using drive letters because sometimes I have more than 1 usb sticks connected in PC.
Hope someone can help.
Regards
find the correct drive letter:
for /f "usebackq tokens=2 delims=:=" %%a in (`wmic logicaldisk where VolumeName^="John's usb" get caption /value`) do set drive=%%a:
if "%drive%"=="" (
echo not inserted
) else (
echo inserted as %drive%
)
In a batch file, can I copy a folder located by a constant path like \\?\Volume{GUID}?
When copying (copy, xcopy or robocopy) a directory and its content from a local removable drive (a usb external drive for instance) to another location on the same drive, I'd like to use unique and constant absolute paths like \\?\Volume{GUID} to avoid using drive letters that can change over time.
To operate the copy, the batch file is meant to be placed on the removable device, but in case the file is moved or placed somewhere else I'd rather be sure it's operating on the good drive.
So far I've tried:
COPY can handle \\?\Volume{GUID} paths to copy a file but cannot copy
folders
XCOPY returns an "invalid drive" error
ROBOCOPY gives a "network path not found, wait 30 sec..."
for each command above : syntax variations with \\?\UNC\Volume{guid} and with the trailing "\"
Am I doing something wrong or is this just not the way to do that?
Is there another way to use invariant locations?
Ideally it should involve the least tweaking possible. By tweaking I mean: labeling the drive or giving it a fixed letter, etc.
Have a look at pushd /? and popd /?
pushd \\?\UNC\Volume{guid}
will short time map the UNC path as a network drive and change the directory to it according to the next free drive of your computer from the end of the alphabet.
For example, you got Z:\ and Y:\ already it will map the path to X:\ and change the current directory to X:\.
From that on you can go for copy filefromServer.foo C:\localfile.bar
The other commands such as XCopy can use the mapped drive as well.
To unmap the drive use popd.
None of the copy function can work with unc path.
Thus the only way is a workaround that consists in retrieving the drive letter through wmic command.
Here's the code :
for /F "usebackq tokens=1,2 delims==" %%G in (`
wmic volume where "DeviceID='%_volID:\=\\%'" get DriveLetter /value
`) do set _%%G=%%H
with _volID being the variable containing the unc path of the volume in question
I have a file TestProject.dll and it resides in different locations on my computer like D:\Folder1\TestProject.dll, D:\Test\Info\TestProject.dll etc.
I want to find all these locations wherever it is located and prepare a text file (SearchResults.txt) which looks like as shown below:
D:\Folder1\TestProject.dll
D:\Test\Info\TestProject.dll
I want to do this using a batch scripting file. I am new to this scripting.
Please help me here.
use dir /s /b to search in a single drive.
Build a loop around that to check every disk.
put the complete output into a file.
(
for /f %%a in ('wmic logicaldisk where "drivetype=3" get caption^,size^|find ":"') do (
echo now checking drive %%a...
dir /b /s %%a\TestProject.dll
)
)>SearchResults.txt
Note: we don't really need the size here, this is only one of several ways to come around wmic's ugly line endings, that would ruin the rest of the code
where drivetype=3 means "Harddisks only" (drop it, if you want to search in all drivetypes (Thumbdrives, CD, whatever)
Remember: this will search through your whole file system(s), so it will need some time.
I'm a mortgage loan guy in Seattle, WA and I frequently set up a folder hierarchy into which I save a client's documents and as they come in to me. I've been creating these manually for years and I'd like to save the 3 to 4 minutes it takes to set these up by using a batch file.
So... I have a default set of folders, some of which contain a couple of small Adobe PDFs. What I'd like to do (and cannot make happen) is to run a batch file that would facilitate some custom remarks or input from me during the batch so that with a click and a couple of keystrokes, I have an organized folder setup for a new client within seconds rather than minutes.
I've written the following but it isn't producing any output folders or files.
______not sure character terms show correctly - see linked images below for actual______
#echo off
::Ask
echo Your Source Path:
set INPUT1=
set /P INPUT1=Type input:
echo Your Destination Path:
set INPUT2=
set /P INPUT2=Type input:
C:\WINDOWS\system32\xcopy.exe /e /v %INPUT1% %INPUT2%
My responses were:
to the first prompt "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014"
to the second prompt "E:\DV8333 MY DOCUMENTS\001 CLIENTS\"
I have verified that xcopy.exe is in fact located as indicated above.
I'm on XP SP3
My actual paths and .bat file are shown in the linked image for clarity.
http://www.avidrecording.com/images/01.png
Thanks in advance, much appreciated.
#Rem save this as a .bat file and run
#echo off
set /P source=Enter Source Folder:
echo %Source%
set /P destination=Enter Destination Folder:
echo %destination%
xcopy %source% %destination% /v /i /e
Fundamentally, your problem appears to be that the xcopy command can't figure out which of the data it is receiving is parameters and which switches and which superfluous because you have spaces in your directorynames.
Fortunately, the cure is simple. "quote your parameters"
C:\WINDOWS\system32\xcopy.exe /e /v "%INPUT1%" "%INPUT2%"
Now - the path to xcopy.exe is probably superfluous - as is the extension, so
xcopy /e /v "%INPUT1%" "%INPUT2%"
is more than likely all you'd need.
(I'd caution to experiment with a throw-away destination until you've perfected your method. I use a RAMDISK...)
Also, if you're copying a template, then there's no apparent reason for all the folderol about inputting Input1. If you have more than one template set, set up a separate batch and shortcut for each one with a fixed template path, eg
xcopy /e /v "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014" "%INPUT2%"
Note the use of quotes to defeat the evil spaces.
Next, your destination could be built, but may contain spaces. For instance, you may have a client to which you wish to refer as "037 - Fred Nurk". Now it's a pain to have to type in the full path, so make it easy. Just type in the 037 - Fred Nurk part and let batch fill in the rest.
xcopy /e /v "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014" "E:\DV8333 MY DOCUMENTS\001 CLIENTS"\"%input2%"\
Note that this will append the input as a directory under E...001 clients. Note that the strings are concatenated and the double-quotes are there solely to tell batch "here be a string that may contain spaces."
If this works, and there's no reason why it wouldn't (does for me...) then all you'd need to do is enter the client details and the template would be copied to a new directory. Now actually playing around with the data in the files that are copied so that they are customised - well, at the price, that would be worthy of another question...
#echo off
echo Backing up file
set /P source=Enter source folder:
set /P destination=Enter Destination folder:
set /P Folder name=Enter Folder name
set xcopy=xcopy // Set the switches as per your need
%xcopy% %source% %destination%
pause
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.