-3

How can I use PowerShell to copy a range of files where the file name is as sequence of numbers?

For example, say I have a bunch of files where the names are numbers starting at 23540987577 to 27495847547388. However I only want to copy files where the middle 5 numbers are between 43565 and 43769. I have made a few attempts, but it either copies everything, or errors out.

So far I have the following :

    $START = read-host -prompt "Enter starting number"
$END = read-host -Prompt "Enter ending number"


$Files = Get-ChildItem
$Files = $Files.name
$i = 1
foreach ($i in $Files) {
if ($Files[$i] -ge "*$START*" -and $Files[$i] -le "*$END*") {
/
Copy-Item $Files[$i] .\pulled
$i++
}
else {
Write-Host "no"

}
}

I have a list of files where the file name is a large sequence of numbers. Within said sequence (some where towards the middle) is a transaction number. I need to find and copy a small subset of transaction numbers that are within a specific range.

If I am searching for said files manually in Windows Explorer I would have search for each number in the range as follows:

*43565*
*43566*
*43567*
*43568*

and so on...

I want to automate this process as it takes a long time to search for each transaction number with larger batches.

  • what are the middle five numbers in `27495847547388`? 58475 or 84754? – Kory Gill May 31 '18 at 00:54
  • In reality it could be any of them. I simply want all files that contain a number in the range within the file name, but not the first or last digit. So when I am doing it manually, I search in windows explorer for each number in the sequence one by one using the asterisk wildcard. For example *43565*, *43566*,*43567*, and so on. I then have to manually copy the desired files. I want to automate this repetitive task. – Johnathan Rubinsky May 31 '18 at 01:46
  • The asterisk would be at both the beginning and end of each number in the sequence when I search manually in explorer. – Johnathan Rubinsky May 31 '18 at 02:05
  • I don't understand the description of what you're trying to do. – Bill_Stewart May 31 '18 at 21:30
  • Ok so I have a large group of files. The file names are a large string of numbers. Buried with in the middle of said string is a transaction number. I want to copy all files within a range of transaction numbers to another folder. Basically I am writing a script that asks for the beginning of the range of transaction numbers, and the end of said batch. The script should then be able to examine the files and copy the files that contain the smaller transaction number within the larger string that makes up the file name. – Johnathan Rubinsky Jun 01 '18 at 05:39

1 Answers1

0

what about the following:

it's not regex but I dont see any advantage of using regex here:

I didn't use much Powershell yet, therefore I stick to Pseude Code, but you should be able to adapt this to Powershell script easily:

CopyFiles(int lowerBound, int upperBound)
{
    foreach (file in fileList)
    {
         int filename = (int)file.filename.substring(1,X) // you have to know the length of your substring, maybe pass it also as parameter
         if(filename >= lowerBound && filename <= upperBound)
         {
             move (file.filename, new location)
         }
    }
}
misanthrop
  • 771
  • 7
  • 32