Home > database >  Rename a file depending on hour created in powershell
Rename a file depending on hour created in powershell

Time:09-20

I have a couple of CSV files I have to merge, and purge of duplicates. The merging and purging I figured out. When I run my script it creates a new file in ./merged/ folder called merged.csv It then gets rid of duplicate barcodes and creates a new CSV called cleaned.csv in the working folder.

What I am having a problem with is renaming this cleaned.csv file as morning.csv, afternoon.csv, night.csv depending on the time it was created, i.e if created before 13:00 morning.csv, between 13:00-19:00 afternoon.csv, and any later night.csv.

#This line removes duplicate entries from the merged.csv and creates a new file called cleaned.csv in the working folder.
$inputCsv = Import-Csv ./merged/merged.csv | Sort-Object Barcode -Unique
$inputCsv | Export-Csv cleaned.csv -NoTypeInformation

Is there an easy way to do this in PS?

CodePudding user response:

All you need to do is assign the filename based off of the time.

# This line removes duplicate entries from the merged.csv and
# creates a new file called cleaned.csv in the working folder.
$inputCsv = Import-Csv ./merged/merged.csv | Sort-Object Barcode -Unique
$thisHour = [DateTime]::Now.Hour
$exportFileName =  # We can assign $exportFieldName based off a conditional
    if ($thisHour -le 13) { # If the hour was less then 13
        "morning.csv"       # it's morning
    } elseif ($thisHour -le 18) {  # If it was less than 18
        "afternoon.csv"     # it's afternoon
    } else {                # otherwise
        "night.csv"         # it's night.
    }
$inputCsv | Export-CSV -Path $exportFileName -NoTypeInformation

CodePudding user response:

Possibly try something like this

if (get-date -format "HH") > 19
    $inputCsv | Export-Csv evening.csv -NoTypeInformation
elseif (get-date -format "HH") >= 13 and (get-date -format "HH") <= 19
    $inputCsv | Export-Csv afternoon.csv -NoTypeInformation
else
    $inputCsv | Export-Csv morning.csv -NoTypeInformation

You probably should set the output of get-date -format "HH" to a variable and work off of that. But I think the "format "HH"" is the part you may be looking for

CodePudding user response:

As per my comment.

$CurrentHour = (Get-Date -DisplayHint Time).Hour

switch ($CurrentHour)
{
    {$CurrentHour -lt 13}                          {'Renaming file to to Morning.csv'}
    {$CurrentHour -gt 13 -and $CurrentHour -lt 19} {'Renaming file to to Afternoon.csv'}
    {$CurrentHour -ge 20}                          {'Renaming file to to Night.csv'}
}

Renaming file to Morning.csv

 $CurrentHour = (Get-Date -DisplayHint Time).Hour   3

switch ($CurrentHour)
{
    {$CurrentHour -lt 13}                          {'Renaming file to to Morning.csv'}
    {$CurrentHour -gt 13 -and $CurrentHour -lt 19} {'Renaming file to to Afternoon.csv'}
    {$CurrentHour -ge 20}                          {'Renaming file to to Night.csv'}
}

Renaming file to Afternoon.csv

 $CurrentHour = (Get-Date -DisplayHint Time).Hour   8

switch ($CurrentHour)
{
    {$CurrentHour -lt 13}                          {'Renaming file to to Morning.csv'}
    {$CurrentHour -gt 13 -and $CurrentHour -lt 19} {'Renaming file to to Afternoon.csv'}
    {$CurrentHour -ge 20}                          {'Renaming file to to Night.csv'}
}


Renaming file to Night.csv
  • Related