Loop through folder of multiple wim's

Discussion in 'Windows 8' started by hypedave, Nov 11, 2014.

  1. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    Hello everyone,
    I am in need of assistance. I have a folder of a random number of custom wim files. I need help with creating a powershell of bat script that will loop through a folder of wim's and merge multiple wim files to one wim. Can anyone assist?
     
  2. cocachris89

    cocachris89 MDL Senior Member

    Mar 1, 2013
    491
    151
    10
    #2 cocachris89, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
  3. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #3 hypedave, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    It's my understanding that DISM does not support wildcards. This is what my script current looks likes;

    Code:
    Dism /ExportImage /SourceImageFile:Z:\Images\Project01.wim /SourceIndex:1 /DestinationImageFile:Z:\Images\install.wim
    Dism /ExportImage /SourceImageFile:Z:\Images\Project02.wim /SourceIndex:1 /DestinationImageFile:Z:\Images\install.wim
    Dism /ExportImage /SourceImageFile:Z:\Images\Project03.wim /SourceIndex:1 /DestinationImageFile:Z:\Images\install.wim
    Dism /ExportImage /SourceImageFile:Z:\Images\Project04.wim /SourceIndex:1 /DestinationImageFile:Z:\Images\install.wim
    
    The current problem is every time I add a new image I have to go back and edit the powershell script. Looking for a way to just automate this process.
     
  4. cocachris89

    cocachris89 MDL Senior Member

    Mar 1, 2013
    491
    151
    10
    Then in your bat script, just change the source image file name you want to export.
     
  5. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #5 s1ave77, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    Would use a for loop to get the WIM files ... :g::
    Code:
    for /r "Z:\Images" %%a in (*.wim) do (
    Dism /ExportImage /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:Z:\Images\install.wim
    )
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    Thanks Slave77, will this be a powershell or bat?
     
  7. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    It's pure batch.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    Didn't notice that you export to install.wim in same folder, that could act unpredictable. Recommend to save to another folder.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #9 hypedave, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I created a bat file with the following;

    Code:
    for /r "Z:\Images" %%a in (*.wim) do (
    Dism /ExportImage /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:Z:\Images\sources\install.wim
    
    I have received the following error code.

    Error: 87
    The exportimage option is unknown.
    For more information, refer to the help by running DISM.exe /?.
     
  10. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #10 s1ave77, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    Yeah, didn't notice the little error in your dism lines; should read:
    Code:
    for /r "Z:\Images" %%a in (*.wim) do (
    Dism /Export-Image /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:Z:\Images\sources\install.wim
    )
    or use:
    Code:
    for /r "Z:\Images" %%a in (*.wim) do Dism /Export-Image /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:Z:\Images\sources\install.wim
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,389
    11,614
    240
    #11 murphy78, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    can do that with normal cmd prompt:
    Code:
    forfiles /m *.wim /s /c "cmd /c dism /export-image /sourceimagefile:"@FILE" /sourceindex:1 /destinationimagefile:newfile.wim"
     
  12. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #12 s1ave77, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    I always underestimate forfiles command :doh:. Need to fight with it next.


    EDIT: Needs /p switch to set folder for WIMs:
    Code:
    forfiles /p "Z:\Images" /m *.wim /s /c "cmd /c dism /export-image /sourceimagefile:"@FILE" /sourceindex:1 /destinationimagefile:"Z:\install.wim"
    Code:
    F:\win\mount>forfiles /p "f:\win\mount" /m *.wim /s /c "cmd /c dism /export-imag
    e /sourceimagefile:"@FILE" /sourceindex:1 /destinationimagefile:f:\win\install.w
    im"
    
    
    Tool zur Imageverwaltung für die Bereitstellung
    Version: 6.3.9600.17031
    
    Das Image wird exportiert.
    [==========================100.0%==========================]
    Der Vorgang wurde erfolgreich beendet.
    
    Tool zur Imageverwaltung für die Bereitstellung
    Version: 6.3.9600.17031
    
    Das Image wird exportiert.
    [==========================100.0%==========================]
    Der Vorgang wurde erfolgreich beendet.
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #13 hypedave, Nov 12, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    The following code worked for me

    Code:
    for /r "Z:\Images" %%a in (*.wim) do Dism /Export-Image /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:Z:\Images\sources\install.wim
    
    Now I will focus on building out the rest of my script.

    1 - Service WIM files with latest updates
    2 - Automagically find and merge all wims files in a folder to a single wim file.
    3 - Split newly created merged wim file to separate swm files

    Thanks everyone.
     
  14. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #14 s1ave77, Nov 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Nice challenge you found :D. Are those WIMs Win 7 or Win 8?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    One folder contains Windows 7 and the other folder contains Windows 8.1.
    Now the clients wants me to create a MSI package of the final script. I need to learn more about the forfiles command.
     
  16. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #16 s1ave77, Nov 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Code:
    C:\Windows\system32>forfiles /?
    
    FORFILES [/P pathname] [/M searchmask] [/S]
             [/C command] [/D [+ | -] {dd.MM.yyyy | dd}]
    
    Description:
        Selects a file (or set of files) and executes a
        command on that file. This is helpful for batch jobs.
    
    Parameter List:
        /P    pathname      Indicates the path to start searching.
                            The default folder is the current working
                            directory (.).
    
        /M    searchmask    Searches files according to a searchmask.
                            The default searchmask is '*' .
    
        /S                  Instructs forfiles to recurse into
                            subdirectories. Like "DIR /S".
    
        /C    command       Indicates the command to execute for each file.
                            Command strings should be wrapped in double
                            quotes.
    
                            The default command is "cmd /c echo @file".
    
                            The following variables can be used in the
                            command string:
                            @file    - returns the name of the file.
                            @fname   - returns the file name without
                                       extension.
                            @ext     - returns only the extension of the
                                       file.
                            @path    - returns the full path of the file.
                            @relpath - returns the relative path of the
                                       file.
                            @isdir   - returns "TRUE" if a file type is
                                       a directory, and "FALSE" for files.
                            @fsize   - returns the size of the file in
                                       bytes.
                            @fdate   - returns the last modified date of the
                                       file.
                            @ftime   - returns the last modified time of the
                                       file.
    
                            To include special characters in the command
                            line, use the hexadecimal code for the character
                            in 0xHH format (ex. 0x09 for tab). Internal
                            CMD.exe commands should be preceded with
                            "cmd /c".
    
        /D    date          Selects files with a last modified date greater
                            than or equal to (+), or less than or equal to
                            (-), the specified date using the
                            "dd.MM.yyyy" format; or selects files with a
                            last modified date greater than or equal to (+)
                            the current date plus "dd" days, or less than or
                            equal to (-) the current date minus "dd" days. A
                            valid "dd" number of days can be any number in
                            the range of 0 - 32768.
                            "+" is taken as default sign if not specified.
    
        /?                  Displays this help message.
    
    Examples:
        FORFILES /?
        FORFILES
        FORFILES /P C:\WINDOWS /S /M DNS*.*
        FORFILES /S /M *.txt /C "cmd /c type @file | more"
        FORFILES /P C:\ /S /M *.bat
        FORFILES /D -30 /M *.exe
                 /C "cmd /c echo @path 0x09 was changed 30 days ago"
        FORFILES /D 01.01.2001
                 /C "cmd /c echo @fname is new since Jan 1st 2001"
        FORFILES /D +12.11.2014 /C "cmd /c echo @fname is new today"
        FORFILES /M *.exe /D +1
        FORFILES /S /M *.doc /C "cmd /c echo @fsize"
        FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #17 hypedave, Nov 14, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Do I have this right? The goal is to do the following;

    Loop through folder of wims
    mount the wim
    inject windows updates
    dismount

    move on to the next wim until complete. then merge wims to one file and then split. Here is my code.

    I deally I 'd prefer to mount the master wim with multiple indexes and inject the updates.

    Code:
    for /r "C:\images" %%a in (*.wim) do DISM /mount-wim /wimfile:C:\Extract\sources\%%a /mountdir:C:\mount /index:
    DISM /image:C:\images\mount /Add-Package /PackagePath:C:\images\wsusoffline\client\w63-x64\glb
    DISM /unmount-wim /mountdir:C:\images\mount /commit
    
    for /r "C:\images" %%a in (*.wim) do Dism /Export-Image /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:C:\images\master\install.wim
    Dism /Split-Image /ImageFile:C:\images\master\install.wim /SWMFile:C:\images\sources\install.swm /FileSize:256
    
    
     
  18. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #18 s1ave77, Nov 14, 2014
    Last edited by a moderator: Apr 20, 2017
    Would recommend to mount the single WIMS one by one and add updates in each pass:

    Code:
    for /r "C:\images" %%a in (*.wim) do (
    DISM /mount-wim /wimfile:%%a /index:1 /mountdir:C:\images\mount
    DISM /image:C:\images\mount /Add-Package /PackagePath:C:\images\wsusoffline\client\w63-x64\glb
    DISM /unmount-wim /mountdir:C:\images\mount /commit
    )
    
    Then export all single WIMs to master WIM and split. Recommend to store master WIM in another folder to avoid problems in the loop.

    Code:
    for /r "C:\images" %%a in (*.wim) do Dism /Export-Image /SourceImageFile:%%a /SourceIndex:1 /DestinationImageFile:C:\master\install.wim /compress:max /CheckIntegrity
    Dism /Split-Image /ImageFile:C:\images\master\install.wim /SWMFile:C:\images\sources\install.swm /FileSize:256
    
    
    Would go for a bigger size of SWMs: 4000.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    Okay what if I want to do the following;

    1. Backup Wim
    2. Mount it and save as new file name.

    I've noticed that imaging is quacking when using smaller swm files. What's the concern over a file 4GB vs a file of 500mb? Thanks for all your help so far.
     
  20. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #1. I mostly apply WIM/ESD/SWM via DISM from Win PE to avoid several Setup errors/traps (especially with splitted files).

    #2. OK. You want to export all WIMs to one master WIM, backup that one and then work with a copy, correct? Mount is limitted here, you can only mount a WIM and then unmount/commit to the same WIM. To change the names you need to export it.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...