Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Kwan
- Posts: 7
- Joined: 19 Oct 2012 05:06
#1
Post
by Kwan » 19 Oct 2012 05:12
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
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#2
Post
by abc0502 » 19 Oct 2012 05:30
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
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#4
Post
by abc0502 » 19 Oct 2012 06:19
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 deletedThis is not TestedCode: 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
)
-
Kwan
- Posts: 7
- Joined: 19 Oct 2012 05:06
#5
Post
by Kwan » 19 Oct 2012 07:01
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
-
Kwan
- Posts: 7
- Joined: 19 Oct 2012 05:06
#6
Post
by Kwan » 19 Oct 2012 07:04
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 deletedThis is not TestedCode: 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.
-
Kwan
- Posts: 7
- Joined: 19 Oct 2012 05:06
#7
Post
by Kwan » 19 Oct 2012 07:07
How do I use that command?
Can you demonstrate with the rest of the code please?
-
Kwan
- Posts: 7
- Joined: 19 Oct 2012 05:06
#8
Post
by Kwan » 19 Oct 2012 07:17
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

-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 19 Oct 2012 07:31
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"
-
psychoid69
- Posts: 18
- Joined: 02 May 2013 06:09
#10
Post
by psychoid69 » 02 May 2013 06:16
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.
Last edited by
psychoid69 on 02 May 2013 07:21, edited 1 time in total.
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#11
Post
by abc0502 » 02 May 2013 06:19
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.