PowerShell 技能连载 - 文件系统操作技巧

在 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. 文件压缩和解压:
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. 文件权限管理:
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. 文件系统清理:
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) - $_"
}
}
}

这些技巧将帮助您更有效地处理文件系统操作。记住,在进行文件系统操作时,始终要注意数据安全性和权限管理。同时,建议在执行批量操作前先进行备份。

PowerShell 技能连载 - 文件系统操作技巧

https://blog.vichamp.com/2025/01/07/powershell-file-system-operations/

作者

Victor Woo

发布于

2025-01-07

更新于

2025-01-07

许可协议

PowerShell 技术 QQ 群