125

I have a list of directories with numbers. I have to find the highest number and and increment it by 1 and create a new directory with that increment value. I am able to sort the below array, but I am not able to increment the last element as it is a string.

How do I convert this below array element to an integer?

PS C:\Users\Suman\Desktop> $FileList

Name
----
11
2
1

7 Answers 7

238

You can specify the type of a variable before it to force its type. It's called (dynamic) casting (more information is here):

$string = "1654"
$integer = [int]$string

$string + 1
# Outputs 16541

$integer + 1
# Outputs 1655

As an example, the following snippet adds, to each object in $fileList, an IntVal property with the integer value of the Name property, then sorts $fileList on this new property (the default is ascending), takes the last (highest IntVal) object's IntVal value, increments it and finally creates a folder named after it:

# For testing purposes
#$fileList = @([PSCustomObject]@{ Name = "11" }, [PSCustomObject]@{ Name = "2" }, [PSCustomObject]@{ Name = "1" })
# OR
#$fileList = New-Object -TypeName System.Collections.ArrayList
#$fileList.AddRange(@([PSCustomObject]@{ Name = "11" }, [PSCustomObject]@{ Name = "2" }, [PSCustomObject]@{ Name = "1" })) | Out-Null

$highest = $fileList |
    Select-Object *, @{ n = "IntVal"; e = { [int]($_.Name) } } |
    Sort-Object IntVal |
    Select-Object -Last 1

$newName = $highest.IntVal + 1

New-Item $newName -ItemType Directory

Sort-Object IntVal is not needed so you can remove it if you prefer.

[int]::MaxValue = 2147483647 so you need to use the [long] type beyond this value ([long]::MaxValue = 9223372036854775807).

Sign up to request clarification or add additional context in comments.

1 Comment

and if it is a long number use [long]"232312333123"
5

Example:

2.032 MB (2,131,022 bytes)

$u=($mbox.TotalItemSize.value).tostring()

$u=$u.trimend(" bytes)") #yields 2.032 MB (2,131,022

$u=$u.Split("(") #yields `$u[1]` as 2,131,022

$uI=[int]$u[1]

The result is 2131022 in integer form.

Comments

1

Use:

$filelist = @(11, 1, 2)
$filelist | sort @{expression={$_[0]}} | 
  % {$newName = [string]([int]$($_[0]) + 1)}
  New-Item $newName -ItemType Directory

Comments

1

Use:

$filelist = @("11", "1", "2")
$filelist | sort @{expression={[int]$_}} | % {$newName = [string]([int]$_ + 1)}
New-Item $newName -ItemType Directory

Comments

0

If someone is looking for how this can be run from command line, as a single command, this is one way it can be done:

$FileList | ` # Writes array to pipeline
Select-Object -Last 1 | ` # Selects last item in array
ConvertFrom-String -TemplateContent "{[int]NameTmp:12}" | ` # Converts string to number and names the variable "NameTmp"
Add-Member -Name "Name" -Value { $this.NameTmp + 1 } -MemberType ScriptProperty -PassThru | ` # Increments variable "NameTmp" by one and adds new variable named "Name" to pipeline object
New-Item -Type Directory # Creates new directy in current folder. Takes directory name from pipelined "Name" variable

Comments

0

Ah, PowerShell. Here is the magic set of syntax to do it with a reasonably simple and comprehensible pipeline:

Starting with

$FileList = @([PSCustomObject]@{ Name = "11" }, [PSCustomObject]@{ Name = "2" }, [PSCustomObject]@{ Name = "1" })

This will convert the objects into a sequence of integers:

$FileList `
  | Select-Object -ExpandProperty Name ` #instead of objects with a 'name' property, just use the name property as the object
  | ForEach-Object { ([int]$_) } #convert each value into an int

Given that, you can get the next number by sorting, taking one element, and adding 1 to that element, in a similar fashion (new line added only for clarity)

#as above
$FileList `
  | Select-Object -ExpandProperty Name ` 
  | ForEach-Object { ([int]$_) } `

  | Sort-Object -Descending ` 
  | Select-Object -First 1 ` #grab only the maximum number
  | ForEach-Object { $_ + 1 }; #increment that maximum to the next number

You can further take that pipeline and use it to generate the file name from the number, and probably pipe that into a call to create the file/directory

Comments

-3

Once you have selected the highest value, which is "12" in my example, you can then declare it as integer and increment your value:

$FileList = "1", "2", "11"
$foldername = [int]$FileList[2] + 1
$foldername

3 Comments

I think OP wants to have the script find the highest value
Sorry but your answer assumes the array/list always has 3 elements, which is not really flexible.
... and creating a directory. Outputting the directory name will not magicly create it : )

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.