rename file.jpg.jpg to file.jpg using powershell - powershell

I inadvertently named over a thousand files as filename.jpg.jpg. My desired end state is to have the file name as filename.jpg. How can I use PowerShell to fix this?
I have tried many examples from blogs and find that the first .jpg is apparently being seen as the file extension. Any assistance would be greatly appreciated as my only alternative is to manually rename all the files.

Try this,
Get-childItem constant* | % {rename-item $_.name ($_.name -replace '.jpg.jpg','.jpg')}
it will replace .jpg.jpg to .jpg

You could use the Get-ChildItem cmdlet to retrieve the files and prefilter it using the -Filter parameter.
You should use the FullName property instead of the Name for the Rename-Item cmdlet whenever your working directory is a diffrent one.
The regexI use here escapes the periods (mentioned by Matt) and also ensures to match the end of the filename ($).
Get-ChildItem -Path 'YOUR_PATH_HERE' -Filter '*.jpg.jpg' |
foreach { Rename-Item $_.FullName ($_.FullName-replace '\.jpg\.jpg$','.jpg') }
Note: If you need to recursively rename the files, you just need to add -recurse to the Get-ChildItemcmdlet.

Related

PowerShell Script finding File using extension and excluding a folder

I am using the below code:
Get-ChildItem -Path N:\USERS -Filter DANTOM.DTM -Recurse -ErrorAction SilentlyContinue -Force
I need it to either find the file "DANTOM.DTM" or the extension ".DTM". I need to exclude the folder N:\USERS\EDI because it is a 1.7TB folder that would never have this file in it. So in doing so would really speed up the process.
I would like the end result to either spit into a .txt file saying which folders inside of N:\USERS has the file or just have it display as a list in powershell.
Thank you,
Assuming that the files of interest do not reside directly in N:\USERS (only in subdirs.), try the following (PSv3+ syntax); send to a file by appending > dirs.txt, for instance.
Get-ChildItem N:\USERS -Directory | ? Name -ne 'EDI' |
Get-ChildItem -Recurse -Filter *.DTM |
ForEach-Object { $_.DirectoryName }
Note: While it is tempting to try a simpler approach with -Exclude EDI, it unfortunately doesn't seem to be effective in excluding the entire subtree of the EDI subfolder.

Renaming multiple files in Windows

I have a folder containing multiple .txt files which have been created by another program. However the program outputs each file with "-Module" in the
filename. eg
filename-Module.txt
filename1-Module.txt
filename2-Module.txt
I would like to know if there's a script or command (Powershell/cmd) that I can run which will iterate over each file and remove the "-Module" from each filename so I simply end up with
filename.txt
filename1.txt
filename2.txt
I have tried using cmd from the directory, with the following:
rename *-Module.txt *txt
This resulted in no change.
You can use get-childitem and pipe it into rename-item.
get-childitem -path 'c:\folderwherefilesarelocated' -recurse |
rename-item -newname {$_.Name -replace "-module", ""}
Relatively straightforward:
Get-ChildItem ComputerName\TestLocation\testfolder -filter "*-module*" | Rename-Item -NewName {$_.name -replace '-module', ''}
Get the items in the folder, look for filenames with "-module" in them, and then replace the "-module" text with an empty string. You can also append a -whatif to see the output before performing this action.

Rename parts of file names that match a pattern

Say I have a list of file names like this:
some-file.ts
my-project-service.ts
other.ts
something-my-project.ts
I need to change the file names that have my-project in them to have just that part renamed to $appname$.
So my-project-service.ts would become $appname$-service.ts
And I need to do this recursively from a root directory.
I seem to be to be hopeless at PowerShell so I thought I would ask here to see if anyone can help me out.
Use the Get-ChildItem cmdlet with the -recurse switch to get all items recursivly from a root directory. Filter all items containing my-project using the Where-Object cmdlet and finally rename them using the Rename-Item cmdlet:
Get-ChildItem "D:\tmp" -Recurse |
Where {$_.Name -Match 'my-project'} |
Rename-Item -NewName {$_.name -replace 'my-project','$appname$' }

Select multiple file types in powershell. Version 2

