powershell 怎么做桌面清洁器?

toe95027  于 2023-06-06  发布在  Shell
关注(0)|答案(1)|浏览(263)

我每次都有一个凌乱的桌面,因为我忘记把文件保存在正确的活页夹里。现在我在GitHub和其他地方搜索,为每周五阅读我的桌面活页夹的程序员,然后它将所有带有音频扩展的字段移动到音频活页夹,以及带有文本,图片和其他数据的SAE。
也许我可以自己定义扩展,但我不知道阅读部分的代码。我试着让它自己,但它不工作。并且代码应该在Power Shell上运行。
谢谢你的帮助

rxztt3cl

rxztt3cl1#

$desktopPath = [Environment]::GetFolderPath('Desktop')
$destinationPath = 'C:\Path\to\Destination\Folder'

# Get all files on the desktop
$files = Get-ChildItem -Path $desktopPath -File

# Filter files based on criteria (e.g., file extension)
$filteredFiles = $files | Where-Object { $_.Extension -eq '.txt' }

# Create the destination folder if it doesn't exist
if (-not (Test-Path -Path $destinationPath)) {
    New-Item -Path $destinationPath -ItemType Directory | Out-Null
}

# Move filtered files to the destination folder
foreach ($file in $filteredFiles) {
    Move-Item -Path $file.FullName -Destination $destinationPath
}

要运行脚本,例如每个星期五,您必须将其包含到Windows任务计划程序中

相关问题