[英]Powershell how to move everything but newly created file to another directory
我有一個目錄,每天早上都會填充文本文件,它會生成一個 output 文件。 我還創建了一個Dump
目錄,該目錄將填充用於創建 output 文件的文件。
我的問題是,如何將除 output 文件之外的所有內容移動到要刪除的轉儲文件夾中? 我不太確定該怎么做。
到目前為止我的代碼:
# If the path does not exist, force it to create it
if(!(Test-Path $Path)) {
New-Item -ItemType Directory -Force -Path $Path
# New-Item -ItemType Directory -Force -Path $SOF
# New-Item -ItemType Directory -Force -Path $EOF
}
if ($Path) {
# Move-Item $PathWithFiles -Destination $Path # move (not copy) files into new directory to concat
Get-ChildItem $PathWithFiles | ForEach-Object { # Output all except first and last line of current file
Get-Content $_ | Select-Object -Skip 1 | Select-Object -SkipLast 1
'' # Output an empty line
} | Add-Content $OutPutFile
}
參考我在您之前的問題中的回答,您可以將該代碼更改為
$Path = 'C:\RemoveFirst\*.txt'
$PathDump = 'C:\RemoveFirst\DumpARoo'
$Output = 'C:\RemoveFirst\TestingFile.txt'
if(!(Test-Path -Path $PathDump)) {
# create the folder if it does not yet exist
$null = New-Item -ItemType Directory $PathDump
}
# move all *.txt items from 'C:\RemoveFirst\txt' to 'C:\RemoveFirst\DumpARoo'
# EXCEPT the output file itself
$moveThese =(Get-ChildItem -Path $Path -Filter '*.txt' -File).FullName | Where-Object { $_ -ne $Output }
Move-Item -Path $moveThese -Destination $PathDump # move (not copy) files into new directory to concat
Get-ChildItem -Path $PathDump -Filter '*.txt' -File | ForEach-Object {
$_ | Get-Content |
Select-Object -Skip 1 |
Select-Object -SkipLast 1 |
Add-Content -Path $OutPut
}
為了移動所有 txt 文件,除了用於 output 的文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.