簡體   English   中英

如何在PowerShell中創建desktop.ini?

[英]How to create desktop.ini in PowerShell?

我的目標是自定義所有文件夾圖標(如果查找器的名稱以“ _S”結尾)。因此我編寫了PowerShell函數,但自定義無效。

我的靈感來自:
網站在desktop.ini上設置屬性
設置自定義文件夾圖標的功能

function Set-icon_Folder_S($path) 
{

$ini = '[.ShellClassInfo]
        IconIndex =  0
        IconResource=C:\private.ico,0
        ConfirmFileOp = 0       
        DefaultDropEffect = 1'


    Set-Location $path
    #List all folder end with _S
    $items = Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S"))} | Select-Object -ExpandProperty FullName

    #For each folder _S ...
    foreach ($item in $items)
    {
        Set-Location $item

        #Init. Delete desktop.ini if exist
        try {
            Remove-Item desktop.ini -Force -erroraction stop
        }
        catch {         
        }

        #Write desktop.ini
        Write-Host "Go to $($item)"
        $ini | Out-File desktop.ini -Force
        Set-ItemProperty desktop.ini -Name Attributes -Value “ReadOnly,System,Hidden”

    }
}

我沒有發現錯誤。

以下調整后的腳本對我attrib.exe :將空行添加到desktop.ini尾部本身應該可以解決問題(我首先嘗試過調用attrib.exe所以它可能很豐富 ):

function Set-icon_Folder_S($path) 
{
$ini = '[.ShellClassInfo]
        IconIndex =  0
        IconResource=D:\Remote\icons\folderBlue.ico,0
        ConfirmFileOp = 0       
        DefaultDropEffect = 1
        '                          ### empty line: important

    Push-Location "$path"          ### Push-Location instead of Set-Location
    #List all folder end with _S
    $items = Get-ChildItem -Recurse | 
        Where-Object {
          ($_.Attributes -match "Directory") -and ($_.Name.EndsWith("es"))} | 
        Select-Object -ExpandProperty FullName                   ### ↑↑  change!!!

    #For each folder _S ...
    foreach ($item in $items)
    {
        Push-Location "$item"      ### Push-Location instead of Set-Location

        #Init. Delete desktop.ini if exist
        try {
            Remove-Item desktop.ini -Force -erroraction stop
        }
        catch {                    ### Write-Host "error removing $($item)"
        }

        #Write desktop.ini
        Write-Host "Go to $($item)"
        $ini | Out-File desktop.ini -Force
        Set-ItemProperty desktop.ini -Name Attributes -Value “ReadOnly,System,Hidden”
        Pop-Location               ### Pop-Location for corresponding Push-Location
        attrib.exe +R +S "$item"   ### Follow DESKTOP.INI CUSTOMIZATIONS DO NOT …
    }
    Pop-Location                   ### Pop-Location for corresponding Push-Location
}

Set-icon_Folder_S "D:\PShell"      ### call function declared above

上面的代碼中使用###注釋( IconResource 除外 )描述了更改:

### another IconResource used to match my testing environment
### empty line: important
### Push-Location instead of Set-Location
### used `"es"` suffix instead of `"_S"` one to match my testing environment
### Pop-Location for corresponding Push-Location
attrib.exe +R +S "$item"           ### Follow DESKTOP.INI CUSTOMIZATIONS DO NOT TAKE EFFECT
Set-icon_Folder_S "D:\PShell"      ### call function declared above

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM