powershell 文件/文件夹操作
生活随笔
收集整理的這篇文章主要介紹了
powershell 文件/文件夹操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
新建文件夾
New-Item -ItemType Directory -Force -Path $TargetPath復(fù)制文件夾到另外文件夾
Copy-Item <源文件夾> <新文件夾> -recurse -force復(fù)制文件(與修改文件名)
// 達(dá)到復(fù)制文件到新文件夾,及修改文件名效果 copy-item $testPic $nowselectHDD// 目標(biāo)文件不能包含路徑,既不能移動(dòng)到新文件夾 rename-Item $filePath -NewName $NewFilePath最新修改文件
$allFolder= Get-ChildItem "C:\Target" $newFolder=($allFolder|Sort-Object LastWriteTime -Descending|select -first 1).name//1. Get-ChildItem獲取所有文件 //2. Sort-Object LastWriteTime -Descending 排序 //3. select -first 1 選擇前 1 個(gè) //4. .name 只獲取文件名。如果沒有會(huì)出現(xiàn)制表跳過最新的前面n個(gè)文件
$files = $allPIc | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 2 if ($files.count -gt 0) {foreach($file in $files){Remove-Item $file.FullName -Recurse -Force} } //1. Select-Object -Skip 2 排除前兩個(gè)文件 //2. foreach 刪除所有選擇文件。 //3. 最后剩下1跳過的文件。刪除文件
Remove-Item?c:/scripts/*?-recurse
$TargetFolder = "c:\Test" $Files = get-childitem $TargetFolder -force Foreach ($File in $Files) { $FilePath=$File.FullName Remove-Item -Path $FilePath -Recurse -Force }可以使用“get-help remove-item -full”命令來查看Remove-Item的完整幫助文檔,內(nèi)容如下:
PS C:\> get-help remove-item -full
復(fù)制文件
Copy-Item c:/scripts/Test.txt c:/Test Copy-Item c:/scripts/* c:/Test Copy-Item c:/scripts/*.txt c:/Test Copy-Item c:/scripts c:/Test –recurse //復(fù)制文件夾獲取文件名
[System.IO.Path]::GetFileNameWithoutExtension("test.mm.txt") //結(jié)果為test.mm獲取txt內(nèi)容
$txt=(Get-Content d:\1.txt -TotalCount 2)[-1].Trim()$txt[0] //第一行//-TotalCount 表示獲取txt內(nèi)行數(shù),2表示取前2行 //[-1] 表示最后一個(gè) //Trim() 去空格輸出到txt文件
"Hello World!" | Out-File d:\1.txt $fileCount | Out-File -Append d:\1.txt //追加獲取txt所有內(nèi)容,以及行數(shù)
$statusList = Get-Content ./StatusList.txt $statusList[0] //第一行$statusList.length//所有行比較文件內(nèi)容
PS C:\> $old=Get-Content c:\test\File1.txt PS C:\> $new=Get-Content c:\test\File2.txt PS C:\> Compare-Object $old $new其他文件名操作參考
https://www.pstips.net/navigating-the-file-system.html
總結(jié)
以上是生活随笔為你收集整理的powershell 文件/文件夹操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django07: 模板语言(旧笔记)
- 下一篇: django08: 视图与路由(旧笔记)