Powershell to rename files with datetime formatted - powershell

I have a folder with media files named by timestamp like following yyyyMMdd_HHmmss_*.*. I need to rename them to yyyy-MM-dd HH-mm-ss *.*
For example I need to rename file 20181019_210353_BURST2.jpg to 2018-10-19 21-03-53 BURST2.jpg
There is a my ugly approach
PS E:> gci | Rename-Item -NewName { $_.Name.Substring(0,4) + '-' + $_.Name.Substring(4,2) + '-' + $_.Name.Substring(6,2) + ' ' + $_.Name.Substring(9,2) + '-' + $_.Name.Substring(11,2) + '-' + $_.Name.Substring(13,2) + $_.Name.Substring(15) }
What is the right command to obtain my purpose?

If it is concision you're looking for, you can use the -replace operator with the following regex:
Get-ChildItem -File -Filter *.jpg | Rename-Item -NewName {
$_.Name -replace '(^\d{2})?(\d{2})(\d{2})(\d{2})_', '$1$2-$3-$4 '
} -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
An explanation of the regex, as well as the ability to experiment with it and the substitution expression, can be found on this regex101.com page.

Your method will work, but you will need to provide a -Path parameter to Rename-Item. The short answer is that the string will need to be broken down into the components to use in the new name.
If you want to have some regex fun, you could use something like this. When you are convinced that the files will be renamed correctly, remove the -WhatIf from the Rename-Item command.
Get-ChildItem |
ForEach-Object {
if ($_.Name -match '^(....)(..)(..)_(..)(..)(..)(.*)') {
$NewName = "$($Matches[1..3] -join '-') $($Matches[4..6] -join '-')$($Matches[7])"
Rename-Item -Path $_.FullName -NewName $NewName -WhatIf
}
}

Related

How do I recursively rename files in a directory with Powershell?

I have a powershell script which appends " - Confidential" to the end of files that don't already match the string "Confidential" within the filename. When running this script in the directory, it works. However, I need it to rename all the items within the subfolder too. How could I achieve this?
Get-ChildItem * -Exclude *Confidential* -Recurse |
ForEach {Rename-Item -NewName { $_.BaseName + " - Confidential" + $_.Extension }}
Thanks all for reading.
You're close, but there's a few problems:
Rename-Item is missing the original file, so let's add $_ to it. We could have also given it $_.FullName but I'm just passing the entire object to it.
Rename-Item is being given a script block with no input for -NewName because you've used curly braces ({}), so we change it to regular brackets (()).
Subfolders are also being renamed, which also results in files in them not being recursed (because they contain Confidential), so we specify the -File switch on Get-ChildItem to only return files.
So that brings us to this:
Get-ChildItem * -File -Exclude *Confidential* -Recurse |
ForEach {Rename-Item $_ -NewName ( $_.BaseName + " - Confidential" + $_.Extension )}
And just some cleanup/optimisation:
Remove wildcard (*) from Get-ChildItem. It already assumes all items in the folder you're in.
Enclose string in quote marks.
Add some spaces inside script block and change ForEach alias to shorter % alias (both just my personal preference).
And the final result looks like this:
Get-ChildItem -File -Exclude "*Confidential*" -Recurse |
% { Rename-Item $_ -NewName ( $_.BaseName + " - Confidential" + $_.Extension ) }
As always, you can do a dry run by specifying -WhatIf on Rename-Item, just in case there's some unexpected behaviour.
Edit: You could actually just pipe the output of Get-ChildItem into Rename-Item like so without the need for ForEach-Object:
Get-ChildItem -File -Exclude "*Confidential*" -Recurse |
Rename-Item -NewName { $_.BaseName + " - Confidential" + $_.Extension }

How to rename files with a conditional statement in PowerShell

