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:\
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%
)
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
We use batch files to back up files or folders to external Media (such as a flash drive, or Rewritable CD-ROM or DVR disk). I will start with USB flash drive.
We set the desired drive letter in our startup so it's part of the environment: Set ZDRIVE1=F
So, want to be able to check if %ZDRIVE1%: is available, and if not, prompt user to insert the flash drive, retest, them move to the part where we start copying files to the designated flash drive.
We had a utility called dskquery that basically did this, but no one can find the source so we can recompile for 32/64 bit compatibility.
#echo off
Set "ZDRIVE1=F"
:test
pushd %ZDRIVE1%:
if "%errorlevel%" == "0" (
echo the drive is aviable.
pause
goto :eof
) else (
echo please insert the flash drive.
pause
)
goto test
replace goto :eof on line 8 with goto yourlabel
also the drive of a USB flash drive can be G:,H: and so on, not necessary F: ...
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
I have a back up batch file that will backup my files and create a zip file out of them, then move this zip file to a USB drive that will be plugged into the PC, the USB drive could be allocated a drive letter between E to H, dependant on what else is using the ports, the below code works on a Windows 7 PC but not on a Windows XP machine, which the Batch File is going to be used on
The USB drive letter changes depending on which machine I'm backing up and /or how many devices are connected and using drive letters, I don't want to get a choice, just move the file to whatever drive is plugged in as there will only ever be one drive plugged in at a time as it is only used for back up purposes
How can I make this work on an XP machine, it will only be used on an XP machine, also can I throw up and error if a USB drive is not found and delete the zip file, as this is all running from a CF card, embedded system, so space is limited to save any files
REM ------ Creation of a date stamp for the ZIP file on the USB and set the File Name Structure------
set DT=%DATE:/=-%
set timestamp=%DT:~7,3%%DT:~4,3%%DT:~10,4%
Set FileStamp=%BackupName%-%timeStamp%
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 %%D in (E F G H) do if exist %%D: (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break
Put on your USB drive in root a file for example with name BackupDrive.txt. The content does not matter, but I suggest to store in the file something like
File to identify the USB backup drive. Never delete this file.
Set the hidden file attribute on this file.
In your batch file use:
for %%D in (E F G H) do if exist %%D:\BackupDrive.txt (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
Or most likely better, you give your USB drive a unique label and find the drive by label, see
How to reference a volume/drive by label?
How to refer to/select a drive based only on its label (i.e. not the drive letter)?