Simplify 3 lines of Code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Simplify 3 lines of Code

#1 Post by booga73 » 08 Jan 2014 21:35

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:


brianadams
Posts: 5
Joined: 14 Dec 2013 10:12

Re: Simplify 3 lines of Code

#2 Post by brianadams » 08 Jan 2014 21:46

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.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Simplify 3 lines of Code

#3 Post by booga73 » 08 Jan 2014 21:58

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Simplify 3 lines of Code

#4 Post by dbenham » 08 Jan 2014 22:46

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Simplify 3 lines of Code

#5 Post by Aacini » 08 Jan 2014 22:51

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simplify 3 lines of Code

#6 Post by foxidrive » 09 Jan 2014 03:11

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Simplify 3 lines of Code

#7 Post by Endoro » 09 Jan 2014 04:45

..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

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Simplify 3 lines of Code

#8 Post by booga73 » 09 Jan 2014 09:58

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:\>


Post Reply