Results 1 to 6 of 6

Thread: Copy text from registry & store it to a variable

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default Copy text from registry & store it to a variable

    I'm sure this is pretty simple, and I think it can be done via vbs, but I'm attempting to do this through a batch script (*.bat).

    Anywho here is the code
    Code:
    set defaultPrinter = "REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device"
    Basically I set a variable, and I want the reg_sz key value to be stored into that variable.
    Only problem is the output I get is something like this:
    Code:
    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows
        Device      REG_SZ  \\earth\IS_Copier,winspool,Ne09:
    I don't need anything after the printer info, so how would I only be able to set the variable the string "\\earth\IS_Copier" and filter out everything after it?

    Every user has a different default printer assigned to them so I can't just set a printer as the variable.

    Any help is appreciated.

  2. #2
    Administrator JoTo's Avatar
    Join Date
    May 2010
    Location
    www.scapp.net
    Posts
    1,953

    Default

    hm, you probably need to do some string manipulation, but no clue what string commands are available under dos. If there is a command to read a single character from a string like mid$ or something your done I would I say, you would loop through the string and read until the first occurence of the ","

  3. #3
    Administrator JoTo's Avatar
    Join Date
    May 2010
    Location
    www.scapp.net
    Posts
    1,953

    Default

    found something here:
    http://www.dostips.com/DtTipsStringO...pets.MidString

    not sure if that would be available in your environment though

    if it is sure that the "," is always at same position you can use the "Mid String - Extract a Substring by Position" command

    like:

    Code:
    set defaultPrinter = "REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device"
    set defaultprinter=%defaultprinter:~0,40%
    where 40 is the position of the comma

  4. #4
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    I'm testing out the output by a simple batch like this, but it doesn't seem to work properly. Any ideas?
    Code:
    set defaultPrinter= REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device
    echo %defaultPrinter%
    It should be outputting this:
    Code:
    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows
        Device      REG_SZ  Microsoft XPS Document Writer,winspool,Ne00:
    and instead it outputs the input haha
    Code:
    C:\Documents and Settings\James>"C:\Documents and Settings\James\Desktop\blah.bat"
    
    C:\Documents and Settings\James>set defaultPrinter= REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device
    
    C:\Documents and Settings\James>echo  REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device
     REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device
    Hmm...

  5. #5

    Default

    Came up with this:
    Code:
    for /f "skip=2 tokens=2,* " %%i in ('REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device') do set defaultPrinter=%%j
    set defaultprinter="aaa,%defaultprinter%"
    for /f "tokens=1,2 delims=," %%i in (%defaultprinter%) do set defaultPrinter=%%j
    echo %defaultPrinter%
    The first line fetches the value from the registry key, skips the reg_cz and stuff and puts it into defaultPrinter. The second adds an extra delimiter to it to make it easier for the third line. The third line only takes everything between two , which is your \\earth\IS_Copier. And the last, of course, echoes the stuff.

    Hope that's what you were looking for. That is if you're still looking for it

  6. #6
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    A bit late, but nonetheless, I appreciate the fact that you replied. Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •