Jump to content











Photo
- - - - -

Tweaking Script required


  • Please log in to reply
39 replies to this topic

#1 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 24 March 2011 - 02:03 PM

Hi
I have a script that steve rmprep kindly helped me write that searches for the VOL name of a USB ready to copy data.
It works a treat however I got burned real bad today running it from a mapped drive!

running from C: works fine

Running it from network share mapping it sees the mapped drive letter N:\ for example as the USB. And guess what I had robocopy MIR set up and it wiped the whole lot. OOOOPS. Never mind booked a restore from the serve guys.

Please could some one take a quick look and why it does this? thanks alot.

SET VOL="USB"
echo Finding disk which has Volume Label of %VOL%
SET LETTER=
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
dir %%a: > tmp.txt
find /I %VOL% tmp.txt 1> nul
if NOT errorlevel 1 SET LETTER=%%a
)
@echo DRIVE LETTER IS %LETTER%
del tmp.txt

#2 amalux

amalux

    Platinum Member

  • Tutorial Writer
  • 2,813 posts
  •  
    United States

Posted 24 March 2011 - 02:42 PM

Try this:

@echo off & setLocal EnableDelayedExpansion



for %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do (

for /f "tokens=6 delims= " %%i in ('vol %%a: ^|find "USB"') do (

echo %%i | find "USB" > nul

if not errorlevel 1 call :sub1 %%a:

)

)



goto :eof



:sub1

echo found %1



goto :eof


#3 steve6375

steve6375

    Platinum Member

  • Developer
  • 5,184 posts
  • Location:UK
  • Interests:computers (!), programming (masm,vb6,C,vbs), OSes, photography,TV,films,guitars
  •  
    United Kingdom

Posted 24 March 2011 - 02:52 PM

SET VOL="USB"
echo Finding disk which has Volume Label of %VOL%
SET LETTER=
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
vol %%a: > tmp.txt
find /I %VOL% tmp.txt 1> nul
if NOT errorlevel 1 SET LETTER=%%a
)
@echo DRIVE LETTER IS %LETTER%
del tmp.txt



just replace dir with vol (or use amalux's neater suggestion but add line after sub1: of SET LETTER=%1 )








#4 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 24 March 2011 - 05:45 PM

Hi
I have a script that steve rmprep kindly helped me write that searches for the VOL name of a USB ready to copy data.
It works a treat however I got burned real bad today running it from a mapped drive!

running from C: works fine

Running it from network share mapping it sees the mapped drive letter N:\ for example as the USB. And guess what I had robocopy MIR set up and it wiped the whole lot. OOOOPS. Never mind booked a restore from the serve guys.

Please could some one take a quick look and why it does this? thanks alot.

SET VOL="USB"
echo Finding disk which has Volume Label of %VOL%
SET LETTER=
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
dir %%a: > tmp.txt
find /I %VOL% tmp.txt 1> nul
if NOT errorlevel 1 SET LETTER=%%a
)
@echo DRIVE LETTER IS %LETTER%
del tmp.txt



#5 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 24 March 2011 - 05:49 PM

Hey guys this is great news thanks for the speedy tips I always panic a bit when I get a bit out my depth but always get back on track after checking in with the big guns in this place!

#6 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 24 March 2011 - 06:15 PM

Nearly, trying this right now and getting device not ready, thanks(steve which sub1 should I be pasting under?) @echo off & setLocal EnableDelayedExpansionfor %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do (for /f "tokens=6 delims= " %%i in ('vol %%a: ^|find "UD1BOOT"') do (echo %%i | find "UD1BOOT" > nulif not errorlevel 1 call :sub1 %%a:))goto :eof:sub1SET LETTER=%1pauseecho found %1pausegoto :eof

#7 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 24 March 2011 - 06:24 PM

@echo off & setLocal EnableDelayedExpansion for %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do ( for /f "tokens=6 delims= " %%i in ('vol %%a: ^|find "USB"') do (
echo
%%i | find "USB" > nul
if not errorlevel 1 call :sub1 %%a: ) ) goto :eof

:sub1
SET LETTER=%1

goto :eof


sorry having problems typing - im getting device not ready on amalux code, not sure if im on the right place to add set letter?






#8 steve6375

steve6375

    Platinum Member

  • Developer
  • 5,184 posts
  • Location:UK
  • Interests:computers (!), programming (masm,vb6,C,vbs), OSes, photography,TV,films,guitars
  •  
    United Kingdom

Posted 24 March 2011 - 07:00 PM

works for me - is rest of your code just above :sub1 ????? It should be. If not how are you using this code?

