[英]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.