-3

For example

test-123-abc.mp4
test-456-def.mp4

I want to remove all after _ or -. So the result should be:

123.mp4
456.mp4

Is it possible to use Windows cmd or a batch file, PowerShell to rename?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Enuma
  • 61
  • 1
  • 10
  • PowerShell has a replace method which you can use to replace string/characters/rename files....https://ss64.com/ps/replace.html – Vincent K Nov 09 '17 at 08:45
  • 1
    read this...https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/22/use-powershell-to-rename-files-in-bulk/ – Vincent K Nov 09 '17 at 08:47
  • Let's me take a look :) Thanks ! – Enuma Nov 09 '17 at 08:49
  • When i try to replace its still the same, the result is test123abc.mp4 :( – Enuma Nov 09 '17 at 08:56
  • Possible duplicate of [Extricate a substring using PowerShell](https://stackoverflow.com/questions/2988880/extricate-a-substring-using-powershell) – Manu Nov 09 '17 at 08:59
  • @Manu this is a list of my files,can you show me how to do it ? https://i.imgur.com/0X3HWcf.png – Enuma Nov 09 '17 at 09:06
  • `for %F in ("*-*-*.mp4") do for /F "tokens=2 delims=-" %E in ("%~nF") do ren "%~F" "%~E.%~xF"` or `for /F "tokens=1-3 delims=-" %E in ('dir /B "*-*-*.mp4"') do ren "%E-%F-%G" "%F%~xG"` (double the `%`-signs in a batch file) – aschipfl Nov 09 '17 at 09:27
  • 1
    @Enuma : if you want to remove all after "-" or "_", it means you just want to keep the first word and your example shows that you want the word in the middle. Please be more specific. – Manu Nov 09 '17 at 09:34
  • Yeah Manu,did you take a look at my picture in my cmt above? – Enuma Nov 09 '17 at 09:49
  • Looking at those files in your off site image, should your question not be, how to remove everything from and including the last dash, **`-`**, in my file names? – Compo Nov 09 '17 at 11:27

2 Answers2

0

I am assuming that all the file name are in the same format. This is just an example, you have to write the script yourself. If you are facing any issue, post the script.

Get-ChildItem -Path D:\Videos -File | Rename-Item -NewName { $_.Name -replace "Combo.*","Combo.mp4" }

Ex.

'test-123-abc.mp4' -replace '.*-(.*)-.*(\..*)','$1$2'
123.mp4
'test-456-def.mp4' -replace '.*-(.*)-.*(\..*)','$1$2'
456.mp4

'TRG-21-Aries Combo-JM5rowKV9TU.mp4' -replace 'Combo.*','Combo.mp4'
TRG-21-Aries Combo.mp4
Vincent K
  • 1,326
  • 12
  • 19
  • You should wait for her to be clearer as the result she wants in the question is not corresponding to `"I want to remove all after "_" or "-"`. The result should be `test.mp4` for both files... – Manu Nov 09 '17 at 09:38
  • posted examples on how to use replace – Vincent K Nov 09 '17 at 09:40
  • @VincentK Please take a look at my picture https://i.imgur.com/0X3HWcf.png the name is the youtube_id so i want to remove it and only keep the name of the video ! – Enuma Nov 09 '17 at 09:52
  • One of the file name is "TRG-21-Aries Combo-JM5rowKV9TU.mp4" so i want it to be this name "TRG-21-Aries Combo.mp4" – Enuma Nov 09 '17 at 09:54
  • @VincentK Thank you,but it still change name of 1 file,i want all the file in the folder,is that possible ? because "'TRG-21-Aries Combo-JM5rowKV9TU.mp4' -replace" i have to input the name of the file.. – Enuma Nov 09 '17 at 10:10
  • yes it is possible...I posted an example... you have to try it yourself – Vincent K Nov 09 '17 at 10:12
  • Yeah,but for every files,i have to input the file name,i want all the file of 1 folder..its not just "TRG-21-Aries Combo-JM5rowKV9TU.mp4" they have different name :( – Enuma Nov 09 '17 at 10:24
  • Updated my answer – Vincent K Nov 09 '17 at 16:01
0

It sounds like you want the rename to occur on all .mp4 files in the directory. If so, this might work. When you are satisfied that the files will be correctly renamed, remove the -WhatIf from the Move-Item command.

Get-ChildItem -Filter *.mp4 |
    ForEach-Object {
        $newname = $_.Name -replace '(.*)[-_].*(\..*)','$1$2'
        Rename-Item -Path $_.FullName -NewName $newname -WhatIf
    }
lit
  • 14,456
  • 10
  • 65
  • 119