0
<# copy desktop.ini recursively #> 
Get-ChildItem -Recurse -Directory| foreach { copy "D:\Programs\Media\Media Manager\Filebot\cmdlets\desktop.ini" $_.FullName}

<# set folder attribute readonly recursively #> 
Get-ChildItem -Recurse -Directory| foreach {$_.Attributes = 'readonly'}

<# set attribute readonly for desktop.ini recursively #> 
Get-ChildItem -Recurse -include *.ini| foreach {$_.Attributes = 'readonly,hidden'}

<# set attribute hidden for folder.ico recursively #> 
Get-ChildItem -Recurse -include *.ico| foreach {$_.Attributes = 'hidden'}

This powershell script will

  1. Add desktop.ini in each subfolder
  2. Set some attributes

It works as intented if I excute the script inside a .bat file along other things

 powershell.exe -noprofile -executionpolicy bypass -Command "D:\Programs\Media\'Media Manager'\c.ps1"

THE PROBLEM

I want to add the folders where this script should works, either to the .ps1 or in the .bat whatever possible

For example D:\folder one & D:\folder two

1
  • applying the code to existing folders. i haven't mentioned creating any new directory !!!
    – Rami Magdi
    Commented Jan 3, 2023 at 15:52

2 Answers 2

1

It looks like your script currently applies to all folders below whatever the current directory is:

# the path is implied
Get-ChildItem -Path ./ -Recurse -Directory

You could choose to specify a particular folder a few different ways:

# A. set your working directory first, then run your script normally. Repeat for each new folder
Set-Location "D:\folder one"
Get-ChildItem -Recurse -Directory | ... # Rest of script


# B. specify the folder in Get-ChildItem:
Get-ChildItem -Path "D:\folder one" -Recurse -Directory | ... 


# C. for many folders, try something like this, with a comma-separated list:
Foreach ($folder in "D:\folder one","D:\folder two") {
  Get-ChildItem -Path $folder -Recurse -Directory | ...
}

For option B+C, make sure to specify -Path in each Get-ChildItem command in your script, and you should be good

1
  • brilliant!. options A & B worked. Thanks so much!. for C , i guess the foreach expression outside foreach expression was too much to handle as it failed
    – Rami Magdi
    Commented Jan 3, 2023 at 17:44
0

To specify multiple target directories, you can specify a string array for the -Pathparammeter of Get-CHildItem:

<# Create a string array by spliting a here-string -- avoids typing
multiple quotation marks and delimiters and simplifies subsequent
editing.
#>
$TargetDirs = @'
D:\folder one
D:\folder two
' -split "`n"

Then, in your processing:

  • Copy-Item can use the -Passthru parameter to give you immediate access to the newly copied file to set its Hidden attribute.
  • The other attribute manipulation can also be performed in the same loop.
$iniSource   = 'D:\Programs\Media\Media Manager\Filebot\cmdlets\desktop.ini'

$TargetDirs = @'
D:\folder one
D:\folder two
' -split "`n"

Get-ChildItem $TargetDirs -Recurse -Directory| foreach {
    (copy $iniSource $_.FullName -PassThru).Attributes = 'ReadONly,Hidden'
    $_.Attributes = 'readonly'
    Get-ChildItem $_.FullName -FIlter *.ico | foreach {$_.Attributes = 'hidden'}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .