简体   繁体   English

Powershell将文件移动到特定文件夹

[英]Powershell Move Files to a specific folder

I need to move files, which satisfy specific conditions, from folder A to a specific subfolder in folder B. 我需要将满足特定条件的文件从文件夹A移动到文件夹B中的特定子文件夹。

The conditions are: 条件是:

  • Group the files that contain .exe and take the two with the highest number. 对包含.exe的文件进行分组,并使用编号最高的两个文件。

  • After the number take the string between the first two hyphen characters (-). 在数字后面加上前两个连字符(-)之间的字符串。 The custom-string 自定义字符串

  • Move these files in the folder B\\win(custom-string), if win folder does not exist create it, the same goes for the custom-string folder. 将这些文件移动到文件夹B \\ win(custom-string)中,如果不存在win文件夹,则将其创建,对custom-string文件夹也是如此。

So for example in the image below we would take the files CICone NT Setup 0.25.5-develop-build.0.exe and CICone NT Setup 0.25.5-develop-build.0.exe.blockmap and move them to a folder B\\win\\develop\\ , here the develop is the name of the folder (the string between the two first hyphen characters). 因此,例如在下图中,我们将文件CICone NT Setup 0.25.5-develop-build.0.exeCICone NT Setup 0.25.5-develop-build.0.exe.blockmap移到文件夹B\\win\\develop\\ ,这里的develop是文件夹的名称(两个第一个连字符之间的字符串)。 在此处输入图片说明


Here is the solution: 解决方法如下:

$winFiles = get-childitem | Where-Object {$_.Name -like "*.exe*"} | Sort-Object -Descending -Property Name | Select-Object -First 2

ForEach ($file in $winFiles){ 
$EnvironmentSubstring = $file.Name.Split('-')[1]

if(!(Test-Path ..\B\win)){

    New-Item -Path ..\B\win -ItemType Directory -Force | Out-Null

    if(!(Test-Path ..\B\win\$EnvironmentSubstring)){
    New-Item -Path ..\B\win\$EnvironmentSubstring -ItemType Directory -Force | Out-Null
    }
}

Move-Item -Path $file.Name -Destination ..\B\win\$EnvironmentSubstring\ -Force
}

I mocked up a directory with these files: 我用这些文件模拟了一个目录:

Mode                LastWriteTime         Length Name                                             
----                -------------         ------ ----                                             
-a----        4/25/2018  11:07 AM              3 CICone NT Setup 0.25.5-dev-build.exe             
-a----        4/25/2018  11:07 AM              3 CICone NT Setup 0.25.4-UAT-build.exe             
-a----        4/25/2018  11:07 AM              3 CICone NT Setup 0.25.3-UAT-build.exe             
-a----        4/25/2018  11:07 AM              3 CICone NT Setup 0.25.3-dev-build.exe             

Your first request was finding the two highest numbered .exe files in this path, that's easy. 您的第一个请求是在此路径中找到编号最高的两个.exe文件,这很容易。

>get-childitem *.exe | Sort-Object -Descending -Property Name | Select-Object -First 2

Mode                LastWriteTime         Length Name                                             
----                -------------         ------ ----                                             
-a----        4/25/2018  11:07 AM              3 CICone NT Setup 0.25.5-dev-build.exe             
-a----        4/25/2018  11:07 AM              3 CICone NT Setup 0.25.4-UAT-build.exe    

The next step is to store this list of files in a variable called $files like so. 下一步是像这样将文件列表存储在名为$files的变量中。

>$files = get-childitem *.exe | Sort-Object -Descending -Property Name | Select-Object -First 2

Now, to iterate through them and parse out the environment. 现在,遍历它们并解析环境。

PowerShell is an object based scripting language, which allows us to select properties of objects (in this case the .Name property of each file) and then act on those properties by calling methods on them. PowerShell是一种基于对象的脚本语言,它使我们能够选择对象的属性(在本例中为每个文件的.Name属性),然后通过在对象上调用方法来对它们进行操作。 We can use the .Split() method to break a string up on each instance of a character. 我们可以使用.Split()方法在每个字符实例上分解一个字符串。 For instance, if we want to split our files on the - char, we can do so like this, with the following output: 举例来说,如果我们希望将文件分割上-炭,我们可以这样做这样,具有以下的输出:

>$file.Name.Split('-')
CICone NT Setup 0.25.4
dev
build.exe

We can then select the second one in the list using Index notation like the the following (0 = first position, 1 = second position, and so on) 然后,我们可以使用索引符号选择列表中的第二个,如下所示(0 =第一个位置,1 =第二个位置,依此类推)

>$file.Name.Split('-')[1]
dev

Baking all of these concepts together into a script to get you started: 将所有这些概念结合到一个脚本中即可开始:

$files = get-childitem *.exe | Sort-Object -Descending -Property Name | Select-Object -First 2

ForEach ($file in $files){ 
    $EnvironmentSubstring = $file.Name.Split('-')[1]
    "this file $($file.Name) should go to .\$EnvironmentSubstring\"
}

Running that will give the following output: 运行该命令将产生以下输出:

this file CICone NT Setup 0.25.5-dev-build.exe should go to .\dev\
this file CICone NT Setup 0.25.4-UAT-build.exe should go to .\UAT\

From here, you just need to figure out which command to use to copy a file. 从这里,您只需要确定要使用哪个命令来复制文件。 PowerShell uses a Verb-Noun naming convention, so I'll give you a hint that you will need to learn how to use Copy-Item . PowerShell使用Verb-Noun命名约定,因此,我将向您提示您需要学习如何使用Copy-Item You can run Get-Help Copy-Item -Examples to see detailed examples on how to use each cmdlet in PowerShell by running that from the prompt. 您可以运行Get-Help Copy-Item -Examples来查看有关如何通过在提示符下运行该命令在PowerShell中使用每个cmdlet的详细示例。

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

相关问题 Powershell-将文件深移1个文件夹 - Powershell - Move files 1 folder deeper 如何将文件移动到文件夹中,然后在Powershell中将文件夹压缩 - How to move files into a folder and then zip the folder in powershell Powershell将文件解压缩到特定文件夹 - Powershell unzip files into a specific folder PowerShell-压缩文件夹中的特定文件 - PowerShell - Zip specific files in a folder Powershell:根据创建日期将文件移动到文件夹 - Powershell: Move files to folder based on Date Created 使用 PowerShell 在单独的文件夹中移动“n”个文件 - Move "n" files in Seperate Folder with PowerShell PowerShell脚本将日期作为名称的文件移动到文件夹 - PowerShell script to move files with date as name in it to a folder 将匹配特定日期的文件从服务器/文件夹数组移动到本地文件夹(Powershell) - Move files matching specific date from array of servers/folders to a local folder (Powershell) 如何使用 PowerShell 搜索特定文件名然后将该文件之后的所有文件移动到另一个文件夹 - How to search for a specific file name then move all files after that file to another folder using PowerShell PowerShell - 将特定文件从源子文件夹移动到具有相同子文件夹结构的目标 - PowerShell - Move specific files from source sub folders to destination with identical sub folder structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM