Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save realeroberto/712c70d55f3dc9d49335 to your computer and use it in GitHub Desktop.
Save realeroberto/712c70d55f3dc9d49335 to your computer and use it in GitHub Desktop.
Add a progressive identifier to file names with embedded ISO 8601 dates.
#
# rename a bunch of files as follows
#
# YYYY-MM-DD_NAME.EXT ==> YYYY-MM-DD_nnnn_NAME.EXT
#
# where nnnn is a progressive identifier
#
# initialize the progressive identifier
$id = 0;
# for each file in the local path
Get-ChildItem -name |% {
# old filename is in the form YYYY-MM-DD_NAME.EXT (e.g. 1982-04-26_foobar.txt)
$old_filename = $_;
# extract the date part (e.g., 1982-04-26)
$trailing = $($old_filename.Substring(0, 10));
# extract the suffix part (e.g.. foobar.txt);
$leading = $_.Substring(11);
# build up the new filename (e.g. 1982-04-26_0123_foobar.txt)
$new_filename = "{0}_{1:0000}_{2}" -f $trailing, $id, $leading
# rename the file
move $old_filename $new_filename;
$id += 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment