Page 1 of 2
How to be sure selected drive is removable or not?
Posted: 03 Jun 2016 08:18
by Kamrul08
I want to copy some files to my USB Drive. For this I made the script bellow. It’s working fine.
@echo off
echo.
echo.
set source="%userprofile%\Downloads"
set /p Letter= Enter your USB Drive Letter here:
rem echo n| copy /-y %source%\*.pdf %Letter%:\
echo n | copy /-y %source%\*.pdf %Letter%:\
pause
exit
I want to add one more script to be sure my selected drive is removable or not. If selected drive is removable file/files will be copied. But if selected drive isn’t removable the script will show “
Drive “x” isn’t removable drive” massage.
Re: How to be sure selected drive is removable or not?
Posted: 03 Jun 2016 08:56
by Squashman
Look at this thread.
viewtopic.php?t=3455
Re: How to be sure selected drive is removable or not?
Posted: 03 Jun 2016 09:57
by Kamrul08
Thanks for reply. But this topics doesn't match my requirement. I can find out removable drive. But I want to know weather my selected drive is removable or not.
The command bellow works fine to shows removable drive.
wmic logicaldisk where drivetype=2 get deviceid, description, volumename
Re: How to be sure selected drive is removable or not?
Posted: 03 Jun 2016 10:13
by Squashman
Kamrul08 wrote:Thanks for reply. But this topics doesn't match my requirement. I can find out removable drive. But I want to know weather my selected drive is removable or not.
The command bellow works fine to shows removable drive.
wmic logicaldisk where drivetype=2 get deviceid, description, volumename
What is stopping you from parsing that list to see if your selected drive letter is in that list?
Re: How to be sure selected drive is removable or not?
Posted: 04 Jun 2016 00:10
by Kamrul08
Squashman wrote:What is stopping you from parsing that list to see if your selected drive letter is in that list?
I do this copy job regularly. Although I know the USB drive Letter but sometimes I accidentally press the wrong drive letter. It copies the files to my local drive instead of USB storage and it makes problem. So I need a error massage when I put wrong drive.
Re: How to be sure selected drive is removable or not?
Posted: 04 Jun 2016 02:49
by penpen
Squasman don't want you to do this manually!
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
set source="%userprofile%\Downloads"
set /p Letter= Enter your USB Drive Letter here:
:: parse the list, setting found to 1 if your drive is removable
set "found="
for /F %%a in ('WMIC LOGICALDISK Where "DriveType='2'" get DeviceID /Format:texttable.xsl') do if /I "%Letter%:" == "%%~a" set "found=1"
echo(
echo(
if not defined found ( echo Drive "%Letter%" isn't removable drive
) else echo n | copy /-y "%source%\*.pdf" "%Letter%:\"
pause
endlocal
Sidenote:
If you store your batch on your usb-drive, and double-click it, then you probably could simplify your script to:
Code: Select all
@echo off
echo(
echo(
echo n | copy /-y "%source%\*.pdf" "%~d0\"
pause
penpen
Re: How to be sure selected drive is removable or not?
Posted: 04 Jun 2016 06:44
by Compo
If you're on Windows 7 or newer, just use this script instead!
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "Source=%UserProfile%\Downloads"
For /F "Skip=2 Tokens=*" %%A In ('WMIc DiskDrive Where InterfaceType^="USB"^
Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
For /F Delims^=^":^ Tokens^=6 %%C In (
'WMIc Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
Set "_C=%%C!_C!")))
If Not Defined _C Echo(You don't have a USB drive connected && GoTo :EndIt
Echo( Enter your USB drive letter here:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
If Not Defined Source Echo(You entered drive letter "%Letter%" && GoTo :EndIt
If Not Exist "%Source%\" Echo(Invalid Source Directory && GoTo :EndIt
RoboCopy "%Source%" %Letter% *.pdf /XC /XO /XN /XJF
:EndIt
Timeout -1
Re: How to be sure selected drive is removable or not?
Posted: 04 Jun 2016 20:01
by Squashman
Kamrul08 wrote:Squashman wrote:What is stopping you from parsing that list to see if your selected drive letter is in that list?
I do this copy job regularly. Although I know the USB drive Letter but sometimes I accidentally press the wrong drive letter. It copies the files to my local drive instead of USB storage and it makes problem. So I need a error massage when I put wrong drive.
So what you are saying is you wanted the fish but did not want to learn to fish?
Re: How to be sure selected drive is removable or not?
Posted: 05 Jun 2016 10:09
by Kamrul08
Compo wrote:If you're on Windows 7 or newer, just use this script instead!
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "Source=%UserProfile%\Downloads"
For /F "Skip=2 Tokens=*" %%A In ('WMIc DiskDrive Where InterfaceType^="USB"^
Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
For /F Delims^=^":^ Tokens^=6 %%C In (
'WMIc Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
Set "_C=%%C!_C!")))
If Not Defined _C Echo(You don't have a USB drive connected && GoTo :EndIt
Echo( Enter your USB drive letter here:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
If Not Defined Source Echo(You entered drive letter "%Letter%" && GoTo :EndIt
If Not Exist "%Source%\" Echo(Invalid Source Directory && GoTo :EndIt
RoboCopy "%Source%" %Letter% *.pdf /XC /XO /XN /XJF
:EndIt
Timeout -1
Just awesome. Thanks a lot. No way for all version of windows?
Re: How to be sure selected drive is removable or not?
Posted: 05 Jun 2016 10:14
by Kamrul08
penpen wrote: Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
set source="%userprofile%\Downloads"
set /p Letter= Enter your USB Drive Letter here:
:: parse the list, setting found to 1 if your drive is removable
set "found="
for /F %%a in ('WMIC LOGICALDISK Where "DriveType='2'" get DeviceID /Format:texttable.xsl') do if /I "%Letter%:" == "%%~a" set "found=1"
echo(
echo(
if not defined found ( echo Drive "%Letter%" isn't removable drive
) else echo n | copy /-y "%source%\*.pdf" "%Letter%:\"
pause
endlocal
I exactly wanted this . Thanks a lot.
Re: How to be sure selected drive is removable or not?
Posted: 05 Jun 2016 10:53
by Compo
Kamrul08 wrote:Just awesome. Thanks a lot. No way for all version of windows?
To be honest, I couldn't care less about creating solutions for 9 year plus old operating systems.
In this case you'd need to replace the use of choice for XP and Vista. I also believe that some versions of 2000 had an error which allowed them to use poison characters in the computer name, so the use delayed expansion or even just the output of some of the WMIc commands may be problematic.
Re: How to be sure selected drive is removable or not?
Posted: 07 Jun 2016 01:54
by Kamrul08
Compo wrote:Kamrul08 wrote:Just awesome. Thanks a lot. No way for all version of windows?
To be honest, I couldn't care less about creating solutions for 9 year plus old operating systems.
In this case you'd need to replace the use of choice for XP and Vista. I also believe that some versions of 2000 had an error which allowed them to use poison characters in the computer name, so the use delayed expansion or even just the output of some of the WMIc commands may be problematic.
Thanks.
Re: How to be sure selected drive is removable or not?
Posted: 07 Jun 2016 17:11
by foxidrive
This should work. The code is from the link that Squashman gave you.
Code: Select all
@echo off
echo.
echo.
set source="%userprofile%\Downloads"
set /p Letter= Enter your USB Drive Letter here:
WMIC LOGICALDISK Where "DriveType='2'" get DeviceID /Format:list|find /i "%Letter%:" > nul ||(
echo Drive "%Letter%" isn't a removable drive, now go away or I shall taunt you second time!
pause
goto :EOF
)
echo n | copy /-y %source%\*.pdf %Letter%:\
pause
exit
Re: How to be sure selected drive is removable or not?
Posted: 08 Jun 2016 01:55
by Compo
Just bear in mind that whilst a USB device is removable, all removable drives are not USB, my solution identifies only USB devices!
Re: How to be sure selected drive is removable or not?
Posted: 08 Jun 2016 08:23
by foxidrive
Good point. What types of drives are in the other category? My google-fu finger is asleep here - refuses to search.
I followed the fellow's use of WMIC in his own line of code when I went browsing through Squashman's suggested thread.