简体   繁体   English

复制名称范围内具有数字的文件

[英]Copying files with the names having a number within a range

I am unfamiliar with Powershell (besides its existence and way to run a powershell commands). 我不熟悉Powershell(除了它的存在以及运行powershell命令的方式)。 I have a folder source . 我有一个文件夹 Inside that folder I have several files: 在该文件夹中,我有几个文件:

image_001.jpg
image_002.jpg
...
...
image_250.jpg
...
...
...
image_8000.jpg

Out of these I want to select specific images, say from image_00001.jpg - image_00250.jpg and copy them to destination folder say, destination 从这些中,我想选择特定的图像,例如从image_00001.jpg-image_00250.jpg并将其复制到目标文件夹,例如destination

Can anyone tell me the commands for powershell to do that? 谁能告诉我powershell的命令吗?

I apologize for such a noob question. 对于这个菜鸟问题,我深表歉意。 I don't know a thing about powershell though my intuition says this might be the easiest task to achieve. 尽管我的直觉说这可能是最简单的任务,但我对powershell一无所知。

Note: I tried to understand the other answer about doing similar task but due to lack of my knowledge of regex , I was unable to comprehend it. 注意:我试图了解有关执行类似任务的其他答案,但是由于缺乏对正则表达式的了解,我无法理解它。

You can do the following: 您可以执行以下操作:

cls

$source = "<your source>"
$destination = "<your destination>" 

foreach ($i in Get-ChildItem -Path $source -Recurse)
{
    if ($i.Name -match "image_(000\d\d|001\d\d|002[0-4]\d|00250)\.jpg")
    {
        Copy-Item -Path $i.FullName -Destination $i.FullName.Replace($source,$destination).Trim($i.Name)
    }
}

Explanation for regex: 正则表达式说明:

000\\d\\d|001\\d\\d|002[0-4]\\d|00250 matches the numbers from 00000-00250 000\\d\\d|001\\d\\d|002[0-4]\\d|00250 00000-00250匹配00000-00250中的数字

  • 000\\d\\d matches the pattern 000(any digit)(any digit) ie. 000\\d\\d匹配模式000(any digit)(any digit)即。 till 00099 直到00099
  • 001\\d\\d matches 001(any digit)(any digit) ie from 00100 to 00199 001\\d\\d匹配001(any digit)(any digit)即从00100 to 00199
  • 002[0-4]\\d matches 002(any digit from 0 to 4)(any digit) ie from 00200 to 00249` 002[0-4]\\d匹配002(any digit from 0 to 4)(any digit)即00200到00249`
  • 00250 matches literal 00250 00250匹配文字00250
  • | or operator.. to match any of the above specified patterns 或运算符..以匹配任何上述指定模式

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM