9

I have a directory containing numbered directories:

Archive
 |-1
 |-2
 |-3
 |-...

I need to create the next directory numerically. For which I am currently doing

$lastArchive = ls .\Archive | sort Name | select -Last 1
$dirName = '1'
if($lastArchive) {
  $dirName = ([int]$lastArchive.Name)+1
}

This of course fails once we get to 10 which by sorting rules follows after 1 not 9. I need the sort expression to actually be [int]$_.Name - how would I do this?

1 Answer 1

21

I think you need to change that first line as follows:

$lastArchive = ls .\Archive | 
               Sort-Object -property @{Expression={[int]$_.Name}} | 
               Select-Object -Last 1

Then, you can create the next directory in numerical order like this:

mkdir ([int]$lastArchive.Name + 1).ToString()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.