Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Modify the Powershell script to rename file based on date input

In Powershell, I would like to rename a file called "9.pdf" at C:\Attachment\Folder to the following date format October 16, 2018.pdf....

Only after the following script has run, with the purpose of inserting the new script at the end of this script if possible..  
And, I would like for it to pull the Date from the Date input which is found in the following script..


Get-ChildItem 'C:\Attachment\Folder' -Filter *.pdf | Where-Object {$_.BaseName -ne '9'} | Remove-Item -Force

Function Select-DateForm {
[CmdletBinding()]
Param(
	[String]$Title,
	[String]$Prompt,
	[DateTime]$MinDate,
	[DateTime]$MaxDate
)
	Function Show-FormMain {
		Add-Type -AssemblyName System.Drawing
		Add-Type -AssemblyName System.Windows.Forms
		[System.Windows.Forms.Application]::EnableVisualStyles()

		$formMain = New-Object -TypeName System.Windows.Forms.Form
		$formMain.ClientSize = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 250, 250
		$formMain.FormBorderStyle = 'FixedSingle'
		$formMain.MaximizeBox = $false
		$formMain.MinimizeBox = $true
		$formMain.Text = If ($Title) {$Title} Else {'Date Selection'}
		
		$labelPrompt = New-Object -TypeName System.Windows.Forms.Label
		$labelPrompt.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 16, 8
		$labelPrompt.AutoSize = $true
		$labelPrompt.Text = If ($Prompt) {$Prompt} Else {'Please select a date:'}
		$formMain.Controls.Add($labelPrompt)

		$monthCalendar = New-Object -TypeName System.Windows.Forms.MonthCalendar
		$monthCalendar.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 16, 32
		$monthCalendar.MaxSelectionCount = 1
		If ($MinDate) {$monthCalendar.MinDate = $MinDate}
		If ($MaxDate) {$monthCalendar.MaxDate = $MaxDate}
		$formMain.Controls.Add($monthCalendar)
		
		$buttonOK = New-Object -TypeName System.Windows.Forms.Button
		$buttonOK.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 40, 208
		$buttonOK.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 72, 25
		$buttonOK.Text = '&OK'
		$buttonOK.add_Click({
			Param($Sender, $EventArgs)
				$Script:returnValue = $monthCalendar.SelectionStart
				$formMain.Close()
			})
		$formMain.Controls.Add($buttonOK)
		
		$buttonCancel = New-Object -TypeName System.Windows.Forms.Button
		$buttonCancel.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 120, 208
		$buttonCancel.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 72, 25
		$buttonCancel.Text = '&Cancel'
		$formMain.Controls.Add($buttonCancel)

		$formMain.CancelButton = $buttonCancel
		$formMain.AcceptButton = $buttonOK
		
		[void]$formMain.ShowDialog()
	}
	Show-FormMain
	Return $returnValue
}

$maxDays = 60
$reportDate = Select-DateForm -Prompt 'Select the Report Date, then click OK:' -MinDate ([DateTime]::Now.AddDays(-1 * $maxDays)) -MaxDate ([DateTime]::Now)
If (-not $reportDate) {
      Exit
}
$reportDate

###SETUP START###
#-------DO NOT MODIFY-------#
      #Yesterdays Date
            $date = $reportDate.ToString("MMM'/'dd'/'yyyy")
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
    #Mail Subject
        $Subject = "Report: "+$date+" "
    #Body of Email
        $MessageBody = "Attached is the report for $date. Thank you."
###USER VARIABLES END###


###PROGRAM START###
	# Invokes the Send-MailMessage function to send notification email
	$splat = @{
		From =                  'email1@email.com'
		To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'List.txt')
		SmtpServer =      'smtp.server.com'
		Subject =	  $subject
		body =		  $MessageBody
		BodyAsHtml =	$true
		Attachment =	'C:\Attachment\Folder\9.pdf'
	}
	Send-MailMessage @splat
###PROGRAM END###

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

Thank you.