Musings of a dad with too much time on his hands and not enough to do. Wait. Reverse that.

Tag: powershell (Page 4 of 7)

Grouping and counting by date

Subtitle: In one line

The other day at work, I had a file of a bunch of activities and the dates on which they occurred.  Something like this:

What I wanted to do was to get a count of the number of activities occurring on each day, so I fired up a PowerShell command shell and ran the following one-liner:


1
ipcsv "C:\my_path\test_data.csv"|group event_date|select Name, Count

That line produced these results:


1
2
3
4
5
6
7
8
Name       Count
----       -----
8/23/2018     10
9/19/2018      8
7/3/2018      18
10/13/2018    15
11/2/2018      9
10/1/2018     15

Nice.  I have my counts, but it’d be even better if my dates were in order.  Let’s try to sort those suckers:


1
ipcsv "C:\my_path\test_data.csv"|sort event_date|group event_date|select Name, Count

That produced:


1
2
3
4
5
6
7
8
Name       Count
----       -----
10/1/2018     15
10/13/2018    15
11/2/2018      9
7/3/2018      18
8/23/2018     10
9/19/2018      8

Not the kind of sorting I was looking for.  It sorted the dates like they were strings.  Wait a minute…


1
2
3
4
5
PS C:\WINDOWS\system32> (ipcsv "C:\my_path\test_data.csv")[0].event_date.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                          
-------- -------- ----                                     --------                                                                                                                          
True     True     String                                   System.Object

Just as I suspected: PowerShell is importing those dates as strings!  If I want to sort my results by date, I’ll have to cast that date to a proper datetime data type first.  Here’s what I came up with:


1
ipcsv "C:\my_path\test_data.csv"|select -Property @{Name="event_dt"; Expression={[datetime]$_.event_date}}|sort event_dt|group event_dt|select Name, Count

And the results:


1
2
3
4
5
6
7
8
Name                   Count
----                   -----
7/3/2018 12:00:00 AM      18
8/23/2018 12:00:00 AM     10
9/19/2018 12:00:00 AM      8
10/1/2018 12:00:00 AM     15
10/13/2018 12:00:00 AM    15
11/2/2018 12:00:00 AM      9

Nice!  I generated my counts and sorted by date all in one line of code.  Sweet!

Scanning slides

My dad was a big fan of slides as a film medium back in the day.  I have boxes of these suckers that span at least the last four decades of the Twentieth Century.

A few years ago, I bought one of those slide scanner gizmos and started in on the overwhelming task.  One thing I immediately found frustrating was the timestamp: my scanner would affix a timestamp–June 1, 2013–to every slide.  I examined every nook and cranny of the menu to see where I could set the current time and date and found no way to set it.

To me, having an accurate timestamp on my scanned photos is important–it lets me know, and anyone else with whom I share my files, when I actually did the work.  It can help me group images together that may all be part of the same set.  It might even help me identify unknown individuals in the photos.   So, it’s important to get the date right on the files.

What can I do?  I know: PowerShell can solve this problem!  Here’s a short code snippet I now use to get the dates right on my scanned slides:


1
2
$dir = "C:\my_path\slides"  # set the filepath to my slides
gci $dir | where {$_.Extension.ToLower() -eq ".jpg"} | foreach{$_.CreationTime = (Get-Date); $_.LastWriteTime = (Get-Date)}

This will at least set the image timestamp to the current date and time.  Assuming you scanned the slides on the same day, you should be set.  If you want to set the timestamp to an earlier date, that can easily be done, as well, but I won’t go into it here.  Also know that there are a number of date properties with a file: create date, last modified date, etc.  I’m using a “hammer” approach to this problem by setting both the CreationTime and LastWriteTime properties.

My scanner also has a pre-set naming convention for the slides it scans.  In general, that’s fine with me, but you can also use PowerShell to easily rename your scanned images to your own convention.  Here, I want to indicate that all the slides in a given directory are part of the same group, so I basically add a “_grp001” suffix to the end of each file:


1
2
3
$dir = "C:\data_files\slides2"
$suffix = '_grp001.jpg'
gci $dir | where {$_.Extension.ToLower() -eq ".jpg"} | foreach{mv $_.FullName $_.FullName.ToLower().Replace('.jpg', $suffix)}

 

Music to drive by, Part 3

In a couple of previous posts, I proposed a couple of ways to easily copy all or a portion of your music library to a thumb drive for playing in your usb-enabled automobile.  You can check out my solution at my Github page.

Recently, though, I encountered yet another frustration: my “copy” script iterates through a JSON file of the inventory of my music.  In the file, my music is listed in alphabetical order by the artist.  So, I copy my music to my thumb drive in alphabetical order.  Which means I’ll get Aerosmith on my thumb drive, but will likely never get ZZ Top.  Bummer!  So, I came up with a great solution: Get-Random!

All I need to do is alter one line in my “copy” script:


1
2
# apply my selection criteria and get a list of the songs to copy over to the flashdrive
$mp3s_to_write_to_drive = $mp3_col | where {$genres_i_want -contains $_.genre} | where {$bands_to_skip -notcontains $_.artist} | sort {Get-Random}

I just need to pipe my $mp3_col collection object to “sort {Get-Random}”.  This will sort the mp3 files randomly such that they’ll be copied to my thumb drive in a random order.  Cool!

I’ll probably not update my script in Github with this minor change, but just tack this little command at the end of the line in your downloaded copy of the script and you’ll be set.

« Older posts Newer posts »

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