Search code examples
batch-rename

Rename & Copy Multiple Files (Different names) to One Name With Numbers


how can I rename or copy multiple files (with different name without numbers like "jhf.mp4" "JJHGF.flv" ..) to one name with numbers like (input_01.mp4 input_02 ....) (order by case) by batch file by giving just path of (folder files) to batch file or dropping the files to it (patch file). -- MEAN USING VARIABLE OR (dropping with %*) .

Note 1: I need this technique for encoding animes with X264 if any other good ideas I'll be happy to hear it.

Note 2: I'm using Windows 10.


Solution

  • I think you can solve this problem using WSHost-scripts. WSHost-system should be preinstalled on any modern Windows computer.


    I designed a system of utilities to tackle your problem:

    1. build-mapping-to-normalized-filenames.js

    2. map-filenames.js

    3. cleanup.cmd

    4. before.cmd

    5. after.cmd

    You can find source code for 3-4-5 at the bottom of this answer, and for 1-2 - in separate answers (there is a limit of characters per answer).

    1 and 2 are workhorses of this system. Powerful but not user-friendly. Yes you can use them as stand-alone utilities if you understand what input they use.

    3-4-5 - are glue that bind this system and expose user-friendly controls.

    This system is dependent on absolute paths. But all absolute paths are set as variables at the start of 3-4-5 - so if you want to reorganize system - you need change things there and only there.

    This is how it should be deployed at filesystem:

    folders:
    C:\1\     <- this is the root of system
    C:\1\aOrig    <- folder for original files
    C:\1\norm\    <- folder with files that are part of this system + temp folders
    C:\1\norm\backend   <- place for non-user-friendly utilities
    
    files:
    C:\1\norm\backend\build-mapping-to-normalized-filenames.js
    C:\1\norm\backend\map-filenames.js
    C:\1\norm\after.cmd
    C:\1\norm\before.cmd
    C:\1\norm\cleanup.cmd
    
    +files: (these are example international filesnames that you can use for playground to check how system works to make yourserf familiar with it)
    C:\1\aOrig\English.txt     <- test files, you could add unique content to each
    C:\1\aOrig\Русский.txt
    C:\1\aOrig\العربية.txt
    C:\1\aOrig\ܐܪܡܝܐ.txt
    C:\1\aOrig\中文.txt
    C:\1\aOrig\日本語.txt
    

    How to use it:

    You open your console and go to dir **C:\1\norm**

    You call programs in this order:

    1. cleanup.cmd

    2. before.cmd

    3. What happened now is that two interesting temp folders are created:

      • C:\1\norm\bTemp <- here are normalized files.

      • C:\1\norm\mappingTemp <- it contains a file with mapping between original and normalized names.

      What you must do now - is to process files in C:\1\norm\bTemp - that special processing you mentioned before. Right now for the purposes of learning, you can manually change extensions and contents.

    4. after.cmd

    5. cleanup.cmd

    And that's it.

    So, if we condence it that's your typical session here, plain and simple -

    cleanup.cmd
    before.cmd
    ..do your stuff with files at "C:\1\norm\bTemp"...
    after.cmd
    cleanup.cmd
    

    Also, you don't need command line to use those - you can open **C:\1\norm** in Explorer and double click those utilities in the order that I explained above - cleanup-before-(..manually do your work with bTemp..)-after-cleanup


    Listing for program cleanup.cmd :

    @ECHO OFF
    
    :: name of this script...
    :: useful for debugging...
    SET me=%~nx0
    
    :: aOrig - directory that contains source files...
    :: bTemp - temporary working directory...
    :: mTemp - temporary directory for mapping.txt...
    SET aOrig=C:\1\aOrig
    SET bTemp=C:\1\norm\bTemp
    SET mTemp=C:\1\norm\mappingTemp
    
    
    :: Removing temporary folders...
    :: Be warned - this RD is working
    :: in quiet mode /Q - so it wouldn't ask you politely
    :: about del y/n - thus, please make shure that
    :: %aOrig% doesn't point to anything
    :: important for your personal data or system...
        IF EXIST "%bTemp%" RD /S /Q "%bTemp%"
        IF EXIST "%mTemp%" RD /S /Q "%mTemp%"
    
        IF %ERRORLEVEL% NEQ 0 (
            ECHO Script %me% failed on step cleanup-1: [Removing temporary folders]...
            ECHO Error: %ERRORLEVEL%
            EXIT /B 1
        )
    
    :: Successfully finished
        ECHO.
        ECHO Script %me% successfully finished!
    

    Listing for program before.cmd :

    @ECHO OFF
    
    :: name of this script...
    :: useful for debugging...
    SET me=%~nx0
    
    :: aOrig - directory that contains original files...
    :: bTemp - temporary working directory...
    :: mTemp - temporary directory for mapping.txt...
    SET aOrig=C:\1\aOrig
    SET bTemp=C:\1\norm\bTemp
    SET mTemp=C:\1\norm\mappingTemp
    
    :: scriptBuild - name of .js-script for building a map
    ::               from original to normalized filenames...
    :: scriptMapFi - name of .js-script interpreting
    ::               mapping file that was build by scriptBuild...
    :: mappingFile - path for file that will contain mapping between
    ::               original filenames and normalized filenames...
    SET scriptBuild=C:\1\norm\backend\build-mapping-to-normalized-filenames.js
    SET scriptMapFi=C:\1\norm\backend\map-filenames.js
    SET mappingFile=C:\1\norm\mappingTemp\mapping.txt
    
    :: creating temporary folders...
        IF NOT EXIST "%bTemp%" MKDIR "%bTemp%"
        IF NOT EXIST "%mTemp%" MKDIR "%mTemp%"
        IF %ERRORLEVEL% NEQ 0 (
            ECHO Script %me% failed on step before-1: [Creating temporary folders]...
            ECHO Error: %ERRORLEVEL%
            EXIT /B 1
        )
    
    :: now we run build-mapping-to-normalized-filenames.js ...
    :: that //U thing is important...
        CSCRIPT //U //NoLogo "%scriptBuild%" "%aOrig%" 0 1 > "%mappingFile%"
        IF %ERRORLEVEL% NEQ 0 (
            ECHO Script %me% failed on step before-2: [Building a list of normalized filenames]...
            ECHO Error: %ERRORLEVEL%
            EXIT /B 1
        )
    
    :: now we run map-filenames.js ...
    :: that //U thing is important...
        CSCRIPT //U //NoLogo "%scriptMapFi%" "%aOrig%" "%bTemp%" /NotTraining < "%mappingFile%"
        IF %ERRORLEVEL% NEQ 0 (
            ECHO Script %me% failed on step before-3: [Copy files while simultaneously changing their names to normalised form]...
            ECHO Error: %ERRORLEVEL%
            EXIT /B 1
        )
    
    :: Successfully finished
        ECHO.
        ECHO Script %me% successfully finished!
    

    Listing for program after.cmd :

    @ECHO OFF
    
    :: name of this script...
    :: useful for debugging...
    SET me=%~nx0
    
    :: aOrig - directory that contains original files...
    :: bTemp - temporary working directory...
    :: mTemp - temporary directory for mapping.txt...
    SET aOrig=C:\1\aOrig
    SET bTemp=C:\1\norm\bTemp
    SET mTemp=C:\1\norm\mappingTemp
    
    :: scriptBuild - name of .js-script for building a map
    ::               from original to normalized filenames...
    :: scriptMapFi - name of .js-script interpreting
    ::               mapping file that was build by scriptBuild...
    :: mappingFile - path for file that will contain mapping between
    ::               original filenames and normalized filenames...
    SET scriptBuild=C:\1\norm\backend\build-mapping-to-normalized-filenames.js
    SET scriptMapFi=C:\1\norm\backend\map-filenames.js
    SET mappingFile=C:\1\norm\mappingTemp\mapping.txt
    
    :: Removing files in original folder...
    :: Not recursive
    :: Be warned - this DEL is working
    :: in quiet mode /Q - so it wouldn't ask you politely
    :: about del y/n - thus, please make shure that
    :: %aOrig% doesn't point to anything
    :: important for your personal data or system...
        IF EXIST "%aOrig%" DEL /Q "%aOrig%"
    
        IF %ERRORLEVEL% NEQ 0 (
            ECHO Script %me% failed on step after-1: [Removing files from original folder]...
            ECHO Error: %ERRORLEVEL%
            EXIT /B 1
        )
    
    :: now we run map-filenames.js ...
    :: that //U thing is important...
        CSCRIPT //U //NoLogo "%scriptMapFi%" "%bTemp%" "%aOrig%" /NotTraining /MapFrom2To1 /ResultExtension:2 /FuzzyExtensionsFrom < "%mappingFile%"
        IF %ERRORLEVEL% NEQ 0 (
            ECHO Script %me% failed on step after-2: [Copy files while simultaneously changing their names to normalised form]...
            ECHO Error: %ERRORLEVEL%
            EXIT /B 1
        )
    
    :: Successfully finished
        ECHO.
        ECHO Script %me% successfully finished!