VideoHelp Forum
+ Reply to Thread
Results 1 to 6 of 6
Thread
  1. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    I got tired of replacing the above, so I wrote this.
    replace-dots.cmd
    Code:
    @echo off
    :: Replace all the dots in a file name.
    :: "How.to.milk.a.moo.cow.mp4" becomes "How to milk a moo cow.mp4"
    
    for %%a in (*.avi, *.mp4, *.mkv, *.srt, *.aac, *.mp3) do call :process "%%a"
    goto :end
    
    :process
    setlocal
    set ext=%~x1
    set tmp1=%~1
    set tmp2=%tmp1:.= %
    set tmp3=%tmp2:  = %
    if "%ext%"==".avi" set tmp4=%tmp3: avi=.avi%
    if "%ext%"==".mp4" set tmp4=%tmp3: mp4=.mp4%
    if "%ext%"==".mkv" set tmp4=%tmp3: mkv=.mkv%
    if "%ext%"==".srt" set tmp4=%tmp3: srt=.srt%
    if "%ext%"==".aac" set tmp4=%tmp3: aac=.aac%
    if "%ext%"==".mp3" set tmp4=%tmp3: mp3=.mp3%
    ren "%~1" "%tmp4%"
    endlocal
    goto :eof
    
    :end
    Cheers.
    Last edited by pcspeak; 19th Apr 2024 at 19:46.
    Quote Quote  
  2. Not wanting to dispute simple window batch script posted, it is simple and does what it suppose to, fast and simple.

    Just bringing attention to cross platform python, where a simple graphical gui could be used to confirm renaming. Arguments could be files or a directory (files in it will be renamed) or no argument, just double clicking a python file (current directory will be used for renaming).
    All with pop up to confirm renaming (only once for all arguments if more arguments).

    Code:
    """
    Replace all the dots in a file name.
    "How.to.milk.a.moo.cow.mp4" becomes "How to milk a moo cow.mp4"
    """
    
    import sys
    import os
    from pathlib import Path
    import tkinter
    from tkinter import messagebox
    
    CONFIRM = [True]
    
    
    def rename(path):
        confirm()
        #removes dots from path name
        path = Path(path)
        stem = path.stem
        new_stem = stem.replace(".", " ")
        new_path = path.with_name(f"{new_stem}{path.suffix}")
        path.rename(new_path)
     
    
    def rename_directory_paths(directory):
        EXTENSIONS = {".avi", ".mp4", ".mkv", ".srt", ".aac", ".mp3"}
        directory = Path(directory)
        if not directory.is_dir():
            raise ValueError(f"Not a directory: {directory}")
        for path in directory.iterdir():
            if path.suffix in EXTENSIONS:
                rename(path)
    
    
    def rename_paths(paths):
        for path in paths:
            rename(path)
    
    def confirm():
        if not CONFIRM[0]:
            return
        #asks if wanting renaming, one time only for all arguments
        root = tkinter.Tk()
        root.withdraw()
        result = messagebox.askquestion('Rename dots to spaces in filenames', 'Are you sure to rename filenames?')
        if result == "yes":
            CONFIRM[0] = False
            root.destroy()
        elif result == "no":
            root.destroy()
        root.mainloop()
        if result == "no":
            exit()
    
    
    if __name__ == "__main__":
        """
        Usage:
        python remove_dots.py  file.1.mp3  file.2.mp4 ...
        or
        python remove_dots.py  C:/my_dir
        or
        python remove_dots.py  # will rename fles in current directory
        """
        args = sys.argv[1:]
        if args:
            if Path(args[0]).is_dir():
                directory = args[0]
                rename_directory_paths(directory)
            else:
                rename_paths(args)
        else:
            directory = Path('.')
            rename_directory_paths(directory)
    Quote Quote  
  3. @pcspeak...

    Don't have an issue with dots. Maybe that's a Linux thing. In Windows it is underscores that I'm always tidying up. This will save quite a bit of time...Thanks
    Quote Quote  
  4. Member
    Join Date
    Dec 2005
    Location
    Canada
    Search Comp PM
    @pcspeak

    Good idea : can you modify it to recurse through folders?
    Quote Quote  
  5. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Originally Posted by sambat View Post
    Good idea : can you modify it to recurse through folders?
    This should work.
    Code:
    @echo off
    :: Replace all the dots in a file name.
    :: "How.to.milk.a.moo.cow.mp4" becomes "How to milk a moo cow.mp4"
    
    for /r %%a in (*.avi, *.mp4, *.mkv, *.srt, *.aac, *.mp3) do call :process "%%a"
    goto :end
    
    :process
    setlocal
    set ext=%~x1
    set tmp1=%~nx1
    set tmp2=%tmp1:.= %
    set tmp3=%tmp2:  = %
    if "%ext%"==".avi" set tmp4=%tmp3: avi=.avi%
    if "%ext%"==".mp4" set tmp4=%tmp3: mp4=.mp4%
    if "%ext%"==".mkv" set tmp4=%tmp3: mkv=.mkv%
    if "%ext%"==".srt" set tmp4=%tmp3: srt=.srt%
    if "%ext%"==".aac" set tmp4=%tmp3: aac=.aac%
    if "%ext%"==".mp3" set tmp4=%tmp3: mp3=.mp3%
    ren "%~dpnx1" "%tmp4%"
    endlocal
    goto :eof
    
    :end
    Last edited by pcspeak; 20th Apr 2024 at 19:12.
    Quote Quote  
  6. Member
    Join Date
    Dec 2005
    Location
    Canada
    Search Comp PM
    Great work, thank you very much - it does work.
    I removed the dots from a whole bunch of files in a few seconds.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!