[SOLVED][HELP] Rewriting a batch script code using a For loop

Discussion in 'Scripting' started by Kenafri, Dec 14, 2015.

  1. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    104
    49
    10
    #1 Kenafri, Dec 14, 2015
    Last edited by a moderator: Apr 20, 2017
    Hello,

    I'm tryin' to rewrite code (it's about wallpapers replacer) below using a for loop :weep:.

    Anybody can help me ?


    This is my code:
    Code:
    set W7allpapersType=(Architecture, Characters, Landscapes, Nature, Scenes, Windows)
    set W7allpapersACLFile=(ArchDirFiles.txt, CharDirFiles.txt, LandDirFiles.txt, NatuDirFiles.txt, ScenDirFiles.txt, WinDirFiles.txt)
    
    
    if not "!WimIndexEdition%%i!"=="Starter" if not "!WimIndexEdition%%i!"=="HomeBasic" (
        icacls "%InstallMnt%\%%i\Windows\Web\Wallpaper\Architecture" /save "%ACL%\Install\!WimIndexEdition%%i!\ArchDirFiles.txt" /t /c >nul
        icacls "%InstallMnt%\%%i\Windows\Web\Wallpaper\Characters" /save "%ACL%\Install\!WimIndexEdition%%i!\CharDirFiles.txt" /t /c >nul
        icacls "%InstallMnt%\%%i\Windows\Web\Wallpaper\Landscapes" /save "%ACL%\Install\!WimIndexEdition%%i!\LandDirFiles.txt" /t /c >nul
        icacls "%InstallMnt%\%%i\Windows\Web\Wallpaper\Nature" /save "%ACL%\Install\!WimIndexEdition%%i!\NatuDirFiles.txt" /t /c >nul
        icacls "%InstallMnt%\%%i\Windows\Web\Wallpaper\Scenes" /save "%ACL%\Install\!WimIndexEdition%%i!\ScenDirFiles.txt" /t /c >nul
        icacls "%InstallMnt%\%%i\Windows\Web\Wallpaper\Windows" /save "%ACL%\Install\!WimIndexEdition%%i!\WinDirFiles.txt" /q >nul
    )
    

    My goal is to determine the ACL file name from the wallappers type folder name
    Code:
    |======================================================|
    | Index  | WallPapers Type Name | ACL File Name        |
    |======================================================|
    |    1   | Architecure          | ArchDirFiles.txt     |
    |------------------------------------------------------|
    |    2   | Characters           | CharDirFiles.txt     |
    |------------------------------------------------------|
    |    3   | Landscapes           | LandDirFiles.txt     |
    |------------------------------------------------------|
    |    4   | Nature               | NatuDirFiles.txt     |
    |------------------------------------------------------|
    |    5   | Scenes               | ScenDirFiles.txt     |
    |------------------------------------------------------|
    |    6   | Windows              | WinDirFiles.txt      |
    |======================================================|
    

    Thanx 4 your help.
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #2 Compo, Dec 14, 2015
    Last edited by a moderator: Apr 20, 2017
    My first advice is to post all of the code pertinent to your question and not to post code not pertinent. (unfortunately from what you have posted there is no way we can determine what it is you need advice on. What is the for loop in your Subject Title for?)
    We cannot easily determine an ACL name from the folder name because your names are inconsistent, (all your ACL names use the first four characters of the folder name apart from one).

    It would certainly be easier for you to receive the solution(s) you require if you were to re-evaluate your opening post and change things which may help us to achieve your goal.

    Code:
    @Echo Off
    SetLocal Enableextensions EnableDelayedExpansion
    
    (Set WPType=Characters Landscapes Nature Scenes Windows)
    If Not "!WimIndexEdition%%i!"=="Starter" (
    If Not "!WimIndexEdition%%i!"=="HomeBasic" Call :Sub)
    Pause
    Exit/b
    
    :Sub
    For %%a In (%WPType%) Do (
    Set "FN=%%a"
    If /I "!FN:~3,1!"=="d" (Set LC=!FN:~,3!) Else (Set LC=!FN:~,4!)
    Echo=icacls "%%InstallMnt%%\%%%%i\Windows\Web\Wallpaper\!FN!" /save "%%ACL%%\Install\^!WimIndexEdition%%%%i^!\!LC!DirFiles.txt" /t /c ^>nul
    )
     
  3. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    104
    49
    10
    #3 Kenafri, Dec 16, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thanx 4 your time & reply, Compo :worthy:.

    And sorry for my mistake: i'd send all the code :eek:.

    My portion of code is a modded version of MSMG ToolKit, added to support Windows 7 Wallpapers integration.

    You can find my complete mod HERE.

    Hope now things are clearer.

    Thanx again.
     
  4. Kenafri

    Kenafri MDL Member

    Jul 22, 2014
    104
    49
    10
    #4 Kenafri, Mar 15, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    @ Compo
    Finally fixed as below ::clap:

    Code:
    @echo off
    
    setlocal EnableExtensions EnableDelayedExpansion
    
    :IntCustWallpapers
    
    setlocal
    
    if not "%WimIndexNo%"=="*" (
        call :GetWimIndexArch %InstallWim%, 1 >nul
        
        cd /d "%InstallMnt%\Windows\WinSxS\"
    )
    
    if "%HOSTArch%"=="x64" (set SxSArch=amd64) else (set SxSArch=x86)
    
    if "%SOS%"=="w7x" (
        for /f %%i IN ('"dir /b "x86_*..l-wallpaper-starter*"" 2^>nul') do set StarterX86=%%i
    
        REM  Setting w7x WIM Image Wallpapers Folder Path Variables
        for /f %%i IN ('"dir /b "%SxSArch%_*..lpaper-architecture*"" 2^>nul') do (
            set "WPTypeSxS%%i=Architecture"
            if "%SxSArch%"=="amd64" (set "ArchitectureX64=%%i") else (set "ArchitectureX86=%%i")
        )
        for /f %%i IN ('"dir /b "%SxSArch%_*..allpaper-characters*"" 2^>nul') do (
            set "WPTypeSxS%%i=Characters"
            if "%SxSArch%"=="amd64" (set "CharactersX64=%%i") else (set "CharactersX86=%%i")
        )
        for /f %%i IN ('"dir /b "%SxSArch%_*..allpaper-landscapes*"" 2^>nul') do (
            set "WPTypeSxS%%i=Landscapes"
            if "%SxSArch%"=="amd64" (set "LandscapesX64=%%i") else (set "LandscapesX86=%%i")
        )
        for /f %%i IN ('"dir /b "%SxSArch%_*wallpaper-nature*"" 2^>nul') do (
            set "WPTypeSxS%%i=Nature"
            if "%SxSArch%"=="amd64" (set "NatureX64=%%i") else (set "NatureX86=%%i")
        )
        for /f %%i IN ('"dir /b "%SxSArch%_*wallpaper-scenes*"" 2^>nul') do (
            set "WPTypeSxS%%i=Scenes"
            if "%SxSArch%"=="amd64" (set "ScenesX64=%%i") else (set "ScenesX86=%%i")
        )
    )
    
    REM Setting w7x/w8x/w10x WIM Images Common Wallpaper Folder Path Variables
    for /f %%i IN ('"dir /b "%SxSArch%_*..l-wallpaper-windows*"" 2^>nul') do (
        set "WPTypeSxS%%i=Windows"
        if "%SxSArch%"=="amd64" (set "WindowsX64=%%i") else (set "WindowsX86=%%i")
    )
    
    
    cd /d %ROOT%\
    
    for /f %%i in ('dir /b /a:d "%InstallMnt%\Windows\Web\Wallpaper"') do (
        set "W7allpapersType=%%i"
        
        if "!W7allpapersType!"=="Windows" (
            set W7allpapersACLFile%%i=!W7allpapersType:~,3!
        ) else (
            set W7allpapersACLFile%%i=!W7allpapersType:~,4!
        )
        icacls "%InstallMnt%\Windows\Web\Wallpaper\!W7allpapersType!" /save "%ACL%\Install\%WimIndexEdition%\!W7allpapersACLFile%%i!DirFiles.txt" /c >nul
    )
    
    if "%HOSTArch%"=="x86" (
        if not "%HOSTEdition%"=="Starter" if not "%HOSTEdition%"=="HomeBasic" (
    
            for /d %%j in (!CharactersX86!, !LandscapesX86!, !ArchitectureX86!, !WindowsX86!, !NatureX86!, !ScenesX86!) do (
                set "W7allpapersTypeX86=%%j"
                
                if "!W7allpapersTypeX86!"=="!WindowsX86!" (
                    set "W7allpapersACLFileX86=!WPTypeSxS%%j:~,3!"
                ) else (
                    set "W7allpapersACLFileX86=!WPTypeSxS%%j:~,4!"
                )
                icacls "%InstallMnt%\Windows\WinSxS\!W7allpapersTypeX86!" /save "%ACL%\Install\%WimIndexEdition%\%WimIndexArch%\!W7allpapersACLFileX86!DirFilesX86.txt" /t /c >nul
            )
        )
    )
    

    Thanx again for assistance.