简体   繁体   中英

Loop through drive letters and move file to USB

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM