Read first line of text in txt file them cut charters 5-10 to a variable

This looks great.
Thanks!!!

One quick question.
the 3 files I am dealing with have a little different naming convention. Sorry I was a little too genaric on my sample script.
The 3 files are more like
FileP.txt.1234567
FileD.txt.1234567
FileR.txt.1234567

So this is how I am thinking to update the script

Get-ChildItem $FileLocation | 
Where-Object {$_.name -match "File[PDR]\.txt\..*"} |
ForEach-Object {
    Switch ($_.name[4]) {
        P {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(32,8) } }
        D {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(43,8) } }
        R {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(31,8) } }
    }
    [PSCustomObject]@{'Was' = $($_.Name); 'Now' = $($_.Name -replace "\.\w*$", ".$Date")}
    Rename-Item -Path $_.FullName -NewName $($_.Name -replace "\.\w*$", ".$Date")
}

That looks good. You can also remove the [pscustomobject] line if you like. I just put it there to see some output in the console whil testing.

Getting this error when I run the script

Get-Content : Cannot bind argument to parameter ‘Path’ because it is null.

Thanks!!

looks like it did not find matches based on the path and or file names check the path variable and the file names to insure the regex is looking for the right file

Here is my code but still getting the error.

$FileLocation = "D:\Files"

Get-ChildItem $FileLocation | 
Where-Object {$_.name -match "File[PDR]\.txt\..*"} |
ForEach-Object {
    Switch -regex ($_.name[4]) {
        "P" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(32,8) } }
        "D" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(43,8) } }
        "R" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(31,8) } }
    }
    [PSCustomObject]@{'Was' = $($_.Name); 'Now' = $($_.Name -replace "\.\w*$", ".$Date")}
    Rename-Item -Path $_.FullName -NewName $($_.Name -replace "\.\w*$", ".$Date")
}

It appears you may be running an out dated version of PowerShell. The code works perfectly for me in PoSh 3.0 and 4.0, but gives your error in 2.0. You should upgrade.

Ahh. If upgrading where only an option :wink:

Ok then, here is a workaround that is PoSh 2.0 compatible.

$FileLocation = "D:\Files"

Get-ChildItem $FileLocation | 
Where-Object {$_.name -match "File[PDR]\.txt\..*"} |
ForEach-Object {
    $input = $_
    Switch -regex ($_.name[4]) {
        "P" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(32,8) } }
        "D" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(43,8) } }
        "R" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(31,8) } }
    }
    New-Object -Type PSObject -Property @{'Was' = $($_.Name); 'Now' = $($_.Name -replace "\.\w*$", ".$Date")}
    Rename-Item -Path $_.FullName -NewName $($_.Name -replace "\.\w*$", ".$Date")
}

This is really close.
It is working for the FileD but it is using the same bit of code for the other 2 files.

Any suggestions?

Thanks!!!

The switch is looking at the 5th character in the file names could it be a case issue? Unless the $date variable is no being set after the first pass.

Clear the $date variable after the foreach

ForEach-Object {
$date = $null

If it still sets the date the same then it is selecting switch D for each pass

Never mind…

Cam, you should start a separate post.

Nathan, can you give some actual examples of the 3 file names and the first line of each file?

Due to the project I am working on I can’t give exact names of these files but I can say the each file has the same number of charters.
XXXXXXXP.txt.12345678912345678
XXXXXXXD.txt.12345678912345678
XXXXXXXR.txt.12345678912345678

Thanks so much for the help. The Powershell Community is one of the best there is :slight_smile:

is the XXXXXXX the same name for all files how many characters is the XXXXXXX

It is the same. Each X represents one of the characters of the file name.
The P, D and R are the non common characters and the charaters following the txt. are a time stemp for when the fire was created.

Thanks

the 4 in his Switch is looking at the PDR in your file 5th character if your actual file has seven characters before the PDR you need to change the number to 7. Chances are your 5th character in all your file names is a D and that is why it is selecting that switch for every file

$FileLocation = "D:\Files"

Get-ChildItem $FileLocation | 
Where-Object {$_.name -match "File[PDR]\.txt\..*"} |
ForEach-Object {
    $input = $_
    Switch -regex ($_.name[7]) {
        "P" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(32,8) } }
        "D" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(43,8) } }
        "R" {$Date = Get-Content -Path $input.FullName -TotalCount 1 | ForEach-Object { $_.Substring(31,8) } }
    }
    New-Object -Type PSObject -Property @{'Was' = $($_.Name); 'Now' = $($_.Name -replace "\.\w*$", ".$Date")}
    Rename-Item -Path $_.FullName -NewName $($_.Name -replace "\.\w*$", ".$Date")
}

Perfection!!!

THANKS!!!