#9 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 24 March 2011 - 07:19 PM

trying to make a script that a: formats usb, b: sets up g4d (PBR) c: finds drive letter of USB via %letter%;\ d: then copies all the files it needs grldr etc etc then finishes script

there is code above and after, I just swapped DIR for VOL and retested - works a treat even from mapped drive!!!!

#10 Sha0

Sha0

    WinVBlock Dev

  • Developer
  • 1,672 posts
  • Location:reboot.pro Forums
  • Interests:Booting
  •  
    Canada

Posted 24 March 2011 - 07:38 PM


@echo off



:: The volume label to find the drive letter for.

:: WARNING!  If this label is a sub-string of a

:: volume label, it will match!

set _vol_label=USB



:: Clear any previous result.

set _the_drive=





:: For each mounted volume with a drive letter...

for /f "delims=\" %%a in ('mountvol ^| findstr :\') do (

    :: Process the drive.

    call :_do_drive %%a

  )



:: Did we find the drive?

if [%_the_drive%]==[] goto :_not_found

echo Drive letter for "%_vol_label%" is "%_the_drive%".



goto :eof





:_do_drive

:: For each line in the result of the VOL command

:: containing the volume label we are looking for...

for /f %%a in ('vol %1 2^>nul ^| findstr %_vol_label%') do (

    :: If we have a match, remember this drive.

    if not [%%a]==[] set _the_drive=%1

  )

goto :eof





:_not_found

echo Volume label not found.

goto :eof



#11 amalux

amalux

    Platinum Member

  • Tutorial Writer
  • 2,813 posts
  •  
    United States

Posted 24 March 2011 - 10:13 PM

Technotika,

Sorry for delayed response, got hung up at work... Glad you got it working!

#12 Zharif

Zharif

    Newbie

  • .script developer
  • 27 posts
  • Location:Germany
  •  
    Germany

Posted 26 March 2011 - 08:14 PM

Just some adds about usage of findstr:

Enable case insensitivity with parmameter "/I"

To match a string that contains "USB" only type:
findstr "^USB$" (line boundary -matches only if line begins with "USB" and ends after it -case sensitive)

To match a string that begins with "USB" type:
findstr "^USB" (line boundary -matches only if line begins with "USB" -would also match if a string begins with "USB1" -case sensitive)

To match a string that ends with "USB" type:
findstr "USB$" (line boundary -matches only if line ends with "USB" -would also match if a string ends with "DRV-USB" -case sensitive)


To match a word "USB" anywhere inside a string type:
findstr "\<USB\>" (word boundary - matches only if substring "USB" exist anywhere inside a string -case sensitive)

To match a word beginning with "USB" anywhere inside a string type:
findstr "\<USB" (word boundary left - matches only if word begins with "USB" inside string -would also match if string contains "DRV USB123" -case sensitive)

To match a word ending with "USB" anywhere inside a string type:
findstr "USB\>" (word boundary right - matches only if word ends with "USB" inside string -would also match if string contains "Boot DRV-USB" -case sensitive)


You see, to get exact results command "find" is a very bad choice.
While using "findstr" it's important to integrate some parameters and availability of regular expressions into your script.
But it's a mess to change parameters by editing the script any time you're using different search strings.
Best would be to enable your script to except findstr-parameters.
Hope this information was useful and helps you to improve your script.

#13 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 27 March 2011 - 05:01 AM

thanks my command of batch is pretty basic and this is more advanced than what I'm normally used to, I'm just trying to get my head round what happens in this script can you tell me, or explain in laymens terms what I get wrong




@echo on - telling me what's going on
SET VOL="USB" setting a variable
echo Finding disk which has Volume Label of %VOL% - telling me whats happening
SET LETTER= setting another variable
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
vol %%a: > tmp.txt

this is the main bit im struggling with, I looked here http://www.robvanderwoude.com/for.php but I still cant understand, but I think some kind of
loop is set against searching these drive letters (but how it knows to search drive letters Im not sure) it then creates tmp.txt file (where ever the script is run)

pause
findstr "\<USB\>" tmp.txt 1> nul - then searchescontents of txt file for USB (as defined by variable), then also I dont get what "1>" means
if NOT errorlevel 1 SET LETTER=%%a finds a match and then UPDATES previous variable of LETTER=
)
@echo DRIVE LETTER IS %LETTER% - sets drive letter
del tmp.txt
pause





##############################################################################################################
Also I know Im getting way haead of myself but is there a way to surpress file not found output in the command window while is searches for %VOL% tmp.txt

