WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: juergenk on May 23, 2024, 01:44:13 AM

Title: USB_Stick name and labelname
Post by: juergenk on May 23, 2024, 01:44:13 AM
Hello friends

When I format a USB stick I can give it a name. How can I read this name with Winbatch?
I can also give the stick a name in the Autorun.inf file label=Myname
how can I read this name in Winbatch and how can I change it?

Thanks
Jurgen
Title: Re: USB_Stick name and labelname
Post by: td on May 23, 2024, 07:03:10 AM
To change a drive label you first navigate to the Tech Database and search using the search term "drive label" or something similar. One of the articles listed in the results is "Set Disk Volumn Name". That article offers two solutions of the several possible solutions.

The article can be found here:
Set Disk Volumn Name (https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Set~Disk~Volume~Name.txt)
Title: Re: USB_Stick name and labelname
Post by: cssyphus on May 23, 2024, 07:53:36 AM
For reading the label, you might also want these UDFs... Use them like this:

arrOut = udfCaptureDosOutput("cmd /C dir x:\nada2see.hyr") ;*** Change x: to your drive letter ***
message("Now, just parse the output", arrOut[1])

Using these UDFs (the 2nd one just for "how to use" reference):
    #DEFINEFUNCTION udfCaptureDosOutput(DosCommand) ;-------------------------------------
        ;See next udf ( udfDisplayCapturedDosOutput() ) for more info re How To Use
        ; WB Tech DB - Article ID: W15939 (also, but not used here, W15962)
        ; Ref this post: https://forum.winbatch.com/index.php?topic=883.msg3710#msg3710
        ; Returns array with stdOut data in slot 1
        ;USAGE EXAMPLE:
        ; cmd = 'fc /a C:\Temp\Data\test.txt C:\Temp\Data\test2.txt'
        ; dataarray = udfCaptureDosOutput( cmd )
        ; udfDisplayResult( dataarray )
        ;---------------------------------------------------------------------
      DebugTrace(22, ''); Allow DebugTrace continuation (inherit the debug mode from the caller).
      DataArray=ArrDimension(4)              ;Allocate return array
      ArrInitialize(DataArray,"")            ;initialize to all null strings
      ;Stuff the cmd in the 4 element of the array
      DataArray[3] = DosCommand
      oShell = ObjectCreate("WScript.Shell")    ;open shell object

      ;If 64 bit turn off redirection?
      If WinMetrics(-7) == 2
          ;64-bit Windows
          oldvalue = IntControl( 92, "disable", 0, 0, 0 )
          oScriptExec = oShell.Exec(DosCommand)  ;run the command
          IntControl( 92, "revert", oldvalue, 0, 0 )
      Else
          oScriptExec = oShell.Exec(DosCommand)  ;run the command
      EndIf

      ;Open output objects
      oStdOut = oScriptExec.StdOut
      oStdErr = oScriptExec.StdErr

      While (oScriptExec.Status==0)          ;wait for completion
          ;Caputure StdOut data
          oStdOut = oScriptExec.StdOut
          While ! oStdOut.AtEndOfStream
            strLine = oStdOut.ReadLine
            DataArray[1] = StrCat(DataArray[1],strLine,@CRLF)
          EndWhile
          ;Capture StdErr data
          oStdErr = oScriptExec.StdErr
          While ! oStdErr.AtEndOfStream
            strLine = oStdErr.ReadLine
            DataArray[2] = StrCat(DataArray[2],strLine,@CRLF)
          EndWhile
          TimeDelay(0.1)
      EndWhile

      ;Get remainder of data, if any
          ;Caputure StdOut data
          oStdOut = oScriptExec.StdOut
          While ! oStdOut.AtEndOfStream
            strLine = oStdOut.ReadLine
            DataArray[1] = StrCat(DataArray[1],strLine,@CRLF)
          EndWhile
          ;Capture StdErr data
          oStdErr = oScriptExec.StdErr
          While ! oStdErr.AtEndOfStream
            strLine = oStdErr.ReadLine
            DataArray[2] = StrCat(DataArray[2],strLine,@CRLF)
          EndWhile

      DataArray[0]=oScriptExec.ExitCode        ;save errorlevel/exit code

      ;Close handles that were opened
      oStdOut = 0
      oStdErr = 0
      oScriptExec = 0
      oShell = 0

      ;Return the array
      Return(DataArray)
    #ENDFUNCTION ;udfCaptureDosOutput() --------------------------------------------------
    #DEFINEFUNCTION udfDisplayCapturedDosOutput( dataarray ) ;----------------------------
      Exitcode = dataarray[0]
      StdOut = dataarray[1]
      StdErr = dataarray[2]
      DosCommand = dataarray[3]
      If stdErr != ""
          Message(DosCommand: " - STDERR", StdErr)
          Message(DosCommand:" - EXITCODE", Exitcode)
      Else
          Message(DosCommand:" - STDOUT", StdOut)
      EndIf
      Return 1
    #ENDFUNCTION ;udfDisplayCapturedDosOutput() ------------------------------------------


