Page 1 of 1

Generic USB Drive Letter

Posted: 06 Mar 2023 01:41
by xiro
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 :)

Re: Generic USB Drive Letter

Posted: 06 Mar 2023 15:02
by Compo
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]

Re: Generic USB Drive Letter

Posted: 06 Mar 2023 18:39
by xiro
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 :)


Re: Generic USB Drive Letter

Posted: 07 Mar 2023 11:37
by Compo
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.

Re: Generic USB Drive Letter

Posted: 07 Mar 2023 18:11
by xiro
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 :)

Re: Generic USB Drive Letter

Posted: 09 Mar 2023 04:11
by bxx
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)))

Re: Generic USB Drive Letter

Posted: 09 Mar 2023 09:10
by atfon
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.)

Re: Generic USB Drive Letter

Posted: 09 Mar 2023 18:51
by xiro
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...............

Re: Generic USB Drive Letter

Posted: 10 Mar 2023 01:26
by xiro
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 :)

Re: Generic USB Drive Letter

Posted: 12 Mar 2023 19:02
by ShadowThief
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.

Re: Generic USB Drive Letter

Posted: 12 Mar 2023 20:13
by xiro
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