简体   繁体   中英

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

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