[Batch] The Official Batch File Repository - Learn & ask questions about batch files

Discussion in 'Scripting' started by timesurfer, Oct 11, 2010.

  1. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #381 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    ok, i read many times @Stackoverflow that the findstr command in buggy in some cases, it seems that this is one of them,
    - when the files comparison exceeds 9 lines the findstr command fails.

    The solution is very simple, we create our own comparison function/sub-routine:
    Code:
    for /f "tokens=* delims=" %%A in ('type "%temp%\Af.txt"') do (find "%%A" "%temp%\Be.txt" >nul 2>&1 || set IX=%%A)
    
    echo %IX%
    
    pause
    
    output:
    Code:
    Index=16
    
    full documentation of the findstr command is here @StackOverflow (credits to dbenham @Stackoverflow for this amazing detailed documentation)
    try it and tell me the results :hug2:.
     
  2. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,094
    7,170
    180
    #382 xinso, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    Yeh. Very nice.
    Code:
    :NO
    :--------------------------------------
    :Thanks to Wk-952's instruction
    wmic path Win32_NetworkAdapter get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    type "%temp%\Be.txt"
    pause
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic path Win32_NetworkAdapter get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    type "%temp%\AF.txt"
    pause
    for /f "tokens=* delims=" %%A in ('type "%temp%\Af.txt"') do (find "%%A" "%temp%\Be.txt" >nul 2>&1 || set IX=%%A)
    echo %IX%
    pause
    Code:
    Index=0
    Index=1
    Index=2
    Index=3
    Index=4
    Index=5
    Index=6
    Index=7
    Index=8
    Index=9
    Index=10
    Index=11
    Index=12
    Index=13
    Index=14
    Index=15
    Index=17
    Index=18
    Index=19
    Index=20
    Index=21
    Press any key to continue . . .
    Index=0
    Index=1
    Index=2
    Index=3
    Index=4
    Index=5
    Index=6
    Index=7
    Index=8
    Index=9
    Index=10
    Index=11
    Index=12
    Index=13
    Index=14
    Index=15
    Index=16
    Index=17
    Index=18
    Index=19
    Index=20
    Index=21
    Press any key to continue . . .
    Index=16
    Press any key to continue . . .
    Note: wmic nic allows 3, and wmic path Win32_NetworkAdapter allows 8.


    Solved at last. Don't know how to thank you. You are so nice and kind. Thanks, Wk-952.
     
  3. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,094
    7,170
    180
    #383 xinso, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    @ Wk-952

    Here's a list of PNPDeviceID together with InstallTimeStamp
    Code:
    ROOT\NET\0000=DE070900050013000300280014006002 
    ROOT\NET\0001=DE070900050013000300340036008601 
    ROOT\NET\0002=DE070900050013000300280018004601 
    ROOT\NET\0003=DE07090005001300030028001B006903 
    ROOT\NET\0006=DE0709000500130003002B000100CA00 
    ROOT\NET\0023=DE070900050013000300360032006B03 
    ROOT\NET\0024=DE07090005001300030039000B00BA03 
    ROOT\NET\0028=DE0709000500130003003A0031008102 
    ROOT\NET\0032=DE070900050013000400050020002E03 
    ROOT\NET\0033=DE07090005001300040006000F00A701 
    ROOT\NET\0034=DE0709000500130004000F0012007D03
    Do you think it would be quicker to determine the most recent installed device thru InstallTimeStamp?

    How to revert it to readable and comparable output format and find out the Biggest Number?

    like
    Code:
    C:\Windows\system32>WMIC OS GET InstallDate
    InstallDate
    20140901020313.000000+480

    Thanks.

    Edit:

    Before.txt
    Code:
    ROOT\NET\0001=DE070900050013000C002B0000005B03 
    ROOT\NET\0002=DE070900050013000C002B0003001001 
    ROOT\NET\0003=DE070900050013000C002B0006005A03 
    After.txt
    Code:
    ROOT\NET\0000=DE070900050013000D001C001B009800
    ROOT\NET\0001=DE070900050013000C002B0000005B03 
    ROOT\NET\0002=DE070900050013000C002B0003001001 
    ROOT\NET\0003=DE070900050013000C002B0006005A03 
    Is Number and Alphabet in a string comparable ?

    If yes, could we simply manage to list the biggest one?
     
  4. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #384 wk-952, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    EDIT: when i converted this to decimal format i got a very long and weird number (eg: DE070900050013000300280014006002 ---> 2227905019030400200962),
    so i kept searching for a hex to decimal converter but they all gave the same sumber
    sorry i don't know how convert this, but we can find another way.
    first what's the command you used to get this output:
    Code:
    ...
    ROOT\NET\0001=DE070900050013000300340036008601 
    ROOT\NET\0002=DE070900050013000300280018004601 
    ROOT\NET\0003=DE07090005001300030028001B006903
    ...
    
     
  5. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,094
    7,170
    180
    #385 xinso, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    Code:
    [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0041]
    "InstallTimeStamp"=hex:de,07,09,00,05,00,13,00,04,00,0f,00,12,00,7d,03
    Code:
    @ECHO OFF
    pushd "%~dp0"
    set FL=format:list
    
    for /f "tokens=2 delims==" %%A in ('"wmic nic where ServiceName='tap0901' get PNPDeviceID /%FL%"') do (call:GITS "%%A" "Be")
    
    type Be.txt
    
    pause
    
    :GITS
    for /f "tokens=7 delims=\" %%A in ('reg query "HKLM\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}" /s /d /v /f %~1') do %E0% (set PTH=%%A)
    
    for /f "tokens=3 delims= " %%A in ('reg query "HKLM\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\%PTH%" /v InstallTimeStamp') do %E0% (set ITS=%%A)
    
    echo %~1=%ITS% >> %~2.txt
    
    goto:eof
    ROOT\NET\xxxx is for reference to fetch exact TAP Index of the most recent InstallTimeStamp found

    If possible, then bingo.

    1. Install a new TAP

    2. List and fetch the right InstallTimeStamp

    3. Fetch the Index for the TAP

    4. Set IP/MASK ...

    Done

    Edit: Get Index and InstallTimeStamp at a time

    Code:
    @ECHO OFF
    pushd "%~dp0"
    set FL=format:list
    
    for /f "tokens=2 delims==" %%A in ('"wmic nic where ServiceName='tap0901' get PNPDeviceID /%FL%"') do (call:GITS %%A)
    
    type Index_InstallTimeStamp.txt
    
    pause
    
    :GITS
    for /f "tokens=7 delims=\" %%A in ('reg query "HKLM\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}" /s /d /v /f %~1') do %E0% (set IDX=%%A)
    
    for /f "tokens=3 delims= " %%A in ('reg query "HKLM\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\%IDX%" /v InstallTimeStamp') do %E0% (set ITS=%%A)
    
    echo %IDX%=%ITS% >> Index_InstallTimeStamp.txt
    
    goto:eof
    Code:
    0000=DE070900050013000D001C001B009800
    0004=DE070900050013000C002B0000005B03
    0009=DE070900050013000C002B0003001001
    0010=DE070900050013000C002B0006005A03
    Note: 0, 4, 9, 10 in 00xx is the Index number. And 0 is the TAP we are after. But I don't know yet how to get it without 0.
     
  6. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #386 wk-952, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    if you want to remove the leading zeros for example in (00047)
    use this function:
    (before this command line: echo %IDX%=%ITS% >> Index_InstallTimeStamp.txt)
    Code:
    :Trunc_loop
    set "_tst=%IDX:~0,1%"
    if "%_tst%"=="0" if not "%IDX%"=="0" (set "IDX=%IDX:~1%"&goto :Trunc_loop)
    
    
    echo %IDX%
    
    output:
    Code:
    47
    
    and for this cryptic time-stamp i can't find a solution, sorry o_O
    i'll keep searching until i find a solution.
     
  7. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,094
    7,170
    180
    #387 xinso, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    Thanks.

    And wait, Wk-952. Look
    Code:
    if DE070900050013000D001C001B009800 GTR DE070900050013000C002B0006005A03 (echo BIGGER)
    pause
    
    if DE070900050013000C002B0006005A03 GTR DE070900050013000C002B0003001001 (echo BIGGER)
    pause
    
    if DE070900050013000C002B0003001001 GTR DE070900050013000C002B0000005B03 (echo BIGGER)
    pause
    
    if DE070900050013000C002B0000005B03 LSS DE070900050013000C002B0003001001 (echo SMALLER)
    pause
    
    if DE070900050013000C002B0003001001 LSS DE070900050013000C002B0006005A03 (echo SMALLER)
    pause
    
    if DE070900050013000C002B0006005A03 LSS DE070900050013000D001C001B009800 (echo SMALLER)
    pause
    Code:
    BIGGER
    Press any key to continue . . .
    BIGGER
    Press any key to continue . . .
    BIGGER
    Press any key to continue . . .
    SMALLER
    Press any key to continue . . .
    SMALLER
    Press any key to continue . . .
    SMALLER
    Press any key to continue . . .
    True or false?

    Wk-952, please don't keep it in mind. It's OK with me. Thanks.

    (I'll come back and feedback if I found a solution.)
     
  8. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #388 wk-952, Sep 19, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    :eek: TRUE!, based on results from http://www.asciitohex.com/
    (put a space after each 2 characters eg: DE070900050013000D001C001B009800 ---> DE 07 09 00 05 00 13 00 0D 00 1C 00 1B 00 98 00)

    from the help page of if command:
    Code:
    where compare-op may be one of:
    
    
        EQU - equal
        NEQ - not equal
        LSS - less than
        LEQ - less than or equal
        GTR - greater than
        GEQ - greater than or equal
    
    
    and the /I switch, if specified, says to do case insensitive string
    compares.  The /I switch can also be used on the string1==string2 form
    of IF.  These comparisons are generic, in that if both string1 and
    string2 are both comprised of all numeric digits, then the strings are
    converted to numbers and a numeric comparison is performed.
    
    Nice finding xinso, i guess this is why it works despite the fact that not every character is numeric.

    EDIT:
    of course i'm not very sure of the website's credibility, so if we couldn't find an alternative solution
    you might want to do many severe comparison tests to make sure that it's not just luck.
    again very sorry i couldn't help this time
     
  9. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    Hi All,

    I'd like to do command lines to remove the following lines from hosts file located in C:\WINDOWS\System32\Drivers\etc\hosts instead I have to do manually.

    127.0.0.1 lmlicenses.wip4.adobe.com
    127.0.0.1 lm.licenses.adobe.com

    Please help me do this.

    Thanks for your help and concern.
     
  10. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #390 wk-952, Feb 13, 2015
    Last edited by a moderator: Apr 20, 2017
    @sanjose

    Save this as a .bat file and run as admin, also
    make sure to pause your anti-virus since they oppose
    any changes to the hosts file.
    Code:
    @echo off
    setlocal enableExtensions disableDelayedExpansion
    color 3f
    
    
    :getInput
    echo Type a semi-colon separated list of the unwanted strings
    set /p "strings=> " || goto :getInput
    
    
    :getFile
    set "inputFile=%windir%\System32\drivers\etc\hosts"
    set "inputFile=%inputFile:"=%"
    set "inputFile=%inputFile:/=\%"
    if "%inputFile:~-1%"=="\" set "inputFile=%inputFile:~0,-1%"
    call :chkIF_file_exist || (cls & goto :getFile)
    call :getOrgAttrib
    call :getOrgNameExtension "%inputFile%"
    
    
    :getTmpFolder
    set "tmpOutputFile=%temp%\_tmpWriteFile_"
    set "tmpOutputFile=%tmpOutputFile:"=%"
    set "tmpOutputFile=%tmpOutputFile:/=\%"
    if "%tmpOutputFile:~-1%"=="\" set "tmpOutputFile=%tmpOutputFile:~0,-1%"
    call :chkIF_folder_exist "%tmpOutputFile%" || (cls & goto :getTmpFolder)
    
    
    :read_write
    attrib -r -s -h +a "%inputFile%" 1>nul 2>&1
    for /f "tokens=* delims=" %%A in ('type "%inputFile%"') do (
    set "currentLine=%%~A"
    call :isLineForbidden || (
    setlocal enableDelayedExpansion
    (echo !currentLine!) 1>>"%tmpOutputFile%"
    endLocal
    )
    )
    if exist "%tmpOutputFile%" move /y "%tmpOutputFile%" "%orgDir%" 1>nul 2>&1
    if exist "%inputFile%" del /f /q "%inputFile%" 1>nul 2>&1
    if exist "%orgDir%_tmpWriteFile_" rename "%orgDir%_tmpWriteFile_" "%orgNameExt%" 1>nul 2>&1
    if exist "%inputFile%" attrib %orgAttribs% "%inputFile%" 1>nul 2>&1
    if exist "%tmpOutputFile%" del /f /q "%tmpOutputFile%" 1>nul 2>&1
    goto :end
    
    
    :isLineForbidden
    set /a n=0
    :enum_loop
    set "forbiddenLine=<-|null|->"
    set /a n+=1
    for /f "tokens=%n% delims=;" %%A in ("%strings%") do (
    set "forbiddenLine=%%~A"
    setlocal enableDelayedExpansion
    if /i "!currentLine!"=="!forbiddenLine!" (endlocal & exit /b 0)
    endLocal
    )
    setlocal enableDelayedExpansion
    if not "!forbiddenLine!"=="<-|null|->" (endlocal & goto :enum_loop)
    endlocal
    exit /b 1
    
    
    :getOrgNameExtension
    set "orgDir=%~dp1"
    set "orgNameExt=%~nx1"
    exit /b
    
    
    :getOrgAttrib
    set "r=-r"
    set "a=-a"
    set "s=-s"
    set "h=-h"
    set "i=-i"
    set "x=-x"
    set /a n=0
    :attr_loop
    set "tstLine=<-|null|->"
    set /a n+=1
    for /f "tokens=%n% delims= " %%A in ('attrib "%inputFile%"') do (
    set "tstLine=%%~A"
    if /i "%%~A"=="r" (
    set "r=+r"
    ) else if /i "%%~A"=="a" (
    set "a=+a"
    ) else if /i "%%~A"=="s" (
    set "s=+s"
    ) else if /i "%%~A"=="h" (
    set "h=+h"
    ) else if /i "%%~A"=="i" (
    set "i=+i"
    ) else if /i "%%~A"=="x" (set "x=+x")
    )
    if not "%tstLine%"=="<-|null|->" goto :attr_loop
    set "orgAttribs=%r% %a% %s% %h% %i% %x%"
    exit /b
    
    
    :chkIF_file_exist
    set /a err=0
    dir "%inputFile%*" /b /a:-d 1>nul 2>&1 || (
    set /a err=1
    echo:
    echo xx_[The specified file doesn't exist]_xx
    echo:
    set /p =Press "Enter" to continue... 
    )
    exit /b %err%
    
    
    :chkIF_folder_exist
    set /a err=0
    set "tmpFolder=%~dp1"
    if "%tmpFolder:~-1%"=="\" set "tmpFolder=%tmpFolder:~0,-1%"
    dir "%tmpFolder%\*" /b /a:d 1>nul 2>&1 || (
    set /a err=1
    echo:
    echo xx_[The specified folder doesn't exist]_xx
    echo:
    set /p =Press "Enter" to continue... 
    )
    exit /b %err%
    
    
    :end
    echo:
    set /p =Done...Press "Enter" to exit
    endLocal
    
    
    

    Your input would be:
    Code:
    127.0.0.1 lmlicenses.wip4.adobe.com;127.0.0.1 lm.licenses.adobe.com
    
    This is not a direct search and replace script as you wished,
    instead it writes the same file again without the undesired lines.

    You can also use this script for other files,
    just change this line (red part):
    Code:
    set "inputFile=%windir%\System32\drivers\etc\hosts"
    
    to the full path of the target file (without a backslash at the end)

    Edit: one last thing might be important to you,
    any empty lines will be ignored (empty lines will be lost).

    Edit 2: updated script to be able to handle files with some special characters (%, ^, &, ...)
     
  11. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    @wk-952

    Thanks a lot for your help and concern. As you said, the input would be "127.0.0.1 lmlicenses.wip4.adobe.com;127.0.0.1 lm.licenses.adobe.com" but if my imput as follows:

    127.0.0.1 lmlicenses.wip4.adobe.com
    127.0.0.1 lm.licenses.adobe.com

    Does this .bat file work also ? or Do I have to correct the command lines again ?

    Thanks again, Bro.
     
  12. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    @sanjose

    the script takes the input strings separated by semi-colons only,
    it can't take inputs separated by a new line.
     
  13. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    108
    10
    #393 Compo, Feb 13, 2015
    Last edited by a moderator: Apr 20, 2017
    I'm afraid, that even though this is a batch file Topic, I would suggest you use a powershell script if possible, and instead of deleting/removing lines, just comment them out.

    Here are two powershell scripts for you:
    1. CmntOut.ps1 - comments out the lines you mentioned.
    Code:
    $hostsPath = "$env:windir\System32\drivers\etc\HOSTS"
    $hosts = get-content $hostsPath
    $hosts | Foreach {if ($_ -match '^\s*([^#].*?licenses.*adobe\.com\b)') 
    {"# " + $matches[1]} else {$_}} | Out-File $hostsPath -enc ascii
    2. UnComnt.ps1 - uncomments your lines again.
    Code:
    $hostsPath = "$env:windir\System32\drivers\etc\HOSTS"
    $hosts = get-content $hostsPath
    $hosts | Foreach {if ($_ -match '^\s*#\s*(.*?licenses.*adobe\.com\b)')
    {$matches[1]} else {$_}} | Out-File $hostsPath -enc ascii
    Due to the location of the HOSTS file I would suggest that you would probably need to run the scripts as Administrator.
     
  14. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    @Compo

    Thanks for your help and suggestion. I do not ever run powershell before. Can you write a command lines if possible please.

    Thanks in advance.
     
  15. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    108
    10
    I'm afraid I don't understand your question, my intention was that you copied and pasted the code into a blank notepad page and 'save as' the name I gave. Then you invoke the script assuming that you have an appropriate execution policy.
     
  16. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    @Compo

    I Copied and Save it as CmntOut.ps1 and then run as admin .... It does not remove the following line out of hosts file. anything that I have to correct, bro ?

    127.0.0.1 lmlicenses.wip4.adobe.com
    127.0.0.1 lm.licenses.adobe.com

    Thanks
     
  17. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    108
    10
    #397 Compo, Feb 15, 2015
    Last edited by a moderator: Apr 20, 2017
    The easiest way to run this which is in keeping with the Topic is to create a one line batch file
    Code:
    @Powershell -C "X:\PathTo\CmntOut.ps1"
    right click on the batch file and run as administrator
     
  18. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    13
    10
    Try following command. I added rem del hosts.txt command at last. You can delete rem to delete the file hosts.txt. Otherwise it will remain as backup copy.

    @echo off
    cd %systemroot%\System32\
    ren hosts hosts.txt
    findstr /v /i /c:"lmlicenses.wip4.adobe.com" /c:"lm.licenses.adobe.com" hosts.txt >hosts
    rem del hosts.txt
     
  19. sanjose

    sanjose MDL Member

    Jan 23, 2012
    182
    21
    10
    @Kamrul08

    Thanks so much for your help and concern.
     
  20. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    13
    10
    Did it work?