简体   繁体   English

遍历驱动器号并将文件移动到USB

[英]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 我有一个备份批处理文件,该文件将备份我的文件并从中创建一个zip文件,然后将该zip文件移动到将插入到PC的USB驱动器中,可以在E到H,根据其他使用端口的情况,以下代码可在Windows 7 PC上运行,但不适用于Windows XP计算机,而批处理文件将在Windows XP计算机上使用

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 USB驱动器号会根据我要备份的计算机和/或连接的设备数量以及使用的驱动器号而变化,我不想选择,只需将文件移动到插入的任何驱动器中即可一次只能插入一个驱动器,因为它仅用于备份

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 我如何才能在XP机器上执行此操作,它只能在XP机器上使用,如果找不到USB驱动器,我也可以抛出错误并删除zip文件,因为这都是从CF卡运行的嵌入式系统,因此空间有限,无法保存任何文件

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 . 将您的USB驱动器放在根目录下,例如一个名为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 或者,可能更好的做法是,为USB驱动器赋予唯一的标签,然后按标签查找驱动器,请参见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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