Home > other >  PowerShell to add date to all filenames in a folder
PowerShell to add date to all filenames in a folder

Time:09-10

I have three files named First_File.txt Second_File.txt Thrid_File.txt in a folder named C:\01Source I want to add the current date to the filenames so they look like this: First_File_20220909.txt

The terminal output is this:

What if: Performing the operation "Rename File" on target "Item: C:\01Source\First_File.txt Destination: C:\01Source\First_File_20220909.txt". (runs for all three files names)

BUT... when I look in the C:\01Source folder the filenames are not changed, they are still First_File.txt

What did I miss?

This is my code:

$Path = "C:\01Source\"
write-host $Path
$curDateTime = Get-Date -Format yyyyMMdd
Get-ChildItem $Path -Recurse |
  Rename-Item -NewName {$_.Basename   '_'   $curDateTime   $_.Extension } -WhatIf

CodePudding user response:

Abraham Zinala has provided the crucial pointer:

  • The -WhatIf common parameter in the command above only previews the operation without actually performing it.

    • The purpose of using -WhatIf is to do a "dry run" that allows you to confirm that the command will work as intended when actually run, so as to prevent potentially costly mistakes.
  • That is, the message you saw, What if: Performing the operation "Rename File" on ..., tells you what would happen if you re-ran the command WITHOUT -WhatIf.

    • Therefore, you simply need to remove -WhatIf from the end of your Rename-Item call in order to perform actual renaming.

Note that not all cmdlets support -WhatIf - generally only those with potentially destructive consequences.

Cmdlets that support -WhatIf also support the common -Confirm parameter (and vice versa), which requests that a per-input-object confirmation prompt be shown before processing.

To see whether a given cmdlet supports -WhatIf / -Confirm, look for [-WhatIf] / [-Confirm] in the syntax diagrams output by Get-Help or when you pass the -? switch, or when you use Get-Command -Syntax; e.g., Rename-Item -? or Get-Command -Syntax Rename-Item.
Alternatively, as you're typing a command, try tab completion (type something like -Wha, then press Tab to see if it auto-completes to -WhatIf).


It is possible to implement support for -WhatIf and -Confirm in your own functions and scripts, as long as they are advanced ones, as the following example shows:

function Remove-Foo {
  # * "SupportsShouldProcess" is what activates -WhatIf / -Confirm support.
  # * The "ConfirmImpact" value determines whether confirmation prompts are shown
  #   by default, relative to the $ConfirmPreference preference variable.
  #   Setting the value to 'High' would prompt *by default*.
  [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
  param(
    [Parameter(Mandatory, ValueFromPipeline)]
    [string]
    $FooId
  )

  process {
    # $PSCmdlet.ShouldProcess() either shows the conformation prompt (with -Confirm)
    # or prints a what-if message (with -WhatIf).
    # If $false is returned, the confirmation prompt was declined, or a what-if message was printed,
    # so no further action should be taken for this object.
    if ($false -eq $PSCmdlet.ShouldProcess($FooId)) {
      return # Do nothing (else)
    }
    # Regular processing.
    "Removing $FooId..."
  }

}

Sample call after defining the above function:

PS> 'foo', 'bar' | Remove-Foo -WhatIf
What if: Performing the operation "Remove-Foo" on target "foo".
What if: Performing the operation "Remove-Foo" on target "bar".

CodePudding user response:

Remove -WhatIf from the last line. Your code should like this below:

$Path = "C:\01Source\"
write-host $Path
$curDateTime = Get-Date -Format yyyyMMdd
Get-ChildItem $Path -Recurse |
  Rename-Item -NewName {$_.Basename   '_'   $curDateTime   $_.Extension }
  • Related