通过键盘输入驱动器进行备份

时间:2016-11-23 00:23:26

标签: input cmd keyboard backup scanf

我要做的是通过cmd命令进行备份,但问题是当我将USB备份带到另一台PC备份时,驱动器名称不同。

例如,当我这样做时:

XCOPY G:\*.BMP  X:\ /h/i/c/k/e/y/r/d 

在另一台计算机中,驱动器不会是GX

我想要做的是,是否可以创建一个程序,我可以通过键盘输入我要备份的驱动器和驱动器。

例如:

XCOPY "driver name keyboard input":\*.BMP  "driver name keyboard input":/" /h/i/c/k/e/y/r/d 

1 个答案:

答案 0 :(得分:0)

是的,可以使用 PowerShell 批处理文件脚本完成(cmd标记似乎暗示Windows操作系统)。

让我们选择后者。下一个batch-file代码段与相关命令的作用相同:XCOPY G:*.BMP X:\ /h/i/c/k/e/y/r/d

set "DriveIn=G"
set "DriveOu=X"
XCOPY %DriveIn%:*.BMP %DriveOu%:\ /h/i/c/k/e/y/r/d

您可以prompt for user input代替硬编码的DriveInDriveOu

set /P "DriveIn=please choose SOURCE drive letter "
set /P "DriveOu=please choose TARGET drive letter "
XCOPY %DriveIn%:*.BMP %DriveOu%:\ /h/i/c/k/e/y/r/d

必要!)有效性检查的提示:

:dfromscratch
set "DriveIn="
set "DriveOu="
:dsource
set /P "DriveIn=please choose SOURCE drive letter "
    rem basic validity check
if not defined DriveIn goto :dsource
if not exist "%DriveIn%:\" goto :dsource
:dtarget
set /P "DriveOu=please choose TARGET drive letter "
    rem basic validity check
if not defined DriveOu goto :dtarget
if not exist "%DriveOu%:\" goto :dtarget
if /I "%DriveIn%"=="%DriveOu%" goto :dfromscratch
    rem involve more validity check here!!!
XCOPY %DriveIn%:*.BMP %DriveOu%:\ /h/i/c/k/e/y/r/d

更多)有效性检查的一些提示。

显示可用的磁盘驱动器:

wmic logicaldisk get Description, DeviceID, DriveType, FileSystem, VolumeName

要获取可用磁盘驱动器列表,请使用for /F loop(在批处理脚本中注意%%G):

for /F %%G in ('
    wmic logicaldisk where "DriveType != 5" get DeviceID^, DriveType^|find ":"
    ') do echo %%G

或下一个oneliner(注意%G中的cmd):复制&amp; 粘贴到一个打开的cmd窗口中:< / p>

for /F %G in ('wmic logicaldisk where "DriveType != 5" get DeviceID^, DriveType^|find ":"') do @echo %G

另一种方法(只需草稿,需要更详细说明):建立可用驱动器号%drives%列表并使用choice command代替set /P

set "drives=GX"
choice /C %drives% /M "Select SOURCE drive letter"