0

I was wondering if there's a quick an easy way to search for filenames with several characters and delete all the characters after those.

For example if I have a file called:

Great Movie Title 2021 1080p asldkfjalfj.mkv
Another Great Movie 1997 480p aldfasdfa.avi
Boring movie but its ok 2015 720p asadfqwr.mp4

I want to find 1080p, 720p, 480p in the file name and then delete everything after it.

I know you can use Windows Batch For loop but you can only use a single character to find it. Even if it's a third party app that can do this, but I can't seem to find it.

2 Answers 2

1

Are you searching the whole drive, or just a single directory?

Either way, PowerShell can handle this.

This will do the whole C drive, renaming anything with 1080p in the filename:

cd c:\
Get-ChildItem -recurse -include *1080p* | foreach-object { $name = $_.fullname; $newname = $name -replace '1080p.*\.','1080p.'; rename-item $name $newname } 

You could then repeat it for 480p and 720p.

3
  • Thank you! My test folder with local files worked great. But when I went to map my network drive to run this it gives error. I did login with `net use Y: "\\servername\path" but when I run the command it gives an error: "rename-item : Cannot rename the specified target, because it represents a path or device name." I am in administrator mode as well as CD in the directory I want to work with.
    – HTWingNut
    Commented Apr 16, 2021 at 3:32
  • Well it seemed to only be a few files that errored out and I don't know why, otherwise it seemed to work. Thank you very much!
    – HTWingNut
    Commented Apr 16, 2021 at 3:44
  • For the files that errored out, the most likely cause is characters ine path/filename that require the use of -LiteralPath parameter. Square brackets [ & ] and ddollar signs $ are frequent offenders. Commented Apr 16, 2021 at 5:49
2

PowerShell is the way to go.

Here's another approach that could get them all in one pass and easily modified.

This would be run from the top-level folder. Doing so frees up the -Path (positional here) paremeter to filter by multiple filetypes, because -Path, unlike -Filter, can take an array of values.

Get-ChildItem *.mkv, *.avi, *.mp4 -Recurse | ForEach {
    If ( $_.BaseName -match '^.+(1080p|720p|480p)' ) {
        Rename-Item -LiteralPath $_.FullName -NewName ( $matches[0] + $_.Extension )
    }
}

The regular expression '^.+(1080p|720p|480p)' matches all characters from rthe beginning of the filename up to and including any one of the "or'd" (|) resolution strings. That text is stored in the $matches automatic vafiable, and used to construct the new name.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .