Search code examples
batch-filebatch-processing

RAR files with password and enryption using batch


I have four folders in drive d: and in each folder there is a file called file.txt. If I use the batch script below, all four txt files get compressed and they are compressed without the password and without hidding the file name inside. I only want to compress the file in Folder 4. My password is MYPass and my compressed bz2 is FileCompressed.bz2

I need to only compress the file in Folder 4, assuming yes on all queries, showing no messages, maximum compression, with password, with deletion of File.txt (on Folder 4) after compression, with encription of the name of the file. I used the "rar -?" in windows prompt but I am still not knowing how to get the results I need. Can anyone help?

d:
\Folder 1\file.txt
\Folder 2\file.txt
\Folder 3\file.txt
\Folder 4\file.txt

@echo off
rar a -hpMyPass -m5 -y -df "d:\Folder 4\FileCompressed.bz2" "d:\Folder 4\File.txt"

Solution

  • Try this sample code :

    @echo off
    Title Zip Files with rar.exe in command line
    color 0A & Mode 75,10
    Set "Folder2Compress=D:\Folder 4"
    If Not exist "%Folder2Compress%\" MD "%Folder2Compress%\"
    (echo This is a test to see if the compression with encryption in winrar works or not)>"%Folder2Compress%\File.txt"
    Set "CompressedFolder=%~dp0FileCompressed.bz2"
    set "RAR_Password=MyPass"
    Set "Log=%~dpn0_Log.txt"
    
    Set "strProgramFiles=%ProgramFiles%"
    if exist "%ProgramFiles(x86)%" (
        set "strProgramFiles=%ProgramFiles(x86)%"
    ) else (
        set "strProgramFiles=%ProgramFiles%"
    )
    
    Set RAR="%strProgramFiles%\WinRAR\RAR.exe"
    If Exist %RAR% ( Goto :RAR ) else ( Goto :Fail )
    REM -----------------------------------------------------------------
    :RAR
    %RAR% a -m5 -y -df -hp%RAR_Password% "%CompressedFolder%" "%Folder2Compress%\">nul 2>&1
    IF "%ERRORLEVEL%" EQU "0" ( GOTO Succes ) Else ( Goto Fail )
    REM -----------------------------------------------------------------
    :Succes
    Echo(
    Echo All Files are zipped succesfuly ! in "%CompressedFolder%" 
    Echo All Files are zipped succesfuly ! in "%CompressedFolder%" > "%Log%"
    Start "" /MAX "%Log%"
    Timeout /T 3 /nobreak>nul
    Exit /b
    REM -----------------------------------------------------------------
    :Fail
    Color 0C
    Echo(
    echo There was an error ! 
    echo There was an error ! >"%Log%"
    Start /MAX "" "%Log%
    Timeout /T 3 /nobreak>nul
    REM -----------------------------------------------------------------