References:
https://forum.winbatch.com/index.php?topic=640.msg2347#msg2347
https://forum.winbatch.com/index.php?topic=883.msg3710#msg3710
https://forum.winbatch.com/index.php?topic=251.msg931#msg931
https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsrch.web+WinBatch/How~To+TechHome/WinBatch/How~To


Title: Re: USB_Stick name and labelname
Post by: spl on May 23, 2024, 02:59:58 PM
Can't you just use WMI?  [Win32_Volume] and filter to Drive Letter
Title: Re: USB_Stick name and labelname
Post by: cssyphus on May 24, 2024, 07:53:33 AM
What would that code look like, Stan? Could you post a link to another forum post with similar code on which to base a solution?
Title: Re: USB_Stick name and labelname
Post by: td on May 24, 2024, 08:07:28 AM
Quote from: spl on May 23, 2024, 02:59:58 PMCan't you just use WMI?  [Win32_Volume] and filter to Drive Letter

WMI is always an option. However, using a DllCall just cuts out the middleman and makes for a much simpler script. All other "modern" approaches are extra layers on top of the same ABI.
Title: Re: USB_Stick name and labelname
Post by: spl on May 24, 2024, 11:37:37 AM
Quote from: td on May 24, 2024, 08:07:28 AM
Quote from: spl on May 23, 2024, 02:59:58 PMCan't you just use WMI?  [Win32_Volume] and filter to Drive Letter


WMI is always an option. However, using a DllCall just cuts out the middleman and makes for a much simpler script. All other "modern" approaches are extra layers on top of the same ABI.

True. probably a 'skin the cat' metaphor. I started using a basic PS on-liner and changed my usb G: label from Data to Greatful Dead then back again. Thought it would be just as easy to convert to WB via 'vba' and while easy to identify with a simple Foreach.. WB doesn't like the 'Put_' verb which vba has no problems with... so was going to put the code into 'nice try'. but this seemed to work
[EDIT GROM Previous Post]

;updated code, now seemed to work
drv = "G:"  ;if you know dive in advance, otherwise comment
strComputer = "."
oWMI = GetObject( "winmgmts:\\" : strComputer : "\root\cimv2")
WQL = "SELECT * FROM Win32_Volume WHERE DriveLetter='%drv%'" 
oP= oWMI.ExecQuery(WQL)

ForEach d in oP
  If  d.DriveLetter == drv
    d.Label = "Greatful Dead"
    d.Put_
  Endif
Next
   
oP=0
oWMI=0
Exit


but for the curious... simple PS code
$d = "G:"
Get-WmiObject -Class  Win32_Volume -Filter "DriveLetter = '$d'" |
Set-WmiInstance -Arguments @{Label='Greatful Dead'}
Title: Re: USB_Stick name and labelname
Post by: td on May 24, 2024, 01:29:45 PM
The IntControl is required for newer 64-bit systems when using 32-bit WinBatch.  Should have mentioned that in my original response.

Prev=IntControl(92,"Disable",0,0,0)
RetVal = DLLCall("Kernel32.dll",long:"SetVolumeLabelW",lpwstr:"G:\",lpwstr:"USB Stuff")
IntControl(92,"Revert",Prev,0,0)
exit

If you want assurance of success you can check the return value. 
Title: Re: USB_Stick name and labelname
Post by: td on May 24, 2024, 01:43:34 PM
I take that back. IntControl 92 is not needed. You do need to avoid a stupid user error, however...
Title: Re: USB_Stick name and labelname
Post by: spl on May 25, 2024, 03:24:10 AM
Looks like the OP has 3 approaches to the ask.
Title: Re: USB_Stick name and labelname
Post by: spl on May 30, 2024, 07:07:56 AM
While not related to the OP's specific ask, I dug this out of my archives. WMI is a good way to get comfortable with properties/methods.
strComputer = "."
oWMI = GetObject( "winmgmts:\\" : strComputer : "\root\cimv2")
WQL = "SELECT * FROM Win32_LogicalDisk" 
oP= oWMI.ExecQuery(WQL)
i=1
ForEach d in oP
   id =  d.DeviceID
   t = d.Description
   Display(2,"LogicalDisk %i%",id:" ":t)
   i+=1
Next   
oP=0
oWMI=0
Exit
Title: Re: USB_Stick name and labelname
Post by: cssyphus on May 30, 2024, 07:09:33 AM
upvote / thumbsup