简体   繁体   中英

How to run the powershell code against each subfolder that is in the IIS root directory?

I would like to run the powershell code below, against each sub-folder that is in the IIS root directory. The output should be separate .htm file for each sub-folder contents (containing only the files in that sub-folder). If you need me to clarify my question, just ask.

$basedir = 'c:\inetpub\wwwroot'
$exp     = [regex]::Escape($basedir)
$server  = 'http://172.16.246.76'

function Create-HtmlList($fldr) {
  Get-ChildItem $fldr -Force |
    select ...
    ...
  } | Set-Content "$fldr.htm"
}

# list files in $basedir:
Create-HtmlList $basedir

# list files in all subfolders of $basedir:
Get-ChildItem $basedir -Recurse -Force |
  ? { $_.PSIsContainer } |
  % {
    Create-HtmlList $_.FullName
  }

You need to separate folder-traversal (recursive) from file processing (non-recursive), eg like this:

$basedir = 'c:\inetpub\wwwroot'
$exp     = [regex]::Escape($basedir)
$server  = 'http://172.16.x.x'

function Create-HtmlList($fldr) {
  Get-ChildItem $fldr -Force |
    ? { -not $_.PSIsContainer } |
    select ...
    ...
  } | Set-Content "$fldr.htm"
}

# list files in $basedir:
Create-HtmlList $basedir

# list files in all subfolders of $basedir:
Get-ChildItem $basedir -Recurse -Force |
  ? { $_.PSIsContainer } |
  % {
    Create-HtmlList $_.FullName
  }

The output files will be put into the respective folder (named after the folder with the extension .htm appended). If you want a different name or location for the output files, you need to adjust the Set-Content line in the function Create-HtmlList .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM