Xcopy to multiple external drives using drive label - batch-file

I need to copy a number of files to multiple USB drives.
The USB drive letters are not static, so I need to create a script that will run the xcopy command on all USB drives with a label of "USB DISK".
I've found command lines that will do this for a single drive, but not multiple drives.
The command "for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "My Label"') do set myDrive=%%D" works, but only on the last drive.
Is it possible to copy the files to all the drives at the same time, rather than one after another?

for /f %%D in (
'wmic volume get DriveLetter^, Label ^| find "My Label"'
) do start "Copying to %%D" xcopy "sourcespec" %%D:
should parallel xcopy your source files to each selected drive. xcopy options and specifications are in your court

Related

Drive name in batch

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%
)

Batch Script - Detect Drive letter and Execute a File Transfer

I am trying to create a simple batch script to test the Data transfer between two Thumb Drives connected to a Test PC. This is how the test must proceed:
Detect if the USB Drives are connected to the PC.
If connected, detect the drive letters and assign them as Drive 1 and Drive 2
Test the Data Transfer by moving a file from the PC's Hard Drive to Drive 1. Move the same file from Drive 1 to Drive 2. Move the same file from Drive 2 back to the PC.
Repeat the same test by moving a file from the PC's Hard Drive to Drive 2. Move the same file from Drive 2 to Drive 1. Move the same file from Drive 1 back to the PC.
So far have managed to create the script (Script1) to detect the drive letters. I also have the script (Script2) to transfer the file as described in Points 3 and 4 (But here, the drive letters are fixed). However, I am not able to use the detected drive letters to transfer my file.
I am looking for a way to combine Script1 and Script2. Is there a way to do this? Is there a way to use the detected drive letters and execute the data transfer between the two?
It would be great if you could suggest an alternative solution as well.
Script1 to detect the drive:
#echo off
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
if %%d equ Removable (
echo Drive %%c is Removable (USB^)
)
)
)
)
Script2 is a simple script to move a file (fixed name) from one drive to the other:
#echo off
Echo Moving File from PC to USBDrive1
move E:\Test1.txt N:\
Echo Moving File from USBDrive1 to USBDrive2
move N:\Test1.txt D:\
Echo Moving File from USBDrive2 to PC
move D:\Test1.txt E:\

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)

Move file to a changeable drive location if fitted "for a in range" in XP

How to move a file to a USB drive that has a variable drive letter range dependant on the machine it could be drive E, F, G or H, in Windows Embedded XP, there is only ever one USB drive installed at a time, so it can move only if it is fitted, I can create the file and it moves in Windows 7 but not in Windows Embedded XP, what are the differences in the options available for this in XP, the script will only be used on XP machines.
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Move the backup file to a USB drive with File Name and Date Stamp ------
for %%A in (E F G H) do if exist %%A: (
echo Moving files to USB drive %%A:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%A: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break
Can I also create an error message if the file is not moved and then delete the file, as it takes up valuable space on the drive?
Here is a solution I use. There is a requirement that the USB drive has been named and you know it. So lets say your USB is named "8GB"
If you run the following command:
wmic logicaldisk list brief
You get a list of your drives including the VolumeName.
Using this list you can pipe it to the Find command like so:
wmic logicaldisk list brief | find "8GB"
Which will return all information about your drive with the VolumeName 8GB. It will look something like this.
C:\>wmic LOGICALDISK LIST BRIEF | FIND "8GB"
F: 2 3080192 8082407424 8GB
Now with this command we can take it further and redirect its output to a file. Like so.
wmic logicaldisk list brief | find "8GB" > C:\tmp\usbdriveinfo.txt
After the information we want has been stored we can read it back into a variable using:
set /p driveLetter=C:\tmp\usbdriveinfo.txt
Now that variable has the whole string but we only want the drive letter so we shorten it like so:
set driveLetter=%driveLetter:~-,2%
Now the variable driveLetter contains just your drive letter "F:"
So if you want it all together:
wmic logicaldisk list brief | find "8GB" > C:\tmp\usbdriveinfo.txt
set /p driveLetter=C:\tmp\usbdriveinfo.txt
set driveLetter=%driveLetter:~-,2%
As for checking if the move command fails. If any command fails move included they set the variable errorlevel to some value other than 0 (0 is for successful execution) Therefore all you need to do is add an if statement after the move command like:
if %errorlevel% GTR 0 del %BackupPath%\Backup\%FileStamp%.zip

Running batch file from USB drive when drive letter changes

So, I have made a batch script and it executes multiple portable programs (e.g., prog1.exe, prog2.exe, etc). The problem is whenever I connect the USB drive to another computer, the drive letters change, giving me errors when running my .bat file. Please help me find a solution. Thank you.
%~d0 gives you the current drive letter (including the colon). If the batch file's contained on the USB drive, you can use that.
So, for instance, instead of
E:\PortablePrograms\ProgramName.exe
you would write
%~d0\PortablePrograms\ProgramName.exe
... or you could do something like this
::change directory to the script's directory's drive
pushd %~d0
::navigate from the drive to the relevant path(s)
cd PortablePrograms
::execute any programs
ProgramName.exe
SecondProgramName.exe
::just because I like to pair my pushes with pops; not required
popd
This is how I get the last removable drive in a listing.
#echo off
:: Drivetypes
:: 0=Unknown
:: 1=No Root Directory
:: 2=Removable(USB,Firewire)
:: 3=Local Disk (Internal Hard Drive)
:: 4=Network Drive(\\Server\share\)
:: 5=Compact Disk (CD DVD)
:: 6=Ram Disk
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=2"
get name /format:value') do set driveletter= %%d
echo %driveletter%
pause
you can use command line argument %1, %2 as input path, and modify your bat file accordingly.

Resources