Reputation: 427
I am just starting to get into powershell as a solution for a file collection process. Basically the gist of it is I want to loop through a directory of files that have the beginning wildcard, interrogate the julian date, and then rename the file based on that date (in Gregorian).
Right now I only have code (shown below) to rename the file based on today's date, but that is just because I am having trouble writing something advanced enough to do what I want it to do. The shortcut won't work 100% of the time. Can anyone offer any help on how to interrogate the julian date and rename?
Here is an example of how the file looks before I rename it, and how I want it to look after I rename it.
Before rename - COMPANY.DATA.J2016183.Y98173987 (the first 3 parts are constant aside from the date changing. Only the last delimited section after the dot is a random key that I don't need. The "J" is a constant as it just refers to julian. What I need to interrogate is the date after the "J").
Desired file name after renaming - DATA.2016-07-04.txt
$Today = (Get-Date).Date
$Day = $Today.AddDays(-2)
$newday = $Day.ToString("yyyy-MM-dd")
Get-ChildItem "\\fileserv\folder\COMPANY.DATA.*" -Recurse|
Rename-Item -NewName {'DATA.' + $newday + '.txt' }
(the -2 just has to do with the file name/data difference. File name is 2 days ahead of actual data).
Any help would be appreciated. Thank you,
Upvotes: 1
Views: 386
Reputation: 2312
Here is a function to convert Julian to Gregorian (source: http://powershell.com/cs/media/p/18723.aspx):
<#
ALZDBA 20120831
#>
function Convert-JulianDateFormat2Date ( [int]$YYYYddd ) {
#######################
<#
.AUTHOR
ALZDBA 20120831
.SYNOPSIS
Convert YYYYddd to [date].
.DESCRIPTION
convert a numeric Julian date format to a regular date ( date datatype )
.INPUTS
$YYYYddd e.g. 2012245
.OUTPUTS
[date]
.EXAMPLE
Clear-Host
$mydate = Convert-JulianDateFormat2Date -YYYYddd 2012263
$mydate
$mydate.dayofyear
#>
$year = [int]( $YYYYddd / 1000 )*1000
$DayOfYear = $YYYYddd - $year
$date = Get-Date -Year ( $year/1000 ) -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0
#$date.DayOfYear
$date = $date.AddDays( $DayOfYear - 1 )
$date
}
Using this, you can rename the files as follows:
$delim = "."
Get-ChildItem "\\fileserv\folder\COMPANY.DATA.*" -Recurse |
foreach {
$nameArray = $_.Name.Split($delim)
# CONVERT DATE:
$jDate = $nameArray[2].Replace("J","")
$mydate = Convert-JulianDateFormat2Date -YYYYddd $jDate
$newDate = $mydate.ToString("yyyy-MM-dd")
#RENAME FILES:
$newName = $nameArray[1]+$delim+$newDate+$delim+$nameArray[4]
Rename-Item $_ -NewName $newName
}
Upvotes: 2
Reputation: 164
Ryan Drane has a great post on this problem here: http://www.ryandrane.com/2015/04/convert-julian-date-to-gregorian-date-in-powershell/
His approach, modified for your application, would look something like this:
$filename = "COMPANY.DATA.J2016183.Y98173987"
$JulianYear = $filename.Substring(14,4)
$JulianDay = $filename.Substring(18,3)
$GregorianDate = (Get-Date -Year $JulianYear -Month 01 -Day 01).AddDays($JulianDay - 1)
Although that code does return the gregorian date with the time attached, it shouldn't be much work to remove that, and add in your file handling method.
Upvotes: 0