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

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

  1. Humphrey

    Humphrey MDL Expert

    Dec 13, 2011
    1,466
    990
    60
    #341 Humphrey, Apr 6, 2014
    Last edited by a moderator: Apr 20, 2017
    Capture.bat
    Code:
    @echo off
    echo %%~1     =      %~1 
    echo %%~f1     =      %~f1
    echo %%~d1     =      %~d1
    echo %%~p1     =      %~p1
    echo %%~n1     =      %~n1
    echo %%~x1     =      %~x1
    echo %%~s1     =      %~s1
    echo %%~a1     =      %~a1
    echo %%~t1     =      %~t1
    echo %%~z1     =      %~z1
    echo %%~$PATHATH:1     =      %~$PATHATH:1
    echo %%~dp1     =      %~dp1
    echo %%~nx1     =      %~nx1
    echo %%~dp$PATH:1     =      %~dp$PATH:1
    echo %%~ftza1     =      %~ftza1
    pause
    
    Drop a file ioto Capture.bat and see what happens. (Yes, onto. Do not OPEN the batch but drop the file into the batch file and it will open.)

    Code:
    %~1     =      C:\Users\Humphrey\Desktop\FindMeAGoodOnePC.exe
    %~f1     =      C:\Users\Humphrey\Desktop\FindMeAGoodOnePC.exe
    %~d1     =      C:
    %~p1     =      \Users\Humphrey\Desktop\
    %~n1     =      FindMeAGoodOnePC
    %~x1     =      .exe
    %~s1     =      C:\Users\Humphrey\Desktop\FINDME~1.EXE
    %~a1     =      --a------
    %~t1     =      05/23/2012 02:10 PM
    %~z1     =      8961024
    %~$PATHATH:1     =
    %~dp1     =      C:\Users\Humphrey\Desktop\
    %~nx1     =      FindMeAGoodOnePC.exe
    %~dp$PATH:1     =      C:\Users\Humphrey\Desktop\
    %~ftza1     =      --a------ 05/23/2012 02:10 PM 8961024 C:\Users\Humphrey\Desktop\FindMeAGoodOnePC.exe
    Press any key to continue . . .
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. sanjose

    sanjose MDL Member

    Jan 23, 2012
    184
    21
    10
    Hi all,

    I'd like to integrate all Win XP Updates in Win XP SP3 x86 .ISO Source in case that I re-install Windows " the fresh installation ". Can anyone please help me write a .CMD or .BAT for integrating updates ?

    Thanks in advance.
     
  3. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
  4. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #344 xinso, Aug 24, 2014
    Last edited by a moderator: Apr 20, 2017
  5. BJ0RN

    BJ0RN MDL HTTP-uploader

    Dec 6, 2013
    334
    21,791
    10
    I have many files in a folder. Each file I want to split up into a .rar-file, and the first one has to be a .sfx so when its pressed it will unpack the other (.rar) and give me my original file. This means I have 10 files in a folder, I want winrar to pack each file (one file at the time) into 400MB size rar (in 10 different folders), and the first file(in each folder) has to be a .sfx To do this job i want to use batch, but I dont know the command for it. Hope there is someone here that can help me out.
     
  6. monyzoom

    monyzoom MDL Novice

    Oct 6, 2013
    10
    1
    0
    #346 monyzoom, Sep 4, 2014
    Last edited by a moderator: Apr 20, 2017
    please help me finding error & executing this
    ( i have dism.exe,imagex.exe and oscdimg.exe in the same folder with cmd Script and also have Windows AIK installed )
    Code:
    cd /d %~dp0
    CLS
    @ECHO OFF
    SET DVDSOURCE=%systemdrive%\7MSDN
    SET MOUNTDIR=%systemdrive%\mount
    SET SUBMOUNTDIR=%systemdrive%\mount2
    SET FirstUXRes=%systemdrive%\FirstUXRes
    SET STORE=D:\7ISO\ISO
    SET TRASH=c:\temp
    SET MOUNTNUM=
    :: this startup portion have problems only ( i have dism.exe,imagex.exe and oscdimg.exe in the same folder 
    :: with  cmd Script and also have Windows AIK installed
    SET DISM=%systemdrive%\Program Files\Windows AIK\Tools\Servicing\Dism.exe
    SET imagex=%systemdrive%\Program Files\Windows AIK\Tools\amd64\imagex.exe
    SET oscdimg=%systemdrive%\Program Files\Windows AIK\Tools\amd64\oscdimg.exe
    SET DISMFLAG=0
    IF NOT EXIST "%systemdrive%\Program Files\Windows AIK\Tools\Servicing\Dism.exe" 
    IF %DISMFLAG%==0 SET /p DISM=Path to WAIK DISM.EXE:%~dp0DISM.EXE
    IF EXIST %DISM% SET /p DISM=%DISM%
    SET imagexFLAG=0
    IF NOT EXIST "%systemdrive%\Program Files\Windows AIK\Tools\amd64\imagex.exe" 
    IF %imagexFLAG%==0 SET /p imagex=Path to WAIK imagex.EXE:%~dp0imagex.exe
    IF EXIST %imagex% SET imagex=%imagex%
    SET oscdimgFLAG=0
    IF NOT EXIST "%systemdrive%\Program Files\Windows AIK\Tools\amd64\oscdimg.exe" 
    IF %oscdimgFLAG%==0 SET /p oscdimg=Path to WAIK oscdimg.EXE:%~dp0oscdimg.exe
    IF EXIST %oscdimg% SET oscdimg=%oscdimg% CHOICE /C abcd /n /m "Select a for Update only,select b for customization only ,select c for After Update & customization,select d for Rt7Lite customization"
     
  7. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,965
    908
    60
    You have AIK installed. Why waste your time & confusion checking & pointing to these tools?
    Use the Deployment Tools Command Prompt. If you type set, you'll see paths are already setup for you & your platform...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. monyzoom

    monyzoom MDL Novice

    Oct 6, 2013
    10
    1
    0
    i want those code to be universal & i don't have WAIK installed always.
    so i need to solve this. Thanks for anybody's help.
     
  9. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #349 xinso, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    Help please ...

    I need to query or export the key, to get Source value, in order not to tamper users existent TAP-Windows.

    Code:
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%SystemRoot%/system32/DRIVERS/tap0901.sys]
    "Source"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
      00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,44,00,\
      72,00,69,00,76,00,65,00,72,00,53,00,74,00,6f,00,72,00,65,00,5c,00,46,00,69,\
      00,6c,00,65,00,52,00,65,00,70,00,6f,00,73,00,69,00,74,00,6f,00,72,00,79,00,\
      5c,00,74,00,61,00,70,00,30,00,39,00,30,00,31,00,2e,00,69,00,6e,00,66,00,5f,\
      00,61,00,6d,00,64,00,36,00,34,00,5f,00,35,00,61,00,31,00,66,00,65,00,63,00,\
      32,00,66,00,62,00,62,00,63,00,63,00,65,00,66,00,63,00,63,00,5c,00,74,00,61,\
      00,70,00,30,00,39,00,30,00,31,00,2e,00,73,00,79,00,73,00,00,00
    but it just won't.
    Code:
    ERROR: The system was unable to find the specified registry key or value.
    And how to export hex in readable text?
    Code:
    %SystemRoot%\System32\DriverStore\FileRepository\tap0901.inf_amd64_5a1fec2fbbccefcc\tap0901.sys

    Thanks.
     
  10. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    #350 wk-952, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso
    i assume you haven't added the extra percent mark (within a batch file)

    query the value:
    Code:
    reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%%SystemRoot%%/system32/DRIVERS/tap0901.sys" /v "Source"
    
    append to a variable:
    EDIT 2:
    Code:
    for /f "tokens=2* delims= " %%A in ('reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%%SystemRoot%%/system32/DRIVERS/tap0901.sys" /v "Source"') do set "val=%%~B"
    
    Question: why is the last part (/system32/DRIVERS/tap0901.sys) delimited with a forward slash '/' not a backward one '\' ? :confused: sorry if i'm missing something.

    EDIT: yeah i found similar location:
    Code:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%SystemDrive%/Program Files (x86)/NVIDIA Corporation/coprocmanager/detoured.dll
    
    i figured it out now, sorry my bad.
     
  11. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #351 xinso, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    Thank you, wk-952.

    It is forward slash '/'.

    The first command-line works great. And the Variable failed.

    All I want is to set the Variable for tap0901.sys' folder (PATH), like tap0901.inf_amd64_5a1fec2fbbccefcc

    So I can install new TAP-Windows with the user's own driver.

    Is it possible?

    Code:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFi
    les\%SystemRoot%/system32/DRIVERS/tap0901.sys
    
    
        Source    REG_EXPAND_SZ    %SystemRoot%\System32\DriverStore\FileRepository\
    tap0901.inf_amd64_5a1fec2fbbccefcc\tap0901.sys
    
    
    Press any key to continue . . .
    ERROR: The system was unable to find the specified registry key or value.
     
  12. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    @xinso

    weird :g: i got the same error too despite the fact that the command line sent to the CMD.exe window is correct (echo on)
    querying the value is fine but using the output in a for loop causes the problem, so i'll assume the issue is for loop processor/interpreter

    i'll try to figure a solution to this.
     
  13. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #353 xinso, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    Thank you. wk-952.

    Is it because of the clause being divided into 2 lines?
    Code:
    Source    REG_EXPAND_SZ    %SystemRoot%\System32\DriverStore\FileRepository\
    tap0901.inf_amd64_5a1fec2fbbccefcc\tap0901.sys
     
  14. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    #354 wk-952, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    no that only happens visually because the CMD.exe window is a little bit small

    to expand it:
    add this line before the for loop
    Code:
    mode con lines=25 cols=230
     
  15. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #355 xinso, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    wk-952,

    Code:
    @ECHO OFF
    pushd "%~dp0"
    
    mode con lines=25 cols=230
    
    reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%%SystemRoot%%/system32/DRIVERS/tap0901.sys" /v "Source"
    
    pause
    
    for /f "tokens=* delims=" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%%SystemRoot%%/system32/DRIVERS/tap0901.sys" /v "Source"') do set "val=%%~A"
    
    echo %val%
    
    pause
    Code:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\%System
    Root%/system32/DRIVERS/tap0901.sys    
    
        Source    REG_EXPAND_SZ    %SystemRoot%\System32\DriverStore\FileRepository\tap0901.inf_amd64_5a1fec2fbbccefcc\tap0901.sys
    
    Press any key to continue . . .
    
    ERROR: The system was unable to find the specified registry key or value.
    
    ECHO is off.
    
    Press any key to continue . . .
    Edit: I got it, wk-952.

    Code:
    set E=:
    
    for /f "tokens=6 delims=\" %%A in ('"dir /b /s %SystemRoot%\System32\DriverStore\FileRepository | findstr /i tap0901.sys"') do if errorlevel 0 (set E=e&set FOLDER=%%A)
    
    %E%cho %FOLDER%
    
    pause
    Code:
    tap0901.inf_amd64_5a1fec2fbbccefcc
    
    Press any key to continue . . .
    Thanks a lot, wk-952.

    Now I can install TAP-Windows using user's own one.
     
  16. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    #356 wk-952, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    Great work xinso :worthy:, very nice solution,

    also if you prefer the 1st non-working loop i found its solution,
    my doubts were right, the for loop itself does an additional parsing step (in a little bit different way) on its input before letting the CMD.exe process/interpret it;
    the major difference here between the normal line parser (invoked in any batch file on each line) and the for loop parser is that:
    - A percentage mark '%' can be escaped with the caret character '^' not with another '%' mark (exactly opposite to the normal line parser).
    :eek:unexpected

    so here is the final solution:
    Code:
    for /f "tokens=2* delims= " %%A in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\^^%%SystemRoot^^%%/system32/DRIVERS/tap0901.sys /v "Source"') do set "TAPdir=%%~B"
    
    could you please run this loop in a batch file, then echo the variable %TAPdir% and verify whether this is working as expected or not?

    my test was based on this loop:
    Code:
    @echo on
    for /f "tokens=2* delims= " %%A in ('reg query hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\^^%%SystemRoot^^%%/System32/BthHFSrv.dll /v "owners"') do set "val=%%~B"
    @echo off
    echo "%val%"
    
    output of the echo command:
    Code:
    "bthhfenum.inf"
    
    and the command line sent to the CMD.exe window was (this is after being parsed by the normal parser and before being parsed by the for loop parser):
    Code:
    for /F "tokens=2* delims= " %A in ('reg query hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\^%SystemRoot^%/System32/BthHFSrv.dll /v "owners"') do set "val=%~B"
    
    the down side of this is that we can't wrap the registry location is quotes (reg query "hklm\software...") as it will make the caret character effect-less (non-escaping),
    but in your case this is not an issue since the reg. location doesn't contain spaces.
    Hope this helps you more buddy:hug2:
     
  17. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #357 xinso, Sep 14, 2014
    Last edited by a moderator: Apr 20, 2017
    Hey, nice job.

    Thank you, wk-952.

    It is working wonderfully.

    Code:
    @ECHO OFF
    pushd "%~dp0"
    
    for /f "tokens=3 delims= " %%A in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\^^%%SystemRoot^^%%/system32/DRIVERS/tap0901.sys /v Source') do (set DIR=%%A)
    
    echo %DIR%
    
    pause
    Code:
    %SystemRoot%\System32\DriverStore\FileRepository\tap0901.inf_amd64_5a1fec2fbbccefcc\tap0901.sys
    And this is what Compo taught me:

    Code:
    @ECHO OFF
    pushd "%~dp0"
    
    for /f "tokens=2 delims==" %%A in ('wmic path SoftwareLicensingService get version /format:list') do (set VS=%%A)
    
    echo %VS%
    
    set CV=%VS:~,3%
    
    echo %CV%
    
    pause
    Code:
    6.3.9600.16497
    6.3
    How to get the value without the file name "tap0901.sys" in Compo's way?
    Code:
    %SystemRoot%\System32\DriverStore\FileRepository\tap0901.inf_amd64_5a1fec2fbbccefcc\
    Edit: Got it again from "set" command

    Code:
    Environment variable substitution has been enhanced as follows:
    
        %PATH:str1=str2%
    
    would expand the PATH environment variable, substituting each occurrence
    of "str1" in the expanded result with "str2".  "str2" can be the empty
    string to effectively delete all occurrences of "str1" from the expanded
    output.  "str1" can begin with an asterisk, in which case it will match
    everything from the beginning of the expanded output to the first
    occurrence of the remaining portion of str1.
    Code:
    @ECHO OFF
    pushd "%~dp0"
    
    for /f "tokens=3 delims= " %%A in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles\^^%%SystemRoot^^%%/system32/DRIVERS/tap0901.sys /v Source') do (set DIR=%%A)
    
    echo %DIR%
    
    echo.
    
    set ROUTE=%DIR:tap0901.sys=%
    
    echo %ROUTE%
    
    pause
    Code:
    %SystemRoot%\System32\DriverStore\FileRepository\tap0901.inf_amd64_5a1fec2fbbccefcc\tap0901.sys
    
    %SystemRoot%\System32\DriverStore\FileRepository\tap0901.inf_amd64_5a1fec2fbbccefcc\
     
  18. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #358 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    @ Wk-952

    Here are the command lines I use to get NetConnectionID and PNPDeviceID

    Code:
    for /f "tokens=2 delims==" %%A in ('"wmic nic where %IX% get NetConnectionID /%FL%"') do (set CI=%%A)
    Code:
    for /f "tokens=2 delims==" %%A in ('"wmic nic where %IX% get PNPDeviceID /%FL%"') do (set DI=%%A)
    Since they use the same syntax, how could we combine them into one command line to get both Variables?

    like

    Code:
    for /f "tokens=2,3 delims==" %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /%FL%"') do (set CI=%%A&set DI=%%B)
    Thanks
     
  19. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    #359 wk-952, Sep 18, 2014
    Last edited: Sep 18, 2014
    @xinso

    i know from your script that value of %FL% is "/format:list", but i don't know the value of %IX%,
    could you please provide a sample output of those 2 for loops?
     
  20. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,702
    13,707
    340
    #360 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    Yes, Wk-652.

    %IX% => Index=#

    This seemingly works, but cannot find oemwin2k.inf (installed a second TAP, but not converted to my target TAP-9.0.2.1)
    Code:
    FINDSTR: Cannot open oemwin2k.inf
    
    
    TAP-9.0.2.1 has been successfully installed with V.



    wmic nic where ServiceName='tap0901' get Index
    pause
    Code:
    Index
    0
    wmic nic where Index=0 get NetConnectionID, PNPDeviceID
    pause
    Code:
    NetConnectionID  PNPDeviceID
    Local Area Connection         ROOT\NET\0000
    for /f "tokens=1,2 delims= " %%A in ('"wmic nic where Index=0 get NetConnectionID, PNPDeviceID | findstr /i ROOT\NET"') do (set CI=%%A&set DI=%%B)
    echo %CI%
    echo %DI%
    pause
    Code:
    Local Area Connection
    ROOT\NET\0000
    Oh ... Traditional Chinese "區域連線" - No space, And "Local Area Connection" - with spaces. Does it matter?

    Now
    Code:
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    :--------------------------------------
    for /f "tokens=2 delims==" %%A in ('"wmic nic where %IX% get NetConnectionID /%FL%"') do (set CI=%%A)
    netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    for /f "tokens=2 delims==" %%A in ('"wmic nic where %IX% get PNPDeviceID /%FL%"') do (set DI=%%A)
    "%KQ%\%OA%\devcon" sethwid @%DI% := =%TP% >nul 2>&1
    goto:eof
    Target, like
    Code:
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    :--------------------------------------
    for /f "tokens=1,2 delims= " %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID | findstr /i ROOT\NET"') do (set CI=%%A&set DI=%%B)
    netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    "%KQ%\%OA%\devcon" sethwid @%DI% := =%TP% >nul 2>&1
    goto:eof