1

I need to move files, which satisfy specific conditions, from folder A to a specific subfolder in folder B.

The conditions are:

  • Group the files that contain .exe and take the two with the highest number.

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

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). enter image description here


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
}
Devid
  • 1,823
  • 4
  • 29
  • 48
  • 3
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Apr 25 '18 at 14:29
  • 4
    stackoverflow is not a script writing service. We are here to help you with specific discrete problems that you have with code that you have written. What specific problem are you having with the code that you have written? – EBGreen Apr 25 '18 at 14:29
  • @JeffZeitlin When I come back home I will provide a code that I am working on. It is not failing but as I am not familiar with scripting languages it is not complete. But if I come with a solution before someone provides some help here I will of course write my solution here. – Devid Apr 25 '18 at 14:35

1 Answers1

4

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.

>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 = 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. We can use the .Split() method to break a string up on each instance of a character. 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)

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

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • Nice answer. Just version number sorting get's difficult when the number of places in subversion change i.e. `0.25.9` --> `0.25.10` –  Apr 25 '18 at 16:50
  • 1
    True, to handle this, he could require strict numbering, like 0.25.09, for instance. – FoxDeploy Apr 25 '18 at 20:49
  • Difficult, not impossible. [here](https://stackoverflow.com/a/5429048/6811411) is a way described to sort with all numbers expanded to fixed length with leading zeroes on the fly. Very intersting. –  Apr 25 '18 at 21:06
  • @FoxDeploy thanks. Here is a little fix as I don't want to group by files ending with .exe but group by files containing .exe I did this: `$files = get-childitem | Where-Object {$_.Name -like "*.exe*"} | Sort-Object -Descending -Property Name | Select-Object -First 2` – Devid Apr 25 '18 at 21:17