简体   繁体   English

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

[英]Powershell how to move everything but newly created file to another directory

I have a directory that is getting populated with text files every morning and it generates one single output file.我有一个目录,每天早上都会填充文本文件,它会生成一个 output 文件。 I have also created a Dump directory that will get populated with the files that were used to create the output file.我还创建了一个Dump目录,该目录将填充用于创建 output 文件的文件。

My question is, how can I move everything except the output file to the dump folder to be deleted?我的问题是,如何将除 output 文件之外的所有内容移动到要删除的转储文件夹中? I am not quite sure how to do that.我不太确定该怎么做。

My code so far:到目前为止我的代码:

# 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
}

In reference to my answer in your earlier question , you can change that code to参考我在您之前的问题中的回答,您可以将该代码更改为

$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
}

in order to move all txt files, except the one used for output为了移动所有 txt 文件,除了用于 output 的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件移动到新创建的子文件夹 - Move file to newly created subfolder 如何将文件移动到另一个目录并在 Powershell 的一行中使用 pipe 更改其内容? - How to move a file to another directory and change the content of it using pipe in one line in Powershell? powershell,如何管理目录并将文件移动到另一个文件夹 - powershell, how to mointor a directory and move files over to another folder 使用 PowerShell,如何将文件添加到新创建的文件集合中? - Using PowerShell, how can I add a file to a newly created file collection? 使用PowerShell直接将新创建的组移动到另一个OU - Moving newly created groups to another OU directly with PowerShell 在PowerShell中将新创建的对象从一个列表添加到另一个列表 - Adding newly created objects from one list to another in PowerShell Powershell:根据文件中的字符串将文件从一个目录移动到另一个目录 - Powershell: Move file from one directory to another, based on string within file 使用 Powershell 将文件移动到 Teams SharePoint 目录 - Move file to Teams SharePoint directory using Powershell 如何在 Powershell 中获取新创建的 Firefox window 的正确 PID? - How to get the proper PID of a newly created Firefox window in Powershell? 如何通过Powershell获取新创建的Azure VM的IP地址 - How to get the IP address of the newly created Azure VM via Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM