多个else来检测驱动器是否存在

时间:2017-09-30 02:52:46

标签: windows batch-file cmd

我制作了一个小型批处理文件,将驱动器的内容复制到外部存储器,但很明显,有许多不同的字母可以分配给外部光盘/驱动器。

基本上我想做类似

的事情
if exist E: (
set "destination=E:"
) else ( 
set "destination=F:"
) else ( 
set "destination=G:"

我想知道的另一件事是你如何检查存储是否超过一定数量或少于一定数量?我问这是因为通常的字母是用于Windows分区的C和D,或用于分区的C和D以及CD / DVD阅读器。

显然,CD / DVD阅读器没有存储空间,因此如果它大于指定的存储空间,它将进入下一个其他位置并继续将%destination%设置为下一个字母。我曾想过检查它是否是USB或CD / DVD,因为你可以在你的内部驱动器上制作E,F,G分区,但我不知道如何。

以下是原始文件的样子,以防您想知道我在做什么:

@echo off
title Backup

REM Here is where I will place the code to check for drives
set "srcd=E:\"

REM Here is my destination, the computers partition for C.
set "destd=C:"

REM It will then be set to this, dir is where I make logs etc, dir2 is where I will copy the files to, log is obviously, a text file.

set "dir=%destd%\BackupUSB"
set "dir2=%destd%\%dir%\Files"
set "log=%dir%\Log.txt"


REM I am now finding if the destination exists as it will be copied to a folder, if it does not exist I will make it..

if exist "%dir2%" (
cd "%dir%"
) else (
md "%dir2%"
)

REM I am now going to create the log file saying the date and time the transfer took place.


for /f "tokens=*" %%a in ('date /t') do (
set d=%%a
)
for /f "tokens=*" %%a in ('time /t') do (
set t=%%a
)

REM Add date and time to show on same line.
set "dt=%d%%t%"

REM I now write the text to the file.
echo ==================>>"%log%"
echo =%dt%=>>"%log%"
echo ==================>>"%log%"

REM if the source exists copy the files, if not then save a message to a text file saying it failed.

if exist "%srcd%" (
cd %srcd%
REM /Q copies silently.

echo please wait while your files are copied from %srcd% to %dir2%
xcopy "%srcd%" /E /I /Q /H "%dir2%" /y
echo Copied "%srcd%" to "%dir2%" [%dt%]>>"%log%"
) else (
echo Failed to copy files from drive %srcd% to "%dir2%" please check the drive/path exists and run the batch file again [%dt%]>>"%log%"
)

0 个答案:

没有答案