0

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?

user396581
  • 119
  • 1
  • 2
  • 10

1 Answers1

1

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
Randy Rakestraw
  • 331
  • 1
  • 10
  • Thanks for the reply, but the code depends on my using the same usb drive and everyone else also using the same named drive, which will never be the case, the batch file is a backup software so all we do is insert any card and press back up, the script then sees a card and moves the file to it, simple as that, only one card will be plugged in so it only goes to the card that is in, but if a keyboard is installed then the drive letter will not always be F, hence the need to use (F G H I), the above script I am using is perfect for Windows 7 but I need it for Windows XP as it doesn't work – user396581 Feb 21 '15 at 06:04
  • I see, well you could the same process however use different values for the Find command. First if you know the usb drive size you could use the Find "DriveSize in bytes". Another way would be if there is only one usb drive plugged into the machine then you could let the find search for 2 which is the drive type. 2 means removable drive. The drive type numbers are 0 = Unknown 1 = No Root Directory 2 = Removable Disk 3 = Local Disk 4 = Network Drive 5 = Compact Disc 6 = RAM Disk – Randy Rakestraw Feb 22 '15 at 15:06