Powershell如何将除新创建的文件外的所有文件移动到另一个目录



我有一个目录,每天早上都会用文本文件填充,它会生成一个输出文件。我还创建了一个Dump目录,该目录将填充用于创建输出文件的文件。

我的问题是,如何将除输出文件外的所有内容移动到要删除的转储文件夹?我不太清楚该怎么做。

到目前为止我的代码:

# 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:RemoveFirstDumpARoo'
$Output   = 'C:RemoveFirstTestingFile.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:RemoveFirsttxt' to 'C:RemoveFirstDumpARoo'
# 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文件,除了用于输出的文件

相关内容

最新更新