Rename files from a string in multiple text files?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Rename files from a string in multiple text files?

#1 Post by tlm2408 » 01 Aug 2017 01:28

I am trying to rename a bunch of .m3u files based on the text between ", and (s- in each file.

Here are two examples
in channel1.m3u

Code: Select all

#EXTINF:-1 group-title="Documenteries" logo="/img/icon.png",: Animal Planet   (s-6)
http://*********.m3u8

in channel2.m3u

Code: Select all

#EXTINF:-1 group-title="Misc" logo="/img/icon.png", PAC-12 Net. (Bay Area) 1080P   (s-6)
http://*********.m3u8

The text might contain characters including

Code: Select all

"& (text) . * : | ! and -"
So I need to remove the illegal characters. There is also white space before and after that I'd like to remove.

Here is one the batch files I have tried so far

Code: Select all

@echo off
cd Renamed_Files
for /f "delims=" %%i in ('dir /a-d/b *.m3u') do (
   set "nname="
   set "fname=%%~i"
   for /f "tokens=* delims=,(" %%f in ("%%~i") do if not defined nname set "nname=%%f"
   setlocal enabledelayedexpansion
   
   echo rename "!fname!" "!nname!.m3u"
   endlocal
)
pause


This gives me everything before the comma. If I change delims=,( to delims=( it gives me everything up to the parenthesis, so I get the text I'm after, but I get all the text before it as well.
The characters before the text I want are always ", and the characters after are always (s- if that helps.


This another one I have tried

Code: Select all

@echo off&setlocal
cd Renamed_Files
for /f "delims=" %%i in ('dir /a-d/b Channel*.txt') do (
   set "nname="
   set "fname=%%~i"
   for /f "usebackq delims=" %%f in ("%%~i") do if not defined nname set "nname=%%f"
   setlocal enabledelayedexpansion
   set "nname=!nname:~85,40!"
   echo rename "!fname!" "!nname!".txt
   endlocal
)
pause


This gives me the info I want, but it also shows illegal characters. If I remove the echo it outputs "system cannot find path specified", "filename, directory name, or volume label syntax is incorrect." and some files have "A duplicate file name exists, or the file cannot be found."

I can do simple things with batch files, but this is a bit over my head.
Last edited by tlm2408 on 01 Aug 2017 03:59, edited 1 time in total.

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Rename files from a string in a text file?

#2 Post by Hackoo » 01 Aug 2017 02:54

You can use this function to replace a special Char :

Code: Select all

@echo off
setlocal enabledelayedexpansion
 
SET TEST=Example1 : "< = > & ^^^! |"
SET TEST2=Example2 : ">>=^^^!>=^^^!<====<^^^!<=^^^!<=<=<&&<=^^^!<=<=|<&^=^=^!<||&==="
 
echo;%TEST%      before Replace
call :ReplaceSpecialChar TEST "_"
echo;%TEST%      after Replace
 
echo;
 
echo;%TEST2%     before Replace
call :ReplaceSpecialChar TEST2 "_"
echo;%TEST2%     after Replace
pause>nul&exit

:ReplaceSpecialChar
call :ReplaceSpecialCharTmp %1 "&" %2
call :ReplaceSpecialCharTmp %1 "<" %2
call :ReplaceSpecialCharTmp %1 ">" %2
call :ReplaceSpecialCharTmp %1 "|" %2
call :ReplaceSpecialCharTmp %1 "=" %2
call :ReplaceSpecialCharTmp %1 "^^^!" %2
goto :eof
:ReplaceSpecialCharTmp
set work=!%1!
set /a i = 0
:ReplaceLoop
if "!work:~%i%,1!"=="" (set %1=!work!&goto :eof)
if not "!work:~%i%,1!"=="%~2" (set /a i += 1&goto ReplaceLoop)
set head=!work:~0,%i%!
set /a i += 1
set work=!head!%~3!work:~%i%!
goto :ReplaceLoop

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: Rename files from a string in a text file?

#3 Post by tlm2408 » 01 Aug 2017 03:16

Thank you Hackoo,
I do have a newbie question though if you don't mind and have the time.
How do I incorporate this into the second example in my first post?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename files from a string in multiple text files?

#4 Post by penpen » 01 Aug 2017 05:46

Are you sure you have posted the right source files?
Both don't do, what you've written they should do.
Beside this, the m3u-files seem to be inconsistent (one with a ':' after the ',', another without).

If a comma after a doublequote (sometimes followed by a colon) and a space character seperates the part from what you need with all before, then this might help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
cd Renamed_Files
for %%a in (*.m3u) do (
   set "nname="
   set "fname=%%~a"
   for /f "usebackq tokens=* delims=" %%b in ("%%~a") do if not defined nname set "nname=%%~b"

   setlocal enableExtensions enableDelayedExpansion
   rem: remove first part
   set ^"nname=!nname:*",=!"
   if "!nname:~0,1!" == ":" set "nname=!nname:~1!"
   if "!nname:~0,1!" == " " set "nname=!nname:~1!"

   rem: remove illegal characters " and ?
   set ^"nname=!nname:"=!"
   set "nname=!nname:?=!"

   rem: remove all other illegal characters except *
   for %%b in ("\" "/" ":" "<" ">" "|") do set "nname=!nname:%%~b=!"

   rem: remove illegal character *
   call :strlen "nname"
   set /a "maxIndex=!errorlevel!-1"

   for /l %%b in (!maxIndex!, -1, 0) do (
      if "*" == "!nname:~%%~b,1!" (
         set "last=!nname:~%%~b!"
         set "nname=!nname:~0,%%~b!!last:~1!"
      )
   )

   rem: remove last "  *(s..." part
   set "nname=!nname!\"
   for %%a in ("!nname:(s=" "(s!") do set "last=%%~a"
   for %%a in ("!last!") do set "nname=!nname:%%~a=!\"
   for %%a in ("!nname: =" " !") do set "nname=!nname: \=\!"
   set "nname=!nname:\=!"

   rem: avoid duplicate filenames
   if exist "!nname!.m3u" (
      set "index=1"
      set "todo=true"
      for %%a in ("!nname!*.m3u") do if defined todo (
         if not exist "!nname! !index!.m3u" (
            set "nname=!nname! !index!"
            set "todo="
         )
         set /A "index+=1"
      )
   )
   echo rename "!fname!" "!nname!.m3u"

   endlocal
)

endlocal

goto :eof


:strlen
:: %~1   environment variable containing the string to process
setlocal enableExtensions enableDelayedExpansion
if not "!%~1:~8190!" == "" exit /b 8191
set "str=A!%~1!"
set "len=0"

for %%a in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
   set /a "len+=%%a"
        for %%b in (!len!) do if "!str:~%%b,1!" == "" set /a "len-=%%~a"
)
for %%a in (!len!) do (
   endlocal
   exit /b %%~a
)
exit /b -1


:: Note:
:: Illegal filename characters (Windows, NTFS; depends on OS and used filesystem):
:: \/:*?"<>|


penpen

Edit:Replaced "*.m3u.txt" with *.m3u".
Edit2: Removed two minor bugs, see below.

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: Rename files from a string in multiple text files?

#5 Post by tlm2408 » 01 Aug 2017 06:09

Thank you Penpan,
That is so close to being perfect. The only thing is it removes the first character from some of the files. There are some files that have the colon after the comma and some that don't.

Also when I remove the echo it says system cannot find the file specified.

I think I found the problem with the first letter getting cut off. Some of the files have ",text I want (s-. So there is no space after the comma and others do have the space between the comma and the text.
Last edited by tlm2408 on 01 Aug 2017 06:27, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename files from a string in multiple text files?

#6 Post by penpen » 01 Aug 2017 06:25

tlm2408 wrote:Also when I remove the echo it says system cannot find the file specified.
You need to give an examle, where this issue happens.

tlm2408 wrote:I think I found the problem with the first letter getting cut off. Some of the files have ",text I want (s-. So there is no space after the comma and others do have the space between the comma and the text.

In that case replace

Code: Select all

   if "!nname:~0,1!" == ":" set "nname=!nname:~1!"
   set "nname=!nname:~1!"
with

Code: Select all

   if "!nname:~0,1!" == ":" set "nname=!nname:~1!"
   if "!nname:~0,1!" == " " set "nname=!nname:~1!"


penpen

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: Rename files from a string in multiple text files?

#7 Post by tlm2408 » 01 Aug 2017 06:34

Thank you so very much for your help with this I really appreciate it.

The edit you just made works great thank you.
If I remove the echo from this line

Code: Select all

echo rename "!fname!" "!nname!.m3u"

It tells me it cannot find the file specified. At the moment I am running this from the same folder that the files are in.
When I run this with the echo it displays rename "%~i" "Animal Planet.m3u"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename files from a string in multiple text files?

#8 Post by penpen » 01 Aug 2017 07:42

tlm2408 wrote:When I run this with the echo it displays rename "%~i" "Animal Planet.m3u"
Ups, my fault!

Replace

Code: Select all

   set "fname=%%~i"
with

Code: Select all

   set "fname=%%~a"

I also will update the above code.

penpen

tlm2408
Posts: 31
Joined: 01 Aug 2017 01:19

Re: Rename files from a string in multiple text files?

#9 Post by tlm2408 » 01 Aug 2017 13:45

WOW!!!! That works great. Thank you very, very much.

Post Reply