<# 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'}
此 powershell 脚本将
- 在每个子文件夹中添加desktop.ini
- 设置一些属性
如果我与其他东西一起执行 .bat 文件中的脚本,它就会按预期工作
powershell.exe -noprofile -executionpolicy bypass -Command "D:\Programs\Media\'Media Manager'\c.ps1"
问题
我想添加此脚本应该工作的文件夹,无论是在 .ps1 还是在 .bat 中,无论可能
例如D:\folder one
&D:\folder two
答案1
看起来您的脚本当前适用于当前目录下的所有文件夹:
# the path is implied
Get-ChildItem -Path ./ -Recurse -Directory
您可以选择几种不同的方式来指定特定的文件夹:
# 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 | ...
}
对于选项 B+C,请确保在脚本中的-Path
每个Get-ChildItem
命令中指定,这样就很好了
答案2
要指定多个目标目录,可以为的-Path
参数指定一个字符串数组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"
然后,在您的处理中:
Copy-Item
可以使用-Passthru
参数让您立即访问新复制的文件以设置其Hidden
属性。- 其他属性操作也可以在同一个循环中执行。
$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'}
}