Get-ChildItem参数之 -Exclude,Filter,Recurse应用

本文介绍如何使用 PowerShell 进行目录操作,包括排除特定子目录和文件、遍历指定关键字的目录等技巧,并演示如何删除指定目录下的所有文件但保留目录结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1 $p = "D:\PSScript"
2 
3 gci $p -Exclude "UpdateLog" #排除子目录"UpdateLog",但是后面不能接着使用 -Recurse参数,否则-Exclude参数失效
4 gci $p -Exclude "说明.txt" -Recurse #排除文件"说明.txt",可以一起使用 -Recurse参数
5 
6 gci $p -filter "UpdateLog" #只遍历子目录 "UpdateLog" 
7 gci $p -filter "*server*" #遍历包含关键字 server 的目录
8 gci $p -filter "*server*" -Recurse  # -Recurse参数不生效

 

1  #删除目录下所有文件,保留目录结构,其中除去UpdateLog子目录,除去“说明.txt”文件
2 gci $p -Exclude "UpdateLog" |% { gci $_.FullName -Exclude "说明.txt" -Recurse | ? {!$_.PSIsContainer } | Remove-Item -Force }

 

PS C:\Users\Administrator> # Fixed-ProjectCleanup.ps1 >> $projectRoot = "E:\ProjectEcosystem\ProjectMonitor" >> >> # 1. 创建标准文件夹结构 >> $folders = @( >> "SQLite", >> "Logs", >> "Database", >> "Scripts", >> "Config", >> "Binaries", >> "SourceFiles" >> ) >> >> foreach ($folder in $folders) { >> $path = Join-Path $projectRoot $folder >> if (-not (Test-Path $path)) { >> New-Item -ItemType Directory -Path $path -Force | Out-Null >> } >> } >> >> # 2. 修复的SQLite文件移动方法 >> $sqliteFiles = @( >> "System.Data.SQLite.*", >> "SQLite.Interop.dll", >> "sqlite3.*", >> "SQLite.NET.chm" >> ) >> >> foreach ($pattern in $sqliteFiles) { >> $files = Get-ChildItem -Path $projectRoot -Filter $pattern -File >> foreach ($file in $files) { >> $destDir = Join-Path $projectRoot "SQLite" >> $destPath = Join-Path $destDir $file.Name >> >> # 确保目标目录存在 >> if (-not (Test-Path $destDir)) { >> New-Item -ItemType Directory -Path $destDir -Force | Out-Null >> } >> >> Move-Item -Path $file.FullName -Destination $destPath -Force -ErrorAction SilentlyContinue >> if ($?) { >> Write-Host "✅ 已移动: $($file.Name) -> SQLite\" -ForegroundColor Cyan >> } >> } >> } >> >> # 3. 移动数据库文件 (修复路径问题) >> $dbFiles = Get-ChildItem -Path $projectRoot -Filter "*.db*" -File >> foreach ($db in $dbFiles) { >> $destDir = Join-Path $projectRoot "Database" >> $destPath = Join-Path $destDir $db.Name >> >> Move-Item -Path $db.FullName -Destination $destPath -Force >> Write-Host "✅ 已移动数据库: $($db.Name) -> Database\" -ForegroundColor Cyan >> } >> >> # 4. 移动脚本文件 (添加排除当前脚本) >> $scriptFiles = Get-ChildItem -Path $projectRoot -Filter "*.ps*1" -File | >> Where-Object { $_.Name -ne "Fixed-ProjectCleanup.ps1" } >> >> foreach ($script in $scriptFiles) { >> $destDir = Join-Path $projectRoot "Scripts" >> $destPath = Join-Path $destDir $script.Name >> >> Move-Item -Path $script.FullName -Destination $destPath -Force >> Write-Host "✅ 已移动脚本: $($script.Name) -> Scripts\" -ForegroundColor Cyan >> } >> >> # 5. 修复的配置文件移动方法 (排除目录) >> $configFiles = Get-ChildItem -Path $projectRoot -Filter "*config*" -File >> foreach ($config in $configFiles) { >> $destDir = Join-Path $projectRoot "Config" >> $destPath = Join-Path $destDir $config.Name >> >> Move-Item -Path $config.FullName -Destination $destPath -Force >> Write-Host "✅ 已移动配置: $($config.Name) -> Config\" -ForegroundColor Cyan >> } >> >> # 6. 新增移动可执行文件 >> $exeFiles = Get-ChildItem -Path $projectRoot -Filter "*.exe" -File >> foreach ($exe in $exeFiles) { >> $destDir = Join-Path $projectRoot "Binaries" >> $destPath = Join-Path $destDir $exe.Name >> >> Move-Item -Path $exe.FullName -Destination $destPath -Force >> Write-Host "✅ 已移动可执行文件: $($exe.Name) -> Binaries\" -ForegroundColor Cyan >> } >> >> # 7. 新增移动源代码文件 >> $sourceFiles = Get-ChildItem -Path $projectRoot -Filter "*.py" -File >> foreach ($source in $sourceFiles) { >> $destDir = Join-Path $projectRoot "SourceFiles" >> $destPath = Join-Path $destDir $source.Name >> >> Move-Item -Path $source.FullName -Destination $destPath -Force >> Write-Host "✅ 已移动源代码: $($source.Name) -> SourceFiles\" -ForegroundColor Cyan >> } >> >> # 8. 清理临时文件 (更安全的版本) >> $tempPatterns = @( >> "*.zip", >> "*.tmp", >> "*.bak", >> "Thumbs.db" >> ) >> >> foreach ($pattern in $tempPatterns) { >> Get-ChildItem -Path $projectRoot -Filter $pattern -File -ErrorAction SilentlyContinue | >> Remove-Item -Force -ErrorAction SilentlyContinue >> } >> >> # 9. 清理空文件夹 (排除标准目录) >> $excludeDirs = $folders | ForEach-Object { Join-Path $projectRoot $_ } >> Get-ChildItem -Path $projectRoot -Directory -Recurse | >> Where-Object { >> $_.FullName -notin $excludeDirs -and >> $_.GetFiles().Count -eq 0 -and >> $_.GetDirectories().Count -eq 0 >> } | >> Remove-Item -Force -Recurse >> >> # 10. 最终目录结构展示 >> Write-Host "✨ 清理完成!项目目录已整理 ✨" -ForegroundColor Green >> Write-Host "当前目录结构:" >> tree $projectRoot /F /A >> ✅ 已移动: System.Data.SQLite.dll -> SQLite\ ✅ 已移动: System.Data.SQLite.EF6.dll -> SQLite\ ✅ 已移动: System.Data.SQLite.EF6.pdb -> SQLite\ ✅ 已移动: System.Data.SQLite.EF6.xml -> SQLite\ ✅ 已移动: System.Data.SQLite.Linq.dll -> SQLite\ ✅ 已移动: System.Data.SQLite.Linq.pdb -> SQLite\ ✅ 已移动: System.Data.SQLite.Linq.xml -> SQLite\ ✅ 已移动: System.Data.SQLite.pdb -> SQLite\ ✅ 已移动: System.Data.SQLite.xml -> SQLite\ ✅ 已移动可执行文件: Installer.exe -> Binaries\ ✅ 已移动可执行文件: test.exe -> Binaries\ ✅ 已移动可执行文件: testef6.exe -> Binaries\ ✅ 已移动可执行文件: testlinq.exe -> Binaries\ ✅ 已移动源代码: db_init.py -> SourceFiles\ ✅ 已移动源代码: monitor.py -> SourceFiles\ ✅ 已移动源代码: state_analyzer.py -> SourceFiles\ ✨ 清理完成!项目目录已整理 ✨ 当前目录结构: 卷 ="模型响应", 的文件夹 PATH 列表 卷序列号为 7648-2CD3 E:\PROJECTECOSYSTEM\PROJECTMONITOR | Installer.pdb | SQLite.Designer.dll | SQLite.Designer.pdb | SQLite.Designer.xml | test.pdb | testef6.pdb | testlinq.pdb | +---APIServer | | .gitignore | | APIServer.code-workspace | | package.json | | | \---src | index.js | +---BackendService | | .gitignore | | BackendService.code-workspace | | BackendService.sln | | | \---src | \---BackendService | BackendService.csproj | Program.cs | +---Binaries | Installer.exe | test.exe | testef6.exe | testlinq.exe | +---Config | System.Data.SQLite.dll.config | test.exe.config | testef6.exe.config | testlinq.exe.config | +---DataAnalysis | | .gitignore | | DataAnalysis.code-workspace | | requirements.txt | | | \---src | main.py | +---Database | northwindEF.db | project_state.db | +---EcoMonitor | | .gitignore | | README.md | | | \---config | settings.psd1 | +---Logs +---MyApp | | .gitignore | | MyApp.code-workspace | | | \---src | index.html | main.js | style.css | +---MyWebApp | | .gitignore | | MyWebApp.code-workspace | | | \---src | index.html | main.js | style.css | +---Scripts | Initialize-DevEnv.psm1 | Monitor-Project.ps1 | Start-Ecosystem.ps1 | +---SourceFiles | db_init.py | monitor.py | state_analyzer.py | \---SQLite System.Data.SQLite.dll System.Data.SQLite.EF6.dll System.Data.SQLite.EF6.pdb System.Data.SQLite.EF6.xml System.Data.SQLite.Linq.dll System.Data.SQLite.Linq.pdb System.Data.SQLite.Linq.xml System.Data.SQLite.pdb System.Data.SQLite.xml PS C:\Users\Administrator> Set-Content -Path "E:\ProjectEcosystem\.gitignore" @" >> # 忽略临时文件 >> *.tmp >> *.bak >> Thumbs.db >> >> # 忽略编译输出 >> Binaries/ >> Logs/ >> >> # 忽略数据库文件 >> Database/*.db >> Database/*.db-shm >> Database/*.db-wal >> >> # 忽略个人配置文件 >> Config/personal.* >> "@ >> PS C:\Users\Administrator> # 创建项目工作空间配置文件 >> $workspaceConfig = @{ >> "folders" = @( >> @{ "path" = "SQLite" }, >> @{ "path" = "Database" }, >> @{ "path" = "Scripts" }, >> @{ "path" = "Config" }, >> @{ "path" = "Logs" }, >> @{ "path" = "Binaries" }, >> @{ "path" = "SourceFiles" } >> ) >> "settings" = @{ >> "files.exclude" = @{ >> "**/*.tmp": true, >> "**/*.bak": true, >> "**/Thumbs.db": true >> } >> } >> } >> >> ConvertTo-Json $workspaceConfig -Depth 3 | >> Set-Content -Path "E:\ProjectEcosystem\ProjectMonitor.code-workspace"
最新发布
08-14
PS C:\Users\Administrator> # ProjectCleanup.ps1 >> $projectRoot = "E:\ProjectEcosystem\ProjectMonitor" >> >> # 1. 创建标准文件夹结构 >> $folders = @( >> "SQLite", >> "Logs", >> "Database", >> "Scripts", >> "Config" >> ) >> >> foreach ($folder in $folders) { >> $path = Join-Path $projectRoot $folder >> if (-not (Test-Path $path)) { >> New-Item -ItemType Directory -Path $path -Force | Out-Null >> } >> } >> >> # 2. 识别并移动SQLite文件 >> $sqliteFiles = @( >> "System.Data.SQLite.dll", >> "System.Data.SQLite.Linq.dll", >> "SQLite.Interop.dll", >> "sqlite3.def", >> "sqlite3.dll", >> "SQLite.NET.chm" >> ) >> >> foreach ($file in $sqliteFiles) { >> $source = Join-Path $projectRoot $file >> $dest = Join-Path $projectRoot "SQLite" $file >> >> if (Test-Path $source) { >> Move-Item -Path $source -Destination $dest -Force >> Write-Host "✅ 已移动: $file -> SQLite\" -ForegroundColor Cyan >> } >> } >> >> # 3. 移动数据库文件 >> $dbFiles = Get-ChildItem -Path $projectRoot -Filter "*.db*" >> foreach ($db in $dbFiles) { >> Move-Item -Path $db.FullName -Destination (Join-Path $projectRoot "Database") -Force >> Write-Host "✅ 已移动数据库: $($db.Name) -> Database\" -ForegroundColor Cyan >> } >> >> # 4. 移动脚本文件 >> $scriptFiles = Get-ChildItem -Path $projectRoot -Filter "*.ps*1" >> foreach ($script in $scriptFiles) { >> Move-Item -Path $script.FullName -Destination (Join-Path $projectRoot "Scripts") -Force >> Write-Host "✅ 已移动脚本: $($script.Name) -> Scripts\" -ForegroundColor Cyan >> } >> >> # 5. 移动配置文件 >> $configFiles = Get-ChildItem -Path $projectRoot -Filter "*config*" >> foreach ($config in $configFiles) { >> Move-Item -Path $config.FullName -Destination (Join-Path $projectRoot "Config") -Force >> Write-Host "✅ 已移动配置: $($config.Name) -> Config\" -ForegroundColor Cyan >> } >> >> # 6. 清理临时文件 >> $tempFiles = @( >> "*.zip", >> "*.tmp", >> "*.log", >> "*.bak", >> "Thumbs.db" >> ) >> >> foreach ($pattern in $tempFiles) { >> Get-ChildItem -Path $projectRoot -Filter $pattern | Remove-Item -Force >> } >> >> # 7. 清理空文件夹 >> Get-ChildItem -Path $projectRoot -Directory -Recurse | >> Where-Object { $_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0 } | >> Remove-Item -Force -Recurse >> >> Write-Host "✨ 清理完成!项目目录已整理 ✨" -ForegroundColor Green >> Write-Host "当前目录结构:" >> tree $projectRoot /F /A >> Join-Path : 找不到接受实际参数“System.Data.SQLite.dll”的位置形式参数。 所在位置 行:32 字符: 13 + $dest = Join-Path $projectRoot "SQLite" $file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Join-Path],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand Move-Item : 无法处理参数,因为参数“destination”的值为空。请将参数“destination”的值更改为非空值。 所在位置 行:35 字符: 9 + Move-Item -Path $source -Destination $dest -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Move-Item],PSArgumentNullException + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.MoveItemCommand ✅ 已移动: System.Data.SQLite.dll -> SQLite\ Join-Path : 找不到接受实际参数“System.Data.SQLite.Linq.dll”的位置形式参数。 所在位置 行:32 字符: 13 + $dest = Join-Path $projectRoot "SQLite" $file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Join-Path],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand Move-Item : 无法处理参数,因为参数“destination”的值为空。请将参数“destination”的值更改为非空值。 所在位置 行:35 字符: 9 + Move-Item -Path $source -Destination $dest -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Move-Item],PSArgumentNullException + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.MoveItemCommand ✅ 已移动: System.Data.SQLite.Linq.dll -> SQLite\ Join-Path : 找不到接受实际参数“SQLite.Interop.dll”的位置形式参数。 所在位置 行:32 字符: 13 + $dest = Join-Path $projectRoot "SQLite" $file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Join-Path],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand Join-Path : 找不到接受实际参数“sqlite3.def”的位置形式参数。 所在位置 行:32 字符: 13 + $dest = Join-Path $projectRoot "SQLite" $file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Join-Path],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand Join-Path : 找不到接受实际参数“sqlite3.dll”的位置形式参数。 所在位置 行:32 字符: 13 + $dest = Join-Path $projectRoot "SQLite" $file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Join-Path],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand Join-Path : 找不到接受实际参数“SQLite.NET.chm”的位置形式参数。 所在位置 行:32 字符: 13 + $dest = Join-Path $projectRoot "SQLite" $file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Join-Path],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand ✅ 已移动数据库: northwindEF.db -> Database\ ✅ 已移动数据库: project_state.db -> Database\ ✅ 已移动脚本: Initialize-DevEnv.psm1 -> Scripts\ ✅ 已移动脚本: Monitor-Project.ps1 -> Scripts\ ✅ 已移动脚本: Start-Ecosystem.ps1 -> Scripts\ Move-Item : 参数错误。 所在位置 行:57 字符: 5 + Move-Item -Path $config.FullName -Destination (Join-Path $project ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (E:\ProjectEcosy...tMonitor\Config:DirectoryInfo) [Move-Item], IOException + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand ✅ 已移动配置: Config -> Config\ ✅ 已移动配置: System.Data.SQLite.dll.config -> Config\ ✅ 已移动配置: test.exe.config -> Config\ ✅ 已移动配置: testef6.exe.config -> Config\ ✅ 已移动配置: testlinq.exe.config -> Config\ ✨ 清理完成!项目目录已整理 ✨ 当前目录结构: 卷 ="模型响应", 的文件夹 PATH 列表 卷序列号为 7648-2CD3 E:\PROJECTECOSYSTEM\PROJECTMONITOR | db_init.py | Installer.exe | Installer.pdb | monitor.py | SQLite.Designer.dll | SQLite.Designer.pdb | SQLite.Designer.xml | state_analyzer.py | System.Data.SQLite.dll | System.Data.SQLite.EF6.dll | System.Data.SQLite.EF6.pdb | System.Data.SQLite.EF6.xml | System.Data.SQLite.Linq.dll | System.Data.SQLite.Linq.pdb | System.Data.SQLite.Linq.xml | System.Data.SQLite.pdb | System.Data.SQLite.xml | test.exe | test.pdb | testef6.exe | testef6.pdb | testlinq.exe | testlinq.pdb | +---APIServer | | .gitignore | | APIServer.code-workspace | | package.json | | | \---src | index.js | +---BackendService | | .gitignore | | BackendService.code-workspace | | BackendService.sln | | | \---src | \---BackendService | BackendService.csproj | Program.cs | +---Config | System.Data.SQLite.dll.config | test.exe.config | testef6.exe.config | testlinq.exe.config | +---DataAnalysis | | .gitignore | | DataAnalysis.code-workspace | | requirements.txt | | | \---src | main.py | +---Database | northwindEF.db | project_state.db | +---EcoMonitor | | .gitignore | | README.md | | | \---config | settings.psd1 | +---MyApp | | .gitignore | | MyApp.code-workspace | | | \---src | index.html | main.js | style.css | +---MyWebApp | | .gitignore | | MyWebApp.code-workspace | | | \---src | index.html | main.js | style.css | \---Scripts Initialize-DevEnv.psm1 Monitor-Project.ps1 Start-Ecosystem.ps1 PS C:\Users\Administrator>
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值