Author Posts

April 3, 2016 at 4:40 am

Hi Mates,

I need one quick help with a scenario..

I have 2 files named ABC123.txt and ABC456.txt. I want powershell to read the file names and pick the 3 integers from them and store into variables. So the Variables should be

$X="123"
$Y="456"

Many thanks in advance 🙂

Cheers,
Narayan

April 3, 2016 at 6:32 am

$data =Get-ChildItem .\abc123.conf
$D=$data.name.substring(5,3)
$V1=$D.GetValue(0)
$V1=$D.GetValue(1)

April 3, 2016 at 4:05 pm

$files = Get-ChildItem -Path .\ABC123.txt,ABC456.txt
# Create variables
switch -Regex ($files){
'\d+' {New-Variable -Name $_.BaseName -Value $Matches[0] -Verbose}
}

#VERBOSE: Performing the operation "New variable" on target "Name: abc456 Value: 456".
#VERBOSE: Performing the operation "New variable" on target "Name: abc123 Value: 123".