I am trying to select files that have identical names except for the extenstion...
IE:
idiotCode.dll, idiotCode.pdb, idiotCode.xml, StupidFool.dll, StupidFool.pdb, StupidFool.xml
et cetera.
take a gander at my where-Object call in the below script line...
gci -path $FromPath | ? {$_.Name -match "idiotCode|StupidFool|YourAnIdiot|TheSuckIstHeMatterWhichU" -and $_.Name -like "*.dll"} | foreach{write-host("Do I have the files here? : "+ $_.Fullname + " -destination" + $ToPath) }
Can I use the like parameter to do that? Is there another way to do that? Maybe something in my get-childItem method call which I could pipe into my Where-Object call?
There is a lot that you can do with just the Get-ChildItem Cmdlet. If you look at the help Get-ChildItem, you can do a lot of the filtering there. Specifically using the filters -Filter, -Include and -Exclude
For ex:
Get-ChildItem -Path $FromPath -Include "idiotCode.*","StupidFool.*","YourAnIdiot.*","TheSuckIstHeMatterWhichU.*" -Filter "*.dll"
Ok, I'm a little confused as to the whole concept here because you say you want to select multiple files of the same name but different extensions. Then you pull a directory listing of PathA, filter out everything except files in a kind of "approved names list", and only allow .DLL files to show, and then reference PathB for reasons unknown.
I'm guessing here, but I think you want to query .DLL files from PathA, and then check for matching files from PathB.
$Reference = (GCI $FromPath -Filter "*.DLL").basename
GCI $ToPath|?{$_.BaseName -Match $Reference}|FT -Group BaseName

Powershell Rename-Item issue with special characters

I've run into the issue I know has been addressed several times here previously but I'm not overly familiar with PS scripts or regular expressions and I'm struggling to implement a fix here.
Basically, I'd be very happy if this line of my script would work:
Get-childItem *.* -recurse -force | % {rename-item $_.name ($_.name -replace '(\d{2}) \[(\d{1})x(\d{2})\]','$1 s0$2e$3')}
And example file name would be "24 [1x01].avi" and should instead be named "24 s01e01.avi" - I'm trying to tidy up my media collection :)
I know the reason it doesn't is the square brackets in the file names. I think i have to move the files to a temp location, changing the name while doing so and then move back. My difficulty is that I haven't been able to find an example of this using the regular expression and I haven't been able to get this to work.
Also, is there a better workaround than this available yet? The bug on Microsoft Connect is closed as fixed?
Thanks!
I think your regular expressions might make more sense (to you), especially as a beginner, if you used "named groups" (a regular expression concept). I've modified your regular expression slightly to take this into account. You should really get familiar with regular expression terminology though, to ensure that you can update your regex to work in all scenarios.
"24 [1x01].avi" -replace '(?<ShowName>.*) \[(?<Season>\d{1})x(?<Episode>\d{2})\]','${ShowName} s0${Season}e${Episode}';
Result:
24 s01e01.avi
Can you give an example of a file name that doesn't work?
EDIT: Attaching example script. Let me know if this works for you.
# 1. Define a test folder path
$RootPath = "$env:SystemDrive\test";
# 2. Create the folder
mkdir -Path $RootPath;
# 3. Create a test file
Set-Content -Path "$RootPath\24 [1x01].txt" -Value '';
# 4. Get a list of files in the directory
$FileList = Get-ChildItem -Path $RootPath;
foreach ($File in $FileList) {
# 5. Fix up the name of each file
$NewName = $File.Name -replace '(?<ShowName>.*) \[(?<Season>\d{1})x(?<Episode>\d{2})\]','${ShowName} s0${Season}e${Episode}';
# 6. Rename the file
Move-Item -Path $File.FullName -Destination ((Split-Path -Path $File.FullName -Parent) + $NewName);
}
powershell Rename-Item fail to rename
If you are running PS 3+ add -LiteralPath switch to your rename:
One of the easiest ways to handle the Special Characters (such as square/block brackets[]) in the file-names, is to simply use the -LiteralPath parameter.
Error: When attempting to rename files or folders that contain square/block brackets [], the standard error message that PowerShell returns is "file not found", which is not accurate.
Reason: Windows still uses old fashioned 8.3 format short-file-names (max 8 chars with limited allowed chars) unfortunately PowerShell's -Path parameter (even in version 5.1) uses these internal names.
Solution: Use the -LiteralPath argument, available for most cmdlets (including Get-ChildItem or Rename-Item etc.)
Examples: Depicting handling of files or folders that contain square/block brackets []:
Get-ChildItem -LiteralPath "test[1].txt";
Test-Path -LiteralPath "C:\dir\test[1].txt";
Rename-Item -LiteralPath "test[1].txt" "test[2].txt";
Note: In PowerShell version below 3.0, to rename files/directories containing special characters, use Move-Item with -LiteralPath, instead of Rename-Item cmdlet because Rename-Item didn't have -LiteralPath in PS version 2.0 (or below).
Thanks to pointers from #Trevor Sullivan I was able to get the desired results by:
Updating to the most recent version of PowerShell (download link available in the comments)
Edited the script to the following:
Get-childItem *.* -recurse -force | Move-Item -Destination {$_ -replace '(\d{2}) \[(\d{1})x(\d{2})\]','$1 s0$2e$3'}