2017-02-02 58 views
0

我的目標是定製所有文件夾圖標,如果foder的名字以「_S」結尾 所以我編寫了PowerShell函數,但定製不起作用。如何在PowerShell中創建desktop.ini?

我把靈感來自於:
Website to set attribute on desktop.ini
Function that sets a custom folder icon

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」 

    } 
} 

我沒有發現錯誤。

+0

我加入存檔屬性和刪除只讀,但does'n噸工作。文件夾圖標沒有變化。 – Ferfa

回答

0

繼調整劇本對我的作品:添加一個空行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 
相關問題