开发者

Delete Directories based on time modified

I am using Powershell scripts to deploy the codebase on our remote servers.

One Major part of the script copies the current release to the server. Now I just need to keep the last two releases on the remote server and delete all others.

I Need to keep the latest two releases

Eg: In the remote server, I have

//server001/Application/
    Build_1_0_0_19
    Build_1_0_0_18
    Build_1_0_0_17
    Build_1_0_0_16

I need to clear Builds _17 and _16 while deploying _19.

We can sort out the directories according to the time modified and the last two will come on top. Rest all are not required.

Can this be done through Powershell Scripts ?

P.S. The builds are 开发者_运维问答not always in sequential order


You can do something like this:

#requires -version 2
Get-ChildItem //server001/Application/|
    Sort-Object CreationTime -Descending|
    Select-Object -Skip 2|
    Remove-Item -Recurse -Confirm

Just remove the -Confirm switch once you are sure that it does what you want.

Here is a v1 compatible method:

$dirs = @(Get-ChildItem //server001/Application/)

$dirs|
    Sort-Object CreationTime -Descending|
    Select-Object -Last ($dirs.Count - 2)|
    Remove-Item -Recurse -Confirm
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