Page 1 of 1

Simplify 3 lines of Code

Posted: 08 Jan 2014 21:35
by booga73
I've got this script that finds the drive letter which my external USB is connected to based upon the drive label, Seagate.

somehow wmic creates a unicode text file which can't be read as easy as a straight text file, so that's why I have 3 lines, 2 of which write to a file in the temp directory.

Is there a better way, short and simple to find the drive letter of my external drive based upon the drive label, Seagate, without writing a temp file to disk?

the drive letter needs to be found and set to variable based about the drive label, Seagate
Scenario, When going portable, the drive once connected to my other workstation across town will pick up the external drive but set it to a different drive letter.
This is for automating backups.

thank you for your consideration and support,

v/r Booga73

Code: Select all


wmic logicaldisk get Name,VolumeName /Format:csv > %tmp%\TMP.txt
more +2 %tmp%\tmp.txt > %tmp%\tmp2.txt
for /f "tokens=2 delims=," %c in ('findstr Seagate %tmp%\tmp2.txt') do @echo %c




tmp.txt shows the following data:

Code: Select all

Node,Name,VolumeName
BatchNoob,C:,
BatchNoob,D:,Archive Drive
BatchNoob,E:,Win7x64Rec
BatchNoob,F:,iRT Drive
BatchNoob,G:,WSTDGT Drive
BatchNoob,I:,Seagate
BatchNoob,J:,irtDiag
BatchNoob,K:,
BatchNoob,L:,



tmp2.txt shows the following data:
BatchNoob,C:,
BatchNoob,D:,Archive Drive
BatchNoob,E:,Win7x64Rec
BatchNoob,F:,iRT Drive
BatchNoob,G:,WSTDGT Drive
BatchNoob,I:,Seagate
BatchNoob,J:,irtDiag
BatchNoob,K:,
BatchNoob,L:,


My code output is showing:

I:


Re: Simplify 3 lines of Code

Posted: 08 Jan 2014 21:46
by brianadams
what happens when you just do this

Code: Select all

wmic logicaldisk get Name,VolumeName /Format:csv | findstr /I seagate 

type the above on command line and see what you get.

Re: Simplify 3 lines of Code

Posted: 08 Jan 2014 21:58
by booga73
getting close .. . . I tried to modify a little and go on your lead brianadams .. . .

wmic logicaldisk get Name,VolumeName /Format:csv | findstr /I seagate

BatchNoob,I:,Seagate

but if I want to simply get the drive letter, I:, and set I: to a variable, I tried the following at the command line:

for /f "tokens=2 delims=," %d in ('wmic logicaldisk get Name,VolumeName /Format:csv ^| findstr /I seagate') do @echo %d

was thinking that line of code, tokens=2, the final output would be I:

but I got this output:

Invalid GET Expression.

Re: Simplify 3 lines of Code

Posted: 08 Jan 2014 22:46
by dbenham

Code: Select all

for /f "tokens=2 delims=," %A in ('wmic logicaldisk get Name^,VolumeName /Format:csv^|findstr Seagate') do @echo %A
or

Code: Select all

for /f "tokens=2 delims=," %A in ('"wmic logicaldisk get Name,VolumeName /Format:csv|findstr Seagate"') do @echo %A

You could make it very precise (only matching exact string of "Seagate") using:

Code: Select all

for /f "tokens=2 delims=," %A in ('wmic logicaldisk get Name^,VolumeName /Format:csv^|findstr ",Seagate$"') do @echo %A


Dave Benham

Re: Simplify 3 lines of Code

Posted: 08 Jan 2014 22:51
by Aacini
This is untested:
EDIT: I tested it, and it works! (after fixed a couple details)

Code: Select all

for %a in (A B C D E F G H I J L M N O P Q) do @for /F "tokens=6,8" %b in ('dir %a:_ 2^>NUL') do @if "%c" equ "Seagate" echo %b

Perhaps you have to adjust the 6,8 numbers to match the drive and label tokens of your DIR command. This example is based on my Spanish version:

Code: Select all

>dir d:_
 El volumen de la unidad D es RECOVERY


Antonio

Re: Simplify 3 lines of Code

Posted: 09 Jan 2014 03:11
by foxidrive
Here's another method:

Code: Select all

for %a in (A B C D E F G H I J L M N O P Q) do if exist %a:\ vol %a: |find "Seagate">nul && set drv=%a

Re: Simplify 3 lines of Code

Posted: 09 Jan 2014 04:45
by Endoro
..more code:

Code: Select all

for /f "tokens=1*" %a in ('fsutil fsinfo drives') do @for %c in (%b) do @vol %~dc|find "Seagate">nul&&echo %~dc

Re: Simplify 3 lines of Code

Posted: 09 Jan 2014 09:58
by booga73
The script illustrations is great learning; I'm seeing visually what can be better code; thank you. :D


The examples provided, I got to thinking about how to set a value to a variable without first writing to a file; it's better to set value to a variable instead of writing to disk.

Thank you all for your illustrations and support. v/r Booga73

I also came up with this here, entering code into dos window instead of batch file:

Code: Select all

wmic baseboard get Manufacturer,Product,SerialNumber,Version /Format:csv > %tmp%\readme.txt & start %tmp%\readme.txt

more +2 %tmp%\readme.txt > %tmp%\readme2.txt & start %tmp%\readme2.txt

for /f "tokens=3-8 delims=-,/" %a in (%tmp%\readme2.txt) do CLS & (@echo Computer Name: %ComputerName% & @echo. & @echo Manufacturer: %b & @echo. & @echo MB ID#: %c & @echo. & @echo Service Tag#: %d & @echo. & @echo Serial #: %e & @echo. & @echo Motherboard Version: %f)


output in dos window shows:

Computer Name: BatchNoob

Manufacturer: Dell Inc.

MB ID#: 0J4TFW

Service Tag#: 6YM81R1

Serial #: CN1296118TGJ7V

Motherboard Version: A01

H:\>