Search code examples
powershellbatch-filesubdirectory

Execute batch command only in first level subfolders


I want to execute this code only in all first level subfolders.

@echo off

powershell.exe "$o = Get-Content -Raw .\manifest.json | ConvertFrom-Json; '{0} {1} - Microsoft Flight Simulator {2}' -f $o.creator, $o.title, $o.version | Set-Content ('version_{0}_msfs.txt' -f (-split $o.title)[0])"

Can i do this inside the batch file? There is a manifest.json in file in each first level subfolder of the current folder. I want to run the batchfile from the current folder. A PowerShell part of the command will be good.


Solution

  • I finaly found a working code:

    @echo off
    
    setlocal enableDelayedExpansion
    
    REM Retrieve Parent Folder
    set parentfolder=%cd%
    
    REM Retrieve Subfolders
    set i=0
    for /f "eol=: delims=" %%F in ('dir /b /ad *') do (
      set /a i+=1
      set "folder!i!=%%F"
    )
    
    REM Loop Through Sub Folders
    for /l %%N in (1 1 %i%) do (
    cd %parentfolder%\!folder%%N!
    Powershell.exe "$o = Get-Content -Raw .\manifest.json | ConvertFrom-Json; '{0} {1} - Microsoft Flight Simulator {2}' -f $o.creator, $o.title, $o.package_version | Set-Content ('version_{0}_msfs.txt' -f (-split $o.title)[0]).ToLower()"
    )