Search code examples
batch-filefor-loopregistry

Reg File Import - Printer Mapping


I am trying to split the entries in reg files, which are in the format:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices]
"Send To OneNote 2013"="winspool,nul:"
"Adobe PDF"="winspool,Ne02:"
"\\\\myprint\\ZX101_HP1201C2220C"="winspool,Ne03:"
"\\\\myprint\\HSJD201_LexMark1024iFF"="winspool,Ne04:"

I am only trying to output ZX101, HSJD201; which always comes before a "_" (see the last two lines). Other commonality here is the \\\\myprint\\ always comes prior to these above mentioned entities that I am trying to capture.

Other entries in the reg file need to be ignored, like "OneNote, Adobe PDF".

I know that in the second line "HKEY_CURRENT_USER" will also be picked up because it contains "_"; but that is fine.

Edit 1 - Request for my code

for /f "usebackq tokens=* delims=myprint\\" %%a in ("C:\Users\admin\Printers_Mapped - Copy.txt") do set Value=%%a

Solution

  • for /f "usebackq tokens=2,3delims=_\" %%a in ("%filename1%") do IF "%%a"=="myprint" echo Value=%%b
    

    where filename1 contains potatoes.

    delims specifies a set of delimiter-characters, not a string.

    tokens=* specifies the entire line is to be assigned, not individual tokens.

    Using \ and _ as delimiters, the critical lines are tokenised as

    1. "
    2. myprint
    3. required data