Manish Kumar Sharma
Manish Kumar Sharma

Reputation: 13432

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). 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

Can anyone tell me the commands for powershell to do that?

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.

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.

Upvotes: 0

Views: 175

Answers (1)

karthik manchala
karthik manchala

Reputation: 13640

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 matches the pattern 000(any digit)(any digit) i.e.. till 00099
  • 001\d\d matches 001(any digit)(any digit) i.e from 00100 to 00199
  • 002[0-4]\d matches 002(any digit from 0 to 4)(any digit) i.e from 00200 to 00249`
  • 00250 matches literal 00250
  • | or operator.. to match any of the above specified patterns

Upvotes: 2

Related Questions