. (Join-Path $PSScriptRoot 'lab.ps1')
$root=New-LearningDirectory
try {
$source=New-Item -Path (Join-Path $root 'source') -ItemType Directory;$dest=New-Item -Path (Join-Path $root 'destination') -ItemType Directory
$file=Join-Path $source.FullName 'note [1].txt';Set-Content -LiteralPath $file -Value '一' -Encoding utf8;Add-Content -LiteralPath $file -Value '二' -Encoding utf8
$lines=@(Get-Content -LiteralPath $file);Assert-Learning ($lines.Count -eq 2 -and $lines[1] -eq '二') 'write append and literal brackets'
$copy=Assert-LearningPath -Path (Join-Path $dest.FullName 'copy.txt') -Root $root;Copy-Item -LiteralPath $file -Destination $copy
$moved=Assert-LearningPath -Path (Join-Path $dest.FullName 'moved.txt') -Root $root;Move-Item -LiteralPath $copy -Destination $moved;Assert-Learning (-not(Test-Path -LiteralPath $copy) -and (Test-Path -LiteralPath $moved)) 'move actual file'
Rename-Item -LiteralPath $moved -NewName 'renamed.txt';$renamed=Join-Path $dest.FullName 'renamed.txt';$info=Get-Item -LiteralPath $renamed;Assert-Learning ($info -is [IO.FileInfo] -and $info.Extension -eq '.txt') 'rename and FileInfo'
$copiedFolder=Assert-LearningPath -Path (Join-Path $root 'tree-copy') -Root $root;Copy-Item -LiteralPath $source.FullName -Destination $copiedFolder -Recurse
Assert-Learning (Test-Path -LiteralPath (Join-Path $copiedFolder 'note [1].txt') -PathType Leaf) 'recursive copy shape'
$log1=Join-Path $source.FullName 'a.log';$log2=Join-Path $source.FullName 'b.log';[IO.File]::WriteAllBytes($log1,[byte[]](1,2,3));[IO.File]::WriteAllBytes($log2,[byte[]](4,5))
$sum=Get-ChildItem -LiteralPath $source.FullName -Filter '*.log' -File | Measure-Object Length -Sum;Assert-Learning ($sum.Count -eq 2 -and $sum.Sum -eq 5) 'exact log bytes'
$output=Join-Path $root 'merged.txt';$inputs=@(Get-ChildItem -LiteralPath $source.FullName -Filter '*.txt' -File | Sort-Object Name);$inputs | ForEach-Object {Get-Content -LiteralPath $_.FullName} | Set-Content -LiteralPath $output -Encoding utf8
Assert-Learning (@(Get-Content -LiteralPath $output).Count -eq 2) 'merge outside input'
$safeDelete=Assert-LearningPath -Path $renamed -Root $root;Remove-Item -LiteralPath $safeDelete;Assert-Learning (-not(Test-Path -LiteralPath $renamed)) 'only owned file removed'
$escaped=$false;try{Assert-LearningPath -Path (Join-Path $root '../outside.txt') -Root $root}catch{$escaped=$true};Assert-Learning $escaped 'path escape rejected'
Write-LearningReport 'cmdlet-file-system-operations' @{lines=$lines;recursive_copy_contains_file=$true;log_count=$sum.Count;log_bytes=$sum.Sum;merge_rows=2;file_extension=$info.Extension;literal_brackets_handled=$true;path_escape_rejected=$escaped;owned_delete_observed=$true}
}finally{Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验
入口文件:projects/powershell-lab/cmdlet-file-system-operations.ps1
预期结果与验证条件观察提示
- 字面[]文件写追加、复制/移动/改名、目录复制、日志精确5字节、合并2行及实际自有删除。
- 路径越界被拒绝,所有目标只在自建目录。
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联16章) · 含当前章节或实例 · mbskycom202611a525.zip