Generic USB Drive Letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xiro
Posts: 34
Joined: 21 May 2014 00:17

Generic USB Drive Letter

#1 Post by xiro » 06 Mar 2023 01:41

Hi anyone tried not to assign any specific drive letter to your USB using a script

sample code I am making


robocopy "%%\CTI\*.pptx" "%AppData%\Microsoft\Signatures"

start %APPDATA%\Microsoft\Signatures

::start outlook.exe

pause

I want on the first line after the command robocopy a code or keyword for a generic USB drive letter assignment is that possible? Because it is not always drive E: and etc its random depending upon the computer you are connected to right :)

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Generic USB Drive Letter

#2 Post by Compo » 06 Mar 2023 15:02

First things first…your RoboCopy command is incorrect. Please open a Command Prompt window, type %SystemRoot%\System32\Robocopy.exe /? and press the [ENTER] key to find out its basic syntax.

Code: Select all

ROBOCOPY source destination [file]

xiro
Posts: 34
Joined: 21 May 2014 00:17

Re: Generic USB Drive Letter

#3 Post by xiro » 06 Mar 2023 18:39

Compo wrote:
06 Mar 2023 15:02
First things first…your RoboCopy command is incorrect. Please open a Command Prompt window, type %SystemRoot%\System32\Robocopy.exe /? and press the [ENTER] key to find out its basic syntax.

Code: Select all

ROBOCOPY source destination [file]
robocopy "E:\CTI\" "%AppData%\Microsoft\Signatures"

Here I have modified it for clarity :) I was just wondering how to make E: as generic not all PC say your USB is E: right how to make it detected by the computer as any drive letter that is available but still could also deploy the script?

I have tried %% and a lot of combinations do not work or I still just don't get the right combination any assistance will be much appreciated :)


Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Generic USB Drive Letter

#4 Post by Compo » 07 Mar 2023 11:37

I know what you're trying to do, but until you have the basic code correct, I'm not going to add more to it.

You have now submitted a modified RoboCopy command line, which is still invalid/incorrect.

The source and destination should not end with trailing backward slashes. In your case above you need to use "E:\CTI", because the trailing backward slash in "E:\CTI\" is escaping the adjacent doublequote.

xiro
Posts: 34
Joined: 21 May 2014 00:17

Re: Generic USB Drive Letter

#5 Post by xiro » 07 Mar 2023 18:11

Compo wrote:
07 Mar 2023 11:37
I know what you're trying to do, but until you have the basic code correct, I'm not going to add more to it.

You have now submitted a modified RoboCopy command line, which is still invalid/incorrect.

The source and destination should not end with trailing backward slashes. In your case above you need to use "E:\CTI", because the trailing backward slash in "E:\CTI\" is escaping the adjacent doublequote.
robocopy "E:\CTI" "%AppData%\Microsoft\Signatures" here the corrected command :)

Cool you also give tips and explanations of how things are done. Now I have made the code corrected as you can now see thanks to your info.

Please what is lacking from the script for the USB generic drive letter part :)

bxx
Posts: 5
Joined: 09 Mar 2023 03:49

Re: Generic USB Drive Letter

#6 Post by bxx » 09 Mar 2023 04:11

You can just find what drive letter matches the USB.

Code: Select all

for %u in (D E F G H I J K L) do (for /F "tokens=3" %a in ('fsutil fsinfo drivetype %u:') do (if "%a"=="Removable" (set _USBDRIVELETTER=%u&echo %u is an USB)))

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Generic USB Drive Letter

#7 Post by atfon » 09 Mar 2023 09:10

If your system has access to WMIC, you can define the USB drive letter as a variable rather easily like this:

Code: Select all

for /f "skip=2 tokens=2 delims=," %%g in ('%__APPDIR__%wbem\WMIC.exe logicaldisk where "drivetype=2" get DeviceID 2^>nul /format:csv') do set "usbDrive=%%g"
Subsequently, you can use 'if defined usbDrive' to perform some action on that drive, like:

