Insert the date in the filename of files in a folder

A swapping-ground for Regular Expression syntax

Insert the date in the filename of files in a folder

Postby spee » Tue Oct 09, 2018 3:33 pm

Hello,
I want to create multiple folders, one for each month of the year 2019. The following examples would be for the month of October "file month-day-year". I need 31 files for each month and I can manually delete the extras for months with less than 31 days.

"file 10-1-18"
"file 10-2-18"
"file 10-3-18"

I will manually create the folders and the files in each folder. I need help with the batch renaming of the files based on the above example. How can I achieve that?

Thanks
spee
 
Posts: 1
Joined: Tue Oct 09, 2018 3:22 pm

Re: Insert the date in the filename of files in a folder

Postby dude » Thu Oct 11, 2018 7:58 pm

There is not much to rename if you create the files yourself. Or do I miss something?

BTW1: Shouldnt it be "file 10-1-19" instead of "file 10-1-18"?
dude
 

Re: Insert the date in the filename of files in a folder

Postby dude » Mon Oct 15, 2018 6:31 pm

@spee: too bad you never replied...

I wrote something for you that auto-generated the files and folders you wanted. It was aware the length of months (including leap years).
dude
 

Re: Insert the date in the filename of files in a folder

Postby dude » Wed Oct 31, 2018 9:18 am

Code: Select all
$Year = 2019

set-location $PSScriptRoot
md "$Year"

for ($Month=1; $Month -le 12; $Month++) {
   $ThisMonth = Get-Date -Year $Year -Month $Month -Day 1
   $MonthFolder = "$PSScriptRoot\$Year\$($Month.ToString("D2")) - $($ThisMonth.ToString("MMMM"))"
   md "$MonthFolder"

   for ($Day=1; $Day -le [DateTime]::DaysInMonth($Year, $Month); $Day++) {
      New-Item "$MonthFolder\$((Get-Date -Year $Year -Month $Month -Day $Day).ToString("'file 'MM dd yy")).txt"
   }
}

dude
 

Re: Insert the date in the filename of files in a folder

Postby trm2 » Thu Jan 16, 2020 3:19 pm

Hello.

When I run your code in Test it comes up in error:

----------
$Year = 2019

set-location $PSScriptRoot
md "$Year"

for ($Month=1; $Month -le 12; $Month++) {
$ThisMonth = Get-Date -Year $Year -Month $Month -Day 1
$MonthFolder = "$PSScriptRoot\$Year\$($Month.ToString("D2")) - $($ThisMonth.ToString("MMMM"))"
md "$MonthFolder"

for ($Day=1; $Day -le [DateTime]::DaysInMonth($Year, $Month); $Day++) {
New-Item "$MonthFolder\$((Get-Date -Year $Year -Month $Month -Day $Day).ToString("'file 'MM dd yy")).txt"
}
}

------------------

Javascript Error:
SyntaxError: Unexpected identifier in : set-location SPSScriptRoot [Line:3 Start: 17 End:30]


Please assist.
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: Insert the date in the filename of files in a folder

Postby trm2 » Fri Jan 17, 2020 2:23 am

Sorry - darn screenreader messed up on error message - here is corrected error message - still need assistance:

Javascript Error:
SyntaxError: Unexpected identifier in : set-location $PSScriptRoot [Line:3 Start: 13 End:26]


Please assist. Thank you.
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: Insert the date in the filename of files in a folder

Postby Admin » Fri Jan 17, 2020 3:09 am

Hi, the script in this post is written in Powershell, it is not Javascript.
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Insert the date in the filename of files in a folder

Postby trm2 » Fri Jan 17, 2020 4:35 am

oh.

Thanks for clearing that up.

New to JavaScript - don't even recognize code = just presumed that if it wasn't RegEx it was JavaScript.
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm


Return to Regular Expressions


cron