Search code examples
batch-filebatch-processingrarwinrar

How to compress text files of N number sub folder which contains N number text files?


Below is a directory structure where I have to compress all *.txt files in the app* subdirectories into a RAR archive with current date in name with deletion of archived files and folders.

I am totally new on working on command line and writing batch scripts and know only some basics.

Are there any suggestions on how to solve this task using Rar or WinRAR?

E: 
Data 
    Log
    makeRar.bat
        app1
            1.txt
            2.txt
            3.txt
            .
            .
            .
            n.txt
        app2
            1.txt
            2.txt
            3.txt
            .
            .
            .
            n.txt
        app3
            1.txt
            2.txt
            3.txt
            .
            .
            .
            n.txt
        .
        .
        .
        appn
            1.txt
            2.txt
            3.txt
            .
            .
            .
            n.txt

----------- After execution of makeRar.bat file -------------

E: 
Data 
    Log
    makeRar.bat
        app1
            4/1/2016_log.rar
        app2
            4/1/2016_log.rar
        app3
            4/1/2016_log.rar

Solution

  • I have an answer for this problem. But this solution is using a static path, and not a dynamic path. So when this batch file is executed, it produces the output exactly as wanted, but only for a specific app folder according to code below.

    set loc="C:\Program Files\WinRAR"
    cd /D %loc%
    rar.exe a -r -agYYYY-MM-DD -df -ep %source%\app1\log_.rar %source%\app1\*.txt
    

    source must be defined with path of source directory.

    EDIT:

    I found a better solution for this task for your directory structure by iterating two FOR loops where first is for getting current and subdirectories and second for getting files from subdirectories.

    set "source=C:\Program Files\WinRAR"
    
    for /R /D %%s in (.\*) do (
        echo %%s
        for %%F in (%%s\*.txt*) do (
            "%source%\rar.exe" a -r -agYYYY-MM-DD -df -ep %%s\log_.rar %%F
        ) 
    )
    pause