-3

I have a folder full of files (.avi and .txt files):

MyVideo1-MyEncode.avi
MyVideo1.avi 16-05-2019 15-41-01.txt

My Video 2.avi
My Video 2.avi 16-05-2019 16-43-11.txt

My Video 3 [Summer]-MyEncode.avi
My Video 3 [Summer].avi 16-05-2019 17-57-24.txt

My Video 4-(Fall).avi
My Video 4-(Fall).avi 13-05-2019 19-29-16.txt

My Video 5-(Winter)-MyEncode.avi
My Video 5-(Winter).avi 11-05-2019 11-15-05.txt

If the .avi file includes "-MyEncode" in the filename, I would like to then copy this part of the string into the filename of the corresponding text file.

e.g.

"MyVideo1-MyEncode.avi" includes the text "-MyEncode" in the filename

so: "MyVideo1.avi 16-05-2019 15-41-01.txt"

should be renamed as: "MyVideo1-MyEncode.avi 16-05-2019 15-41-01.txt"

on the other hand:

"My Video 2.avi" does not contain the string "-MyEncode" so the corresponding .txt file should not be renamed.

I have a number of folders containing files like this.

Can anyone please help?

PS - If I makes things easier, I can rename the like of "My Video 3 [Summer]-MyEncode.avi" to "My_Video_3_[Summer]-MyEncode.avi" (i.e. to remove the spaces)

1 Answers1

0

SPACE characters are -not- the problem. The brackets used by regex are. [] and () You would be well advised to not use these in file names and directory names.

Create these two (2) files in the same directory. When you are satisfied that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.

=== Do-EncRename.ps1

Get-ChildItem -Path 'C:/src/t/myenc/' -Filter '*-MyEncode.avi' |
    Where-Object { ! $_.PSIsContainer } |
    ForEach-Object {
        if ($_.Name -match '^(.*)\-MyEncode.avi') {
            $header = $Matches[1]
            Get-ChildItem -Filter ($header + "*.txt") |
                Where-Object { ! $_.PSIsContainer } |
                ForEach-Object {
                    if ($_.Name -match (($header -replace '[\[\]\(\)]', '.') + "(\.avi.*\.txt)")) {
                        $trailer = $Matches[1]
                        Rename-Item -LiteralPath $_.FullName -NewName ($header + '-MyEncode' + $trailer) -WhatIf
                    }
                }
        }
    }

=== Do-EncRename.bat

powershell -NoLogo -NoProfile -File "%~dp0%~n0.ps1"

And, here is a way to do it without PowerShell. When the REN statements look correct, remove the echo at the beginning of the line.

=== doit.bat

@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%a in ('dir /b "*-MyEncode.avi"') do (
    set "FN1=%%~a"
    set "HEADER=!FN1:-MyEncode.avi=!"

    for /f "delims=" %%b in ('dir /b "!HEADER!.avi*.txt"') do (
        set "FN2=%%b"
        set "NFN=!FN2:.avi=-MyEncode.avi!"
        echo REN "!FN2!" "!NFN!"
    )
)

Running the .bat script.

C:>CALL "myenc2.bat"
REN "My Video 3 [Summer].avi 16-05-2019 17-57-24.txt" "My Video 3 [Summer]-MyEncode.avi 16-05-2019 17-57-24.txt"
REN "My Video 5-(Winter).avi 11-05-2019 11-15-05.txt" "My Video 5-(Winter)-MyEncode.avi 11-05-2019 11-15-05.txt"
REN "MyVideo1.avi 16-05-2019 15-41-01.txt" "MyVideo1-MyEncode.avi 16-05-2019 15-41-01.txt"
lit
  • 14,456
  • 10
  • 65
  • 119
  • @PresidioSF - No need to change the names. I think this will work. Try it. – lit May 17 '19 at 00:06
  • Processing -File 'C:\MyEnc\Do-EncRename.bat' failed because the file does not have a '.ps1' extension. So I changed the "%~dp0%~n0.bat" in the .bat file to "%~dp0%~n0.ps1" but then instead get the error: Get-ChildItem : A parameter cannot be found that matches parameter name 'File', At C:\MyEnc\Do-EncRename.ps1: 1 char: 20 + Get-ChildItem-File – PresidioSF May 17 '19 at 00:16
  • At the command line, what is the output of `powershell -NoLogo -NoProfile -Command ($PSVersionTable.PSVersion).ToString()`? Sorry about the .ps1 extension. You corrected it exactly right. It might be that your version of PowerShell does not have `-File`. – lit May 17 '19 at 01:04
  • Output at command line from that is: 2.0 – PresidioSF May 17 '19 at 01:10
  • @PresidioSF - The `-File` switch was added in PowerShell 3.0. The code can be changed to work in 2.0. Is it possible that you could upgrade to a current version? The current version from Microsoft is 5.1. – lit May 17 '19 at 01:13
  • @PresidioSF - I changed the code such that it will work with PowerShell 2.0. Yes, it will continue to work with PowerShell 3.0+. – lit May 17 '19 at 01:17
  • Okay, it is running, but something is backwards. "MyVideo1.avi 16-05-2019 15-41-01.txt" should become "MyVideo1-MyEncode.avi 16-05-2019 15-41-01.txt", but instead the .avi file is being renamed as "MyVideo1-MyEncode.avi 16-05-2019 15-41-01.txt" and the .txt file is totally untouched. – PresidioSF May 17 '19 at 02:37
  • @PresidioSF - I have fixed the code to rename the .txt file. Sorry about that. Using `-WhatIf` can be very helpful. – lit May 17 '19 at 10:22
  • The Powershell version appears to be working now... The .bat version runs, but it does not actually rename anything. Also is it possible you could include lines of output in the batch version (i.e. echo the filename) to show which filenames have been successfully renamed? – PresidioSF May 17 '19 at 13:33
  • @PresidioSF - There is already an ECHO in the .bat script. I added the output of running the .bat script to the answer. You, also, can add ECHO statements. Remember that you need to remove the `echo` from the `REN` command line if you want the files to actually be renamed. – lit May 17 '19 at 14:16