Page 1 of 1

Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 05:12
by Kwan
Can anybody tell me what am I doing wrong in this code?

I have done a batch to copy the name of a .avi file to a .srt file. The problem is that the .srt file became with no name.

Ex:
before batch:
matrix.avi
subtitle.srt

after batch:
matrix.avi
.srt

expected result:
matrix.avi
matrix.srt

Here is the code:

Code: Select all

@echo off 

dir *.avi /b "C:\Batch files" >>filelist.txt

for /f "tokens=1* delims=|" %%a in (filelist.txt) do (
    set "str=%%a"
    set  str=!str:~-4!
    ren *.srt "%str%".srt
)

del filelist.txt




Help please

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 05:30
by abc0502
The delims in the for loop, you should make it dot "." not "|" and in this case the %%a will be the files name without the extension and no need for subtracting the last 4 chars.

Code: Select all

@Echo Off

dir *.avi /b "C:\Batch files" >>filelist.txt

for /f "tokens=1* delims=." %%a in (filelist.txt) do (
    set "str=%%a"
    ren "*.srt" "%str%".srt
)
del filelist.txt


This will work for only one avi file and one srt file in the same folder

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 05:59
by Squashman
Assume this is you over on TechGuy as well.
http://forums.techguy.org/dos-other/107 ... -file.html

Don't need to use the delims to remove the file extension. Just use the command modifiers.

%%~na

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 06:19
by abc0502
What do you want to do exactly, are you going to rename many files in different folders using the same principle?

This will go through all sub-folders in the Batch files and check to see it there is a avi file and srt file, if there, it will rename the srt file with the same name of the avi file.

That will be repeated on all sub-folders exist in the Batch files folder.

Any folder MUST contain One AVI file and One SRT file, or all your files will be overwritten or deleted
This is not Tested

Code: Select all

@Echo Off
set "Parent_Folder=%userprofile%\desktop"
Setlocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in ('Dir /B /S "%Parent_Folder%\*.avi"') Do (
   PUSHD %%~dpA
   IF Exist "*.avi" (
      IF Exist "*.srt" (
         Ren "*.srt" "%%~nA.srt"
      )
   )
   POPD
)

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 07:01
by Kwan
abc0502 wrote:The delims in the for loop, you should make it dot "." not "|" and in this case the %%a will be the files name without the extension and no need for subtracting the last 4 chars.

Code: Select all

@Echo Off

dir *.avi /b "C:\Batch files" >>filelist.txt

for /f "tokens=1* delims=." %%a in (filelist.txt) do (
    set "str=%%a"
    ren "*.srt" "%str%".srt
)
del filelist.txt


This will work for only one avi file and one srt file in the same folder



I thought that too but there are files that have dot within their name so the dot cannot be the reference for delims =\
example:
before batch
matrix.reloaded.avi
subtitle.srt

after batch
matrix.reloaded.avi
matrix.srt

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 07:04
by Kwan
abc0502 wrote:What do you want to do exactly, are you going to rename many files in different folders using the same principle?

This will go through all sub-folders in the Batch files and check to see it there is a avi file and srt file, if there, it will rename the srt file with the same name of the avi file.

That will be repeated on all sub-folders exist in the Batch files folder.

Any folder MUST contain One AVI file and One SRT file, or all your files will be overwritten or deleted
This is not Tested

Code: Select all

@Echo Off
set "Parent_Folder=%userprofile%\desktop"
Setlocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in ('Dir /B /S "%Parent_Folder%\*.avi"') Do (
   PUSHD %%~dpA
   IF Exist "*.avi" (
      IF Exist "*.srt" (
         Ren "*.srt" "%%~nA.srt"
      )
   )
   POPD
)


Just want to rename the .srt file with the name of the .avi file that are in the present folder. So I will just have one .avi file, one .srt file and the batch file in the same folder.

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 07:07
by Kwan
Squashman wrote:Assume this is you over on TechGuy as well.
http://forums.techguy.org/dos-other/107 ... -file.html

Don't need to use the delims to remove the file extension. Just use the command modifiers.

%%~na


How do I use that command?
Can you demonstrate with the rest of the code please?

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 07:17
by Kwan
It works this way:

Code: Select all

@echo off 

dir *.mp3 /b "C:\Batch files" >>filelist.txt
for /f "tokens=1* delims=|" %%A in (filelist.txt) do (
    Ren "*.srt" "%%~nA.srt"
)

del filelist.txt


Thanks for your help guys :D

Re: Batch to copy the name of .avi file to a .srt file

Posted: 19 Oct 2012 07:31
by foxidrive
This should rename a .srt file to the same name as the avi file in the folder.

Code: Select all

@echo off
for %%a in (*.avi) do ren *.srt "%%~na.srt"

Re: Batch to copy the name of .avi file to a .srt file

Posted: 02 May 2013 06:16
by psychoid69
Sorry to bump into this old thread, but I need help with a similar problem. I want to make a script that would work with more .avi and .srt files in the same folder.

This is what I've got so far:

Code: Select all

@echo off
for /f "tokens=*" %%P in ('DIR /B /O:N *.avi') do (
for /f "tokens=*" %%R in ('DIR /B /O:N *.srt') do (
ren %%R %%~nP.srt
)
)


It's obvious to me, why it doesn't work (every .srt file would be renamed to the name of the last .avi file in foler), however I have no more ideas how to solve it... I was thinking, if for could somehow take two parameters, like for /f "tokens=*" %%P in ('DIR /B /O:N *.avi') and %%R in ('DIR /B /O:N *.srt'), but that's not how for works unfortunately.

Please help. :)

EDIT: I have made a new thread.

Re: Batch to copy the name of .avi file to a .srt file

Posted: 02 May 2013 06:19
by abc0502
it would be better to start a new thread and post more details on how the files is named.
I mean tell us if there any similarities between the avi file names and the srt file names.