Code: Select all

if defined usbDrive (cd /d %usbDrive%) else (echo There is no attached USB Drive.)

xiro
Posts: 34
Joined: 21 May 2014 00:17

Re: Generic USB Drive Letter

#8 Post by xiro » 09 Mar 2023 18:51

atfon wrote:
09 Mar 2023 09:10
If your system has access to WMIC, you can define the USB drive letter as a variable rather easily like this:

Code: Select all

for /f "skip=2 tokens=2 delims=," %%g in ('%__APPDIR__%wbem\WMIC.exe logicaldisk where "drivetype=2" get DeviceID 2^>nul /format:csv') do set "usbDrive=%%g"
Subsequently, you can use 'if defined usbDrive' to perform some action on that drive, like:

Code: Select all

if defined usbDrive (cd /d %usbDrive%) else (echo There is no attached USB Drive.)

I will try and play with the code thanks...............

xiro
Posts: 34
Joined: 21 May 2014 00:17

Re: Generic USB Drive Letter

#9 Post by xiro » 10 Mar 2023 01:26

atfon wrote:
09 Mar 2023 09:10
If your system has access to WMIC, you can define the USB drive letter as a variable rather easily like this:

Code: Select all

for /f "skip=2 tokens=2 delims=," %%g in ('%__APPDIR__%wbem\WMIC.exe logicaldisk where "drivetype=2" get DeviceID 2^>nul /format:csv') do set "usbDrive=%%g"
Subsequently, you can use 'if defined usbDrive' to perform some action on that drive, like:

Code: Select all

if defined usbDrive (cd /d %usbDrive%) else (echo There is no attached USB Drive.)
Thanks for this I used the first one the second what do you mean by define USB drive thing? But I have already make a walk around for it thank you so much pal :)

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Generic USB Drive Letter

#10 Post by ShadowThief » 12 Mar 2023 19:02

xiro wrote:
10 Mar 2023 01:26
atfon wrote:
09 Mar 2023 09:10
If your system has access to WMIC, you can define the USB drive letter as a variable rather easily like this:

Code: Select all

for /f "skip=2 tokens=2 delims=," %%g in ('%__APPDIR__%wbem\WMIC.exe logicaldisk where "drivetype=2" get DeviceID 2^>nul /format:csv') do set "usbDrive=%%g"
Subsequently, you can use 'if defined usbDrive' to perform some action on that drive, like:

Code: Select all

if defined usbDrive (cd /d %usbDrive%) else (echo There is no attached USB Drive.)
Thanks for this I used the first one the second what do you mean by define USB drive thing? But I have already make a walk around for it thank you so much pal :)
if defined usbDrive means that there is a variable called usbDrive that has some value. It's effectively the same code as if not "%usbDrive%"=="" but with less typing.

xiro
Posts: 34
Joined: 21 May 2014 00:17

Re: Generic USB Drive Letter

#11 Post by xiro » 12 Mar 2023 20:13

ShadowThief wrote:
12 Mar 2023 19:02
xiro wrote:
10 Mar 2023 01:26
atfon wrote:
09 Mar 2023 09:10
If your system has access to WMIC, you can define the USB drive letter as a variable rather easily like this:

Code: Select all

for /f "skip=2 tokens=2 delims=," %%g in ('%__APPDIR__%wbem\WMIC.exe logicaldisk where "drivetype=2" get DeviceID 2^>nul /format:csv') do set "usbDrive=%%g"
Subsequently, you can use 'if defined usbDrive' to perform some action on that drive, like:

Code: Select all

if defined usbDrive (cd /d %usbDrive%) else (echo There is no attached USB Drive.)
Thanks for this I used the first one the second what do you mean by define USB drive thing? But I have already make a walk around for it thank you so much pal :)
if defined usbDrive means that there is a variable called usbDrive that has some value. It's effectively the same code as if not "%usbDrive%"=="" but with less typing.
Thanks for this

Post Reply