many thanks cant beat a bit of learning !!

#14 amalux

amalux

    Platinum Member

  • Tutorial Writer
  • 2,813 posts
  •  
    United States

Posted 27 March 2011 - 06:51 AM

The original code I gave you looks through each drive for a label starting with "USB", so "USB_Test", "USB Drive" etc. Where found it calls sub1, so any code you add under sub1 is run on that drive. Basically, a for loop cycles through each (in this case drive) listed, looking for a set of parameters you give it and where found, calls some action on it.

Here's a slight variation to help clarify and remember, you can do whatever you want under sub1. EOF (end of file) just returns back to keep searching, you can drop it if only one drive is needed. I don't know how to completely silence 'file not found' but you can hide the whole command window if wanted with a simple script; let me know if that would help.
@echo off & setLocal EnableDelayedExpansion



for %%a in (D E F G H I J K L M N O P Q R S T U V W X W Z) do (

for /f "tokens=6 delims= " %%i in (&#39;vol %%a: ^|find "drive"&#39;) do (

echo %%i | find /i "USB" > nul

if not errorlevel 1 call :sub1 %%a:

)

)

goto :eof



:sub1

SET LETTER=%1

echo.

echo Drive %LETTER% matches your criteria, now what?

echo.

echo Hit any key to keep searching...

echo.

pause>nul

goto :eof


#15 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 27 March 2011 - 08:40 AM

hey that works GREAT and no output while for the drive AND runs from mapped drive and still finds drive correctly - legendary!

#16 steve6375

steve6375

    Platinum Member

  • Developer
  • 5,184 posts
  • Location:UK
  • Interests:computers (!), programming (masm,vb6,C,vbs), OSes, photography,TV,films,guitars
  •  
    United Kingdom

Posted 27 March 2011 - 08:57 AM

A good reference source I use is at http://ss64.com/nt/
e.g. for a FOR loop - see http://ss64.com/nt/for_f.html
For parameters see http://ss64.com/nt/syntax-args.html
1>nul means redirect the stdout output (the text displayed as standard use text) to nul (nul is 'nothing' , i.e. do not display it on the screen) see http://ss64.com/nt/s...edirection.html for more details. Error messages can be redirected using 2>. Be careful when you use things like echo %FRED%>my.log - if FRED is set to HELLO2 then the command becomes echo HELLO2>my.log which means my.log will not contain any text!

#17 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 27 March 2011 - 11:45 AM

fantastic stuff, for the last stage I decided I want to output a log file named when the USB script was run ie 270311_1200.txt, although im confused, I was hoping to just add something at the start of the script to pipe to a txt file somewhere however from what I can gather it wants this after every command , where as I was hoping to just have one line at the start saying "right ok, output everything that happens in the CMD window (errors aswell!) to X:\270311_1200.txt

IS this possible too :_) tahnsk

#18 steve6375

steve6375

    Platinum Member

  • Developer
  • 5,184 posts
  • Location:UK
  • Interests:computers (!), programming (masm,vb6,C,vbs), OSes, photography,TV,films,guitars
  •  
    United Kingdom

Posted 27 March 2011 - 12:23 PM

Well, you can use domystuff.cmd >mylog.log 2>mylog.log - but any user messages or prompts will not be seen by the user.

It is best to use >>mylog.log 2>>mylog.log on every command that you want logged.

You might also look into the tee utility (not part of Windows). This will echo to the screen and tee the stream off to a file at the same time.

e.g. domystuff.cmd | tee mylog.log

Wintee http://code.google.com/p/wintee/
or GNU port http://unxutils.sourceforge.net/

I have not personally used these much and am not sure if the stderr stream is also tee'd off....

#19 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 27 March 2011 - 01:31 PM

could I set a variable to be the out put of GETDATE then pipe to %Variable%.txt, hope makes sense?

#20 Sha0

Sha0

    WinVBlock Dev

  • Developer
  • 1,672 posts
  • Location:reboot.pro Forums
  • Interests:Booting
  •  
    Canada

Posted 28 March 2011 - 04:11 AM

...for the last stage I decided I want to output a log file named when the USB script was run...I was hoping to just add something at the start of the script to pipe to a txt file somewhere however from what I can gather it wants this after every command , where as I was hoping to just have one line at the start saying "right ok, output everything that happens in the CMD window (errors aswell!)...


@echo off



:: Call main routine and log standard output and standard error.

call :_main > some_log.txt 2>&1

goto :eof



:_main

echo Hello!

dir v:\nosuchdir

goto :eof