I am trying to rename a whole lot of files all located in one folder but in different subfolders. The files should be renamed so that their name consists of the foldername + the original file name. I am wondering if you could add a conditional statements so that the file name doesn't change if the file name already contains the folder name. The code below performs the function of renaming the files but doesn't contain the if statement.
dir -recurse | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}
The code below is an example on how I imagine the code would look:
dir -recurse | if($_.Name -contains $_.Directory.Name) {Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}}
This should do it:
$rootFolder = 'D:\test'
Get-ChildItem -Path $rootFolder -File -Recurse | ForEach-Object {
$folder = $_.Directory.Name
if (!($_.Name.StartsWith($folder))) { $_ | Rename-Item -NewName ('{0} - {1}' -f $folder, $_.Name) }
}
Theo's answer works well, but there's an alternative that is both conceptually simpler and performs noticeably better:
You can take advantage of the fact that passing an unchanged file name to -NewName is a quiet no-op, so you can place all your logic in the -NewName script block:
Get-ChildItem -File -Recurse |
Rename-Item -NewName {
if ($_.Name -like ($_.Directory.Name + ' - *') { # already renamed
$_.Name # no change
}
else { # rename
$_.Directory.Name ' - ' + $_.Name
}
} -WhatIf
-WhatIf previews the renaming operation; remove it to perform actual renaming.
Rather than using ForEach-Object call with a nested Rename-Item call in its script block - which means that Rename-Item is called once for each input file - this solution uses a single pipeline with a single Rename-Item invocation, where the new name (if changed) is determined via a delay-bind script block - see this answer for details.
I was trying it in a way close to the way the question was asked. I wish I didn't have to add another foreach.
dir -recurse -file | & {
foreach ($i in $input) {
if(-not ($i.Name.contains($i.Directory.Name))) {
Rename-Item $i.fullname -NewName ($i.Directory.Name + ' - ' + $i.Name) -whatif
}
}
}
Or like this
dir -recurse -file | % {
if(-not ($_.Name.contains($_.Directory.Name))) {
Rename-Item $_.fullname -NewName ($_.Directory.Name + ' - ' + $_.Name) -whatif
}
}

Skipping files with a given prefix when renaming

I have the following script for renaming a bunch of files in a directory, adding the name of the directory to the start of them:
$s = "Y:\Teknisk Arkiv\Likeretter 111-Å1\E2_Kretsskjema\02_Likeretter styring\3AJJ000302-222"
Get-ChildItem -Path $s -Exclude $_.Directory.Name* | rename-item -NewName { $_.Directory.Name + '_' + $_.Name }
Before running the script the files in the folder looks something like this
after like this
As you can see it does more or less what I want, except that -exclude $_.DirectoryName* doesn't prevent files which already have the foldername as a prefix from being renamed. What am I doing wrong here?
$_ in a pipeline is only defined inside a script block used in a non-initial pipeline segment, where it refers to the input object at hand, so in your Get-ChildItem command it is effectively undefined.
Even if $_.Directory.Name did have a value, $_.Directory.Name* wouldn't work as expected, because it would be passed as 2 arguments (you'd have to use "$($_.Directory.Name)*" or ($_.Directory.Name + '*').
You instead want to extract the directory name from the $s input path, which you can do with Split-Path -Leaf, and then append '*'.
In order for -Exclude to be effective, the input path must end in \*, because -Include and -Exclude filters - perhaps surprisingly - operate on the leaf component of the -Path argument, not on the child paths (unless -Recurse is also specified).
To put it all together:
Get-Item -Path $s\* -Exclude ((Split-Path -Leaf $s) + '*') |
Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name }
I've switched to Get-Item, since \* is now being used to enumerate the children, but Get-ChildItem would work too.
The $_ is only valid when it is used on the right-side of a pipeline meaning when you have a collection of items and "pipe" them through the "$_" would represent the current item.
Since the directory name you want excluded is static you can just hardcode it and use as your exclude filter.
$s = "Y:\Teknisk Arkiv\Likeretter 111-Å1\E2_Kretsskjema\02_Likeretter styring\3AJJ000302-222"
$exclude_filter = "3AJJ000302-222*"
Get-ChildItem -Path $s -Exclude $exclude_filter | rename-item -NewName { $_.Directory.Name + '_' + $_.Name }
Also try to use "-whatif" with rename-item so you know what will happen before it happens.
$_ represents the currently processed item, what requires a ForEach-Object or a scriptblock inside a pipe, not present at the begin of your command.
Solution make the path a FileInfoObject and use -Exclude
$s = Get-Item "Y:\Teknisk Arkiv\Likeretter 111-Å1\E2_Kretsskjema\02_Likeretter styring\3AJJ000302-222"
Get-ChildItem -Path $s -Exclude "$($s.Name)*"|Rename-Item -NewName {$_.Directory.Name+'_'+$_.Name}
solution use a Where-Object to filter files already starting with the directory name
Get-ChildItem -Path $s | Where-Object {$_.Directory.Name -notlike "$($_.Name)*"} |
Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name }
Solution use the RegEx based -replace operator to prepend the directory name and use a negative lookahead assertion to exclude files which already have it.
Get-ChildItem -Path $s |
Rename-Item -NewName {$x=$_.Directory.Name;$_.Name -replace "^(?!$x)",$x}

Rename file with powershell (variable file name structure)

Currently I'am facing an issue in renaming file names with powershell. I'am actually able to rename files in a particular folder, however if the structure is different the command fails.
Example files:
test file - 1234 - copy.docx
test file - 1234.pdf
I was running the following command:
Get-ChildItem <location> -file | foreach {
Rename-Item -Path $_.FullName -NewName ($_.Name.Split("-")[0] + $_.Extension) }
I want to keep the filename before the last "-". But if I run my command, I always get file name before the first "-".
Any advice for a better approach?
Most straightforward approach:
Get-ChildItem <location> -File | Rename-Item -NewName {
$index = $_.BaseName.LastIndexOf("-")
if ($index -ge 0) {
$_.BaseName.Substring(0, $index).Trim() + $_.Extension
}
else { $_.Name }
}
Regex replace:
Get-ChildItem <location> -File |
Rename-Item -NewName {($_.BaseName -replace '(.*)-.*', '$1').Trim() + $_.Extension}
You could use RegEx to achieve the desired output:
Rename-Item -Path $_.FullName -NewName (($_.Name -replace '(.*)-.*?$','$1') + $_.Extension) }
(.*)-.*?$ selects all chars (greedy) until the last - before the end of the line.

PowerShell command to add character to filename at specific position

I'd like to add underscore character "_" at position 22 of a filename.
What would be the PowerShell command to do it?
alternately you can also make use of the string method insert
Get-Item -Path $Path |
Rename-Item -NewName {$_.BaseName.insert(22,'_') + $_.Extension} -WhatIf
note: remove -whatif to apply the rename
You could use -replace and a simple regex to achieve that.
In the following example, I first retrieve the file using the Get-Item cmdlet and rename it using Rename-Item:
Get-Item $YOURPATH | % { $_ | Rename-Item -NewName ($_.Name -replace '^([\S\s]{22})', '$1_')}
You may have to add a check whether the file name is long enough, otherwise it could happen, that you rename the file extension or nothing...
below script will add '-' at position 3 and 6 and 9 and 12 and 15
from: s270120070336.bmp
to: s27-01-20-07-03-36.bmp
Get-ChildItem -Path 'C:\users\sonook\desktop\screenshot' -Filter '*.bmp' |
ForEach-Object { $_ | Rename-Item -NewName {$_.BaseName.insert(3,'-').insert(6,'-').insert(9,'-').insert(12,'-').insert(15,'-') + $_.Extension}}