在 PowerShell 中处理文件系统操作是一项基础但重要的任务。本文将介绍一些实用的文件系统操作技巧。
首先,让我们看看文件系统的基本操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $basePath = "C:\Projects\MyApp" $directories = @( "src", "src\components", "src\utils", "tests", "docs" )
foreach ($dir in $directories) { $path = Join-Path $basePath $dir New-Item -ItemType Directory -Path $path -Force Write-Host "创建目录:$path" }
|
文件复制和移动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $sourceDir = "C:\SourceFiles" $targetDir = "D:\Backup" $filePattern = "*.docx"
$files = Get-ChildItem -Path $sourceDir -Filter $filePattern -Recurse
foreach ($file in $files) { $relativePath = $file.FullName.Substring($sourceDir.Length) $targetPath = Join-Path $targetDir $relativePath $targetDirPath = Split-Path -Parent $targetPath New-Item -ItemType Directory -Path $targetDirPath -Force Copy-Item -Path $file.FullName -Destination $targetPath Write-Host "已复制:$($file.Name) -> $targetPath" }
|
文件内容处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $sourceFiles = Get-ChildItem -Path "C:\Logs" -Filter "*.log"
foreach ($file in $sourceFiles) { $content = Get-Content -Path $file.FullName -Raw $newContent = $content -replace "ERROR", "错误" $newContent = $newContent -replace "WARNING", "警告" $newPath = Join-Path $file.Directory.FullName "processed_$($file.Name)" $newContent | Set-Content -Path $newPath -Encoding UTF8 }
|
文件系统监控:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| function Watch-FileSystem { param( [string]$Path, [string]$Filter = "*.*", [int]$Duration = 300 ) $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $Path $watcher.Filter = $Filter $watcher.IncludeSubdirectories = $true $watcher.EnableRaisingEvents = $true Write-Host "开始监控目录:$Path" Write-Host "监控时长:$Duration 秒" $action = { $event = $Event.SourceEventArgs $changeType = $event.ChangeType $name = $event.Name $path = $event.FullPath Write-Host "`n检测到变化:" Write-Host "类型:$changeType" Write-Host "文件:$name" Write-Host "路径:$path" } Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $action Register-ObjectEvent -InputObject $watcher -EventName Renamed -Action $action Start-Sleep -Seconds $Duration $watcher.EnableRaisingEvents = $false $watcher.Dispose() }
|
一些实用的文件系统操作技巧:
文件压缩和解压:
1 2 3 4 5 6 7 8 9 10
| $sourcePath = "C:\Data" $zipPath = "C:\Archive\data.zip"
Compress-Archive -Path "$sourcePath\*" -DestinationPath $zipPath -Force
$extractPath = "C:\Extracted" Expand-Archive -Path $zipPath -DestinationPath $extractPath -Force
|
文件权限管理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $filePath = "C:\Sensitive\data.txt" $acl = Get-Acl -Path $filePath
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule( "Domain\Users", "Read", "Allow" ) $acl.SetAccessRule($rule)
Set-Acl -Path $filePath -AclObject $acl
|
文件系统清理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $tempPaths = @( $env:TEMP, "C:\Windows\Temp", "C:\Users\$env:USERNAME\AppData\Local\Temp" )
foreach ($path in $tempPaths) { Write-Host "`n清理目录:$path" $files = Get-ChildItem -Path $path -Recurse -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } foreach ($file in $files) { try { Remove-Item -Path $file.FullName -Force Write-Host "已删除:$($file.Name)" } catch { Write-Host "删除失败:$($file.Name) - $_" } } }
|
这些技巧将帮助您更有效地处理文件系统操作。记住,在进行文件系统操作时,始终要注意数据安全性和权限管理。同时,建议在执行批量操作前先进行备份。