File Rename/move with TC & AHK

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
SysATI
Junior Member
Junior Member
Posts: 4
Joined: 2019-10-15, 17:18 UTC

File Rename/move with TC & AHK

Post by *SysATI »

Hi...

I'm trying to do something failry simple and can't seem to figure it out by my own :(
So before I start climbing up to the curtains, I though I'd ask you guys...

I have the below directory structure:

directory
--sub-directory
subtitles_file.SRT
movie_file.MP4

From that, runing a macro or a AHK script, I would like to rename the subtitles.SRT file with the movie filename.
i.e. end up with the same file name both for the movie and the subtitles :
movie_filemane.MP4
movie_filename.SRT

So far I can go to the subtitles directory and copy the subtitles files to the parent (movie) directory...
But then I'm stuck, the AHK FileMove function doesn't seem to work... Or more probably I'm tooo dumb for it :(


Send {Tab}

Send {Home}
Send {Down}
Send {Enter}

;copy subtitles
Send {Down}
Send ^C

;return to video directory & paste subs
Send {Home}
Send {Enter}
Send ^V



Anyone with a (simple) idea ?

Thanks !
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: File Rename/move with TC & AHK

Post by *gdpr deleted 6 »

This should be so much simpler to do entirely within a script, like:

1. Define directory of the *.mp4 file (either within the script, or pass it as an input parameter to the script).
2. Get the name of the *.mp4 file in the directory, segregate the base file name from the extention.
3. Get the name/path of the sub-directory (depending on the capability of the chosen scripting environment, this could perhaps be combined with the next step)
4. Get the name of the *.srt file in the sub-directory.
5. Move/rename it to <Directory>\<Mp4BaseFileName>.srt

Doing this entirely within a script (AHK or whatever other script language; heck, even batch should do) should be much simpler (not only easier to write, but also easier to test/verify for correctness) than trying to write an intricate script that attempts to "remote-control" TC's UI and which would always be on the verge of completely falling apart if TC's UI doesn't behave exactly (and i mean exactly) in the way you thought it would/should do.

I am not saying that it is impossible to correctly and reliably "remote-control" TC under any circumstances, but it might be quite the undertaking to get right that it works reliably in any circumstances. And the fact you are asking here makes me think you did not knowingly and intentionally aim for this challenge, but rather prefer an approach that could be easily implemented and maintained.
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: File Rename/move with TC & AHK

Post by *gdpr deleted 6 »

I am bored and procrastinating my chores, and i don't want you to think i am a smart-assing idiot on the internet. Yes, i am a smart-ass, but i ain't an idiot (at least most of the time).

So, here a proof of concept batch file that does what you want to do (assuming i understand you correctly). The directory of the MP4 file need to be passed as first argument. Also, it only has rudimentary safety checks -- well, i guess they are rudimentary, haven't spent much time thinking about what possible failure scenarios could be. It is mostly just blindly assuming the passed directory and the files inside adhere perfectly to your description.

Code: Select all

@echo off
setlocal

for /f "usebackq delims=" %%s in (`dir /b %1\*.mp4`) do set "Mp4BaseName=%%~ns"
for /f "usebackq delims=" %%s in (`dir /b /s %1\*.srt`) do set "SrtFile=%%s" && set "SrtDir=%%~dps"

if "%Mp4BaseName%" == "" echo No MP4 file && exit /b
if "%SrtFile%" == "" echo No SRT file && exit /b

move "%SrtFile%" %1\"%Mp4BaseName%.srt"
rd "%SrtDir%"
(Writing an equivalent script in AHK, or Powershell, or most other scripting languages is preferred over batch scripts, in my opinion. Since i don't know AHK that well, nor do i know what version of Windows you are running with regard to Powershell availability, i restricted myself here to a batch script.)
User avatar
Stefan2
Power Member
Power Member
Posts: 4158
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: File Rename/move with TC & AHK

Post by *Stefan2 »

As far as I understood...

YOU HAVE:
base directory\
--sub-directory-1\
--sub-directory-1\subtitles_file.SRT
--sub-directory-1\movie_file1.MP4
--sub-directory-2\
--sub-directory-2\subtitles_file.SRT
--sub-directory-2\movie_file2.MP4
--sub-directory-3\
--sub-directory-3\subtitles_file.SRT
--sub-directory-3\movie_file3.MP4

YOU WANT:
- for each "sub-directory"
--- get base name of MP4
--- set base name of SRT to base name of MP4

TO GET:
base directory\
--sub-directory-1\
--sub-directory-1\movie_file1.SRT
--sub-directory-1\movie_file1.MP4
--sub-directory-2\
--sub-directory-2\movie_file2.SRT
--sub-directory-2\movie_file2.MP4
--sub-directory-3\
--sub-directory-3\movie_file3.SRT
--sub-directory-3\movie_file3.MP4


- - -

Me think for AHK you must use loops.


Here is some dummy code for an start to get the idea:

Code: Select all

; // ## DUMMY code only !!! , read the help
;//From base-directory, loop sub-folders
Loop, FilePattern [, IncludeFolders?, Recurse?]
{
    ;// on each sub-folder "A_LoopFileName", loop *MP4-files
    Loop, FilePattern *.MP4 [, IncludeFolders?, Recurse?]
    {
        myMP4 = "A_LoopFileName"
        break
    }
    ;// on each sub-folder "A_LoopFileName", loop *SRT-files
    Loop, FilePattern *.SRT [, IncludeFolders?, Recurse?]
    {
        mySRT = "A_LoopFileName"
        break
    }
    ;//Get new file name (use MP4-name and just replace the extension):
      StringReplace, OutputVar myNewFileName, InputVar myMP4, SearchText MP4 , ReplaceText SRT
    ;//"Rename": 
      FileMove, SourcePattern mySRT, DestPattern myNewFileName
}

Of course there will be much more elegant code as that above...

In last times I wasn't much into AHK, so we will have to wait for an better script(er) :D .

- - -


Here is an PowerShell script

USAGE:
- make an BACKUP first!
- open powershell console in your "base directory"
- execute this script line below:

Get-ChildItem -Directory|ForEach{$MP4=Get-ChildItem $_\*.MP4;$SRT=Get-ChildItem $_\*.SRT;Rename-Item $srt ($MP4.BaseName + ".SRT")}

THIS script works only if there is only one MP4 and only one SRT in each folder.
Else we would have to use something like "$MP4=Get-ChildItem $_\*.MP4|Select -First 1;"

And you will need to have a recent PoSh-version which supports "-Directory" for "Get-ChildItem",
else we would need to utilize "$PSisContainer", all that just in case if you will have also files in your "base directory".



Again the code but more formatted:
Get-ChildItem -Directory|
ForEach{
$MP4=Get-ChildItem $_\*.MP4;
$SRT=Get-ChildItem $_\*.SRT;
Rename-Item $srt ($MP4.BaseName + ".SRT")
}




 
SysATI
Junior Member
Junior Member
Posts: 4
Joined: 2019-10-15, 17:18 UTC

Re: File Rename/move with TC & AHK

Post by *SysATI »

THANK YOU ALL !!!

Here is a "live" sample...

Directory of d:\Downloads\FBI.S03E02.WEBRip.x264-ION10

25-Nov-20 12:17 440,939,286 FBI.S03E02.WEBRip.x264-ION10.mp4
25-Nov-20 12:16 30 RARBG.txt
25-Nov-20 12:16 99 RARBG_DO_NOT_MIRROR.exe
25-Nov-20 12:17 <DIR> Subs
25-Nov-20 12:16 75,710 2_English.srt

And using the batch file below, I end up with :


25-Nov-20 12:17 440,939,286 FBI.S03E02.WEBRip.x264-ION10.mp4
25-Nov-20 12:16 75,710 FBI.S03E02.WEBRip.x264-ION10.srt
25-Nov-20 12:16 30 RARBG.txt
25-Nov-20 12:16 99 RARBG_DO_NOT_MIRROR.exe

Which is EXACTLY what I wanted to do...


;@echo off
setlocal

for /f "usebackq delims=" %%s in (`dir /b *.mp4`) do set "Mp4BaseName=%%~ns"
for /f "usebackq delims=" %%s in (`dir /b /s Subs\*.srt`) do set "SrtFile=%%s" && set "SrtDir=%%~dps"

if "%Mp4BaseName%" == "" echo No MP4 file && exit /b
if "%SrtFile%" == "" echo No SRT file && exit /b

move "%SrtFile%" "%Mp4BaseName%.srt"
rd "%SrtDir%"

I modified it just a little bit to fit the existing directory structure but it works perfectly fine...

I'll try to figure out the other solutions you've given me (powershell, AHK) but this simple batch file does the trick perfectly ;)

Again thanks a lot guys....
NotNull
Senior Member
Senior Member
Posts: 269
Joined: 2019-11-25, 20:43 UTC
Location: NL

Re: File Rename/move with TC & AHK

Post by *NotNull »

A PowerShell version that supports multiple MP4's and multiple SRT's per folder.
Not thoroughly tested as I'm running out of (Total Commander) time, but it is a one-liner, so "button-ready"
Maybe someone else can make this into something ready-to-run.


Steps:
- In PowerShell, create an empty folder
- cd to that folder
- Run this command to create some demo folders with MP4's and SRT's :

Code: Select all

foreach ($M in 0..2) { foreach ($S in 0..2) { $D = md "movie $M  $S" ; 0..$M -ne 0 | % { 0> "$D\movie $_.mp4"} ; 0..$S -ne 0 | % { 0> "$D\STR $_.srt"}}}
- Run thsi command to rename SRT's (interactive when needed; look at the title bar for the current movie):

Code: Select all

gci . -include *.mp4,*.mkv,*.avi -Depth 2 | % { $M = $_ ; $S = gci -path $_.Directory *.srt -recurse ; if ($S.Count -ne 1) {$S | ogv -out single -Title "Select subtitle for movie $M " | % { ren $_.FullName ($M.BaseName + '.srt')}}  else { ren $S.FullName ($M.BaseName + '.srt') }}
Post Reply