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