Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Kamrul08
- Posts: 6
- Joined: 27 May 2016 20:59
- Location: Bangladesh
-
Contact:
#1
Post
by Kamrul08 » 03 Jun 2016 08:18
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.
-
Kamrul08
- Posts: 6
- Joined: 27 May 2016 20:59
- Location: Bangladesh
-
Contact:
#3
Post
by Kamrul08 » 03 Jun 2016 09:57
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
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 03 Jun 2016 10:13
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?
-
Kamrul08
- Posts: 6
- Joined: 27 May 2016 20:59
- Location: Bangladesh
-
Contact:
#5
Post
by Kamrul08 » 04 Jun 2016 00:10
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.
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#6
Post
by penpen » 04 Jun 2016 02:49
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
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#7
Post
by Compo » 04 Jun 2016 06:44
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
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#8
Post
by Squashman » 04 Jun 2016 20:01
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?
-
Kamrul08
- Posts: 6
- Joined: 27 May 2016 20:59
- Location: Bangladesh
-
Contact:
#9
Post
by Kamrul08 » 05 Jun 2016 10:09
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?
-
Kamrul08
- Posts: 6
- Joined: 27 May 2016 20:59
- Location: Bangladesh
-
Contact:
#10
Post
by Kamrul08 » 05 Jun 2016 10:14
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.
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#11
Post
by Compo » 05 Jun 2016 10:53
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.
-
Kamrul08
- Posts: 6
- Joined: 27 May 2016 20:59
- Location: Bangladesh
-
Contact:
#12
Post
by Kamrul08 » 07 Jun 2016 01:54
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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#13
Post
by foxidrive » 07 Jun 2016 17:11
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
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#14
Post
by Compo » 08 Jun 2016 01:55
Just bear in mind that whilst a USB device is removable, all removable drives are not USB, my solution identifies only USB devices!
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#15
Post
by foxidrive » 08 Jun 2016 08:23
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.