user3801599
user3801599

Reputation: 15

Trim the last 8 characters in Powershell

I have this code:

$folder = "C:\Folder"
Get-ChildItem $folder | ForEach { 
  .\hi-data-upload-utility uploadDataSetFile `
      -said *********************** `
      -sas ************************ `
      -sid ************************ `
      -dsid &&&&_MCK_STAR_EDI_837_5010_INST `
      -sv 1 `
      -fid SINGLE_FILE `
      -rl 20160116 `
      -f $($_.fullname) `
      -re ‘837 Institutional Claims’ 
  }

How do I trim the last 8 digits from the $($_.fullname). Excluding the extension .c5? I just need the date part after the _STAR. The files in the folder are all in this format:

00037888_STAR_08302017.c5

Upvotes: 1

Views: 1814

Answers (3)

Ketanbhut
Ketanbhut

Reputation: 506

Basename property will provide name without extension, as mentioned by Richard. You can use substring method to fetch 8 characters, as below example will display dates (last 8 chars) from files' base names.

dir *.c5 | %{$_.BaseName.substring(($_.BaseName.length)-8)}

Also, a quick check on what is provided by the object

dir *.c5 |select -First 1 | select *

Upvotes: 1

user6811411
user6811411

Reputation:

I had an answer for your yesterdays question you deleted to soon for me to post.

Just a warning, also deleted Q&A can be seen once you have a score of 10k.

I suggest to use splatting to avoid endless command lines. To upload a file with a different name you will first need to rename the file.

Only if the date from LastWriteTime is different from file appendum it is renamed and uploaded For testing purposes I only view the command with EchoArgss from pscx

## Q:\Test\2018\06\05\SO_50701695.ps1
$FilePath = "C:\Users\AG053989\hi-data-upload-utility-1.6\bin\STAR\"
$Files = Get-ChildItem -Path $FilePath -Filter *.c5 -File 

ForEach($File in $Files) {
    if ($File.BaseName -Match '(\d{8}_STAR_)(\d{8})' ){
        $FileDate = $File.LastWriteTime.ToString('ddMMyyyy')
        if ($FileDate -ne $Matches[2]){
            $NewName = $Matches[1]+$FileDate+$File.Extension
            Rename-Item $File -NewName $NewName
            $UpLoad = Join-Path $FilePath $NewName

            $parms = @{
                'said'= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                'sas' = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                'sid' = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
                'dsid'= 'CHLD_DC_MCK_STAR_EDI_837_5010_INST'
                'sv'  = '1' 
                'fid' = 'SINGLE_FILE'
                'rl'  = $FileDate
                'f'   = $UpLoad
                're'  = 'McKesson Star 837 Institutional Claims'
            }
            EchoArgs .\hi-data-upload-utility uploadDataSetFile @parms
        }
    }
}

Dir before, script output, dir after:

> ls *.c5
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-06-06     20:03             56 00038307_STAR_05062018.c5

> Q:\Test\2018\06\05\SO_50701695.ps1
Arg 0 is <.\hi-data-upload-utility>
Arg 1 is <uploadDataSetFile>
Arg 2 is <-re:McKesson Star 837 Institutional Claims>
Arg 3 is <-sid:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Arg 4 is <-sv:1>
Arg 5 is <-dsid:CHLD_DC_MCK_STAR_EDI_837_5010_INST>
Arg 6 is <-f:Q:\Test\2018\06\05\00038307_STAR_06062018.c5>
Arg 7 is <-said:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Arg 8 is <-fid:SINGLE_FILE>
Arg 9 is <-rl:06062018>
Arg 10 is <-sas:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>

Command line:
"EchoArgs.exe" .\hi-data-upload-utility uploadDataSetFile -re:"McKesson Star 837 Institutional Claims" -sid:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -sv:1 -dsid:CHLD_DC_MCK_STAR_EDI_837_5010_INST -f:Q:\Test\2018\06\05\00038307_STAR_06062018.c5 -said:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -fid:SINGLE_FILE -rl:06062018 -sas:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

> ls *.c5
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-06-06     20:04             56 00038307_STAR_06062018.c5

Upvotes: 0

Richard
Richard

Reputation: 109025

The result from Get-ChildItem includes the property BaseName which is the filename without extension.

A range can be passed to the index operator on strings, with negative numbers measuring from the end (-1 is the last character) returning an array of characters, which can of course me merged.

So:

($_.BaseName[-8..-1] -join '')

should do it.

Upvotes: 4

Related Questions