could I set a variable to be the out put of GETDATE then pipe to %Variable%.txt, hope makes sense?

What is GETDATE?

You can parse %date% and %time%.

P. S. Did you see post #10?

#21 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 01 April 2011 - 04:56 AM

Dear Amalux,

I'm very much pleased with this code you provided for me and tests are great, but I was wonder if there was even further refinement possible? (not to worry if not).
It still out puts 2 error's which ideally I'd like suppressed. Please screen grab from my batch testing....(see under" finding USB" drive echo)
Actually ALSO, (sorry dont ask much do I lol) could it just search for whatever USB is plugged in without searching for a volume label, so say I just plug in a stick with NO lable and the script runs and says USB is E: etc - that would be ACE! :cheers:

@echo off & setLocal EnableDelayedExpansion



for %%a in (D E F G H I J K L M N O P Q R S T U V W X W Z) do (

for /f "tokens=6 delims= " %%i in (&#39;vol %%a: ^|find "drive"&#39;) do (

echo %%i | find /i "USB" > nul

if not errorlevel 1 call :sub1 %%a:

)

)

goto :eof



:sub1

SET LETTER=%1

echo.

echo Drive %LETTER% matches your criteria, now what?

echo.

echo Hit any key to keep searching...

echo.

pause>nul

goto :eof


Posted Image

Uploaded with ImageShack.us

#22 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,431 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 01 April 2011 - 02:32 PM

@Technotika
Hadn't you noticed the simple batches that have been suggested are all based on the ESSENTIAL condition that the Volume label contains "USB".
If you cheat and label your internal drive "USB_drive" they will assume that your internal drive is an USB device!
SUCH excessively simplified workarounds are NOT a good thing, as they are prone to produce errors and to create havoc.
If the question is:

How do I get the drive letter assigned to a USB device?

The answer CANNOT be:

Here, this will find any drive whose label contains "USB".


The actual answer is:

You cannot do that in "pure batch" you need to run an utility program.


Maybe these would come handy, first one expecially,as it also has provisions for the "stoopid" USB connected multi-card readers that often "steal" drive letters:
http://www.uwe-sieber.de/usbdlm_e.html
http://www.nirsoft.n...vices_view.html

And maybe you can get some ideas from these batches:
http://reboot.pro/8219/
http://www.msfn.org/...-drive-letters/

An alternative could be using WMI/WMIC:
http://blogs.technet...a-computer.aspx

:dubbio:
Wonko

#23 Technotika

Technotika

    Frequent Member

  • Advanced user
  • 419 posts
  • Location:United Kingdom
  •  
    United Kingdom

Posted 01 April 2011 - 03:11 PM

hmmm I had a hunch it wouldn't be a walk in the park I need to keep it simple and all in batch really, I think then I'd like the first phase of the script to SET UP volume label manually (via user input) then the search looks for that label, or something similar - having a think now....:dubbio:

#24 steve6375

steve6375

    Platinum Member

  • Developer
  • 5,184 posts
  • Location:UK
  • Interests:computers (!), programming (masm,vb6,C,vbs), OSes, photography,TV,films,guitars
  •  
    United Kingdom

Posted 01 April 2011 - 03:27 PM

Exact match to volume label and no (visible) error messages on screen (OK - I cheated!)

@echo off & setLocal EnableDelayedExpansion



SET MATCH=USB



for %%a in (D E F G H I J K L M N O P Q R S T U V W X W Z) do (

for /f "tokens=6 delims= " %%i in (&#39; vol %%a:&#39;  ) do (

 if @%MATCH%@==@%%i@ call :sub1 %%a:

)

cls

)

goto :eof



:sub1

SET LETTER=%1

echo.

echo Drive %LETTER% matches your criteria, now what?

echo.

echo Hit any key to keep searching...

echo.

pause>nul

goto :eof

tip: RMPartUSB will list USB devices only and give you their details as script environment variables - see http://sites.google....e/rmpartusb-faq

#25 amalux

amalux

    Platinum Member

  • Tutorial Writer
  • 2,813 posts
  •  
    United States

Posted 01 April 2011 - 06:46 PM

@steve6375

Cool cheat ;) RMPartUSB (all your stuff) is great!


@Wonko

Trying ddlist.cmd (all versions) gives output:

Environment variable media not defined

Environment variable drive not defined

Drives by drive letter:

Environment variable Line not defined

Press any key to continue . . .



Drives by connection:

Environment variable Line not defined

Press any key to continue . . .

EDIT: Strange, went to older version of DD and works, maybe something corrupted, thanks for nice batch!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users