Batch Scripting Backing up files in USB

你说的曾经没有我的故事 提交于 2020-02-23 04:43:08

问题


Iv got my code to show a list of USb devices connected to my laptop and i can select the USB as well but when i try to back up a folder into the USB what i get instead is a folder named f in the folder i was trying to save. This is the code i have for selectong a USB device

for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a"

:USB  
echo Avaiable Drive ==^> !$List!
set /p "$Drive=Enter the letter of the Backup drive : "

echo !$List! | find /i "%$Drive::=%:" && Echo drive OK || Echo drive do not exist && goto:USB

set backupcmd=robocopy

and this is the code that backup the folder

%backupcmd% "%cd%" "%$Drive%"

回答1:


Give this one a go:

@echo off
setlocal enabledelayedexpansion
set num=0
for /f  %%a in ('wmic logicaldisk where drivetype^=2 get drivetype^, deviceid ^| find ":"') do (
    set /a num+=1
    set cnt=!cnt!!num!
    echo (!num!^) %%a
    set "d!num!=%%a"
)
choice /c !cnt! /M "select a number to choose a drive: "
echo You've chosen drive !d%errorlevel%! you can do everything with it from here..

You will notice that I set delayedexpansion here because of the setting and using of variables inside of the parenthesised code block.

Choice is a good option here as it does not allow typing the wrong values.



来源:https://stackoverflow.com/questions/58917483/batch-scripting-backing-up-files-in-usb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!