【问题标题】:How to create desktop.ini in PowerShell?如何在 PowerShell 中创建 desktop.ini?
【发布时间】:2017-02-02 15:18:37
【问题描述】:

如果文件夹的名称以“_S”结尾,我的目标是自定义所有文件夹图标 所以我写了一个 PowerShell 函数,但是自定义不起作用。

我的灵感来自:
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”

    }
}

我没有发现错误。

【问题讨论】:

  • 我添加了存档属性并删除了只读,但不起作用。文件夹图标没有变化。

标签: powershell desktop.ini


【解决方案1】:

以下调整后的脚本对我有用:在 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

上面的代码使用### cmets (except 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2012-06-23
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    相关资源
    最近更新 更多