Search code examples
winrar

How to edit a .bat file in the Winrar window explorer?


Has Winrar another way to edit bat file directly in explorer?

I click in a zip file it's show me a Winrar popup with many files, double click in some file open it in default program (.txt in Notepad++, .sql in Toad, etc.), but double click in bat files run the bat. Alt + v is a shortcut to view file, but it does not allow me to edit the bat file.

The only way is drag the file to Windows, edit and drag back to Windows.


Solution

  • WinRAR has the feature to temporarily extract a double clicked file to Folder for temporary files as defined on tab Paths in Settings opened from menu Options, then start the application associated with that file, and after started application terminated, repack the file if modified at all back into the archive.

    That is also done for executable files except the option File types to exclude from extracting on tab Security is enabled in Settings opened from menu Options with extracting by default the entire archive as defined under Unpack everything for on tab Viewer of Settings.

    But the default behavior on double click can be customized on tab Viewer of Settings. After selecting there Ask instead of Internal viewer for Viewer type and entering C:\Windows\Notepad.exe as External viewer name a double click on a *.bat file inside an archive results in prompting the user for viewing with internal or external viewer or opening the file with associated program. Of course *.bat should not be in list of setting Unpack everything for.

    By clicking now on button External viewer (Notepad.exe) the batch file is extracted to Folder for temporary files, then Notepad is started with that file for viewing and for editing. When finishing viewing/editing the batch file and exiting Notepad, WinRAR detects a modification and asks the user if the modified file should be updated in the archive.

    There is the button Help on each Settings dialog. Please make use of it and read in help of WinRAR.

    If always the same batch file inside an archive file should be edited, I suggest to automate this task as much as possible by using a batch file.

    @echo off
    setlocal
    set "FileToEdit=Test.bat"
    set "PathInArchive="
    set "DefaultArchive=C:\Temp\Test.zip"
    
    rem Set a title for the command prompt window and determine name
    rem of this batch file with full path for a possible error message.
    title Update %FileToEdit%
    set "BatchFile=%~f0"
    
    rem Use a standard archive file if none is specified as first parameter.
    if "%~1"=="" (
        set "ArchiveFile=%DefaultArchive%"
    ) else (
        set "ArchiveFile=%~f1"
    )
    
    rem Test if the archive file exists at all.
    if not exist "%ArchiveFile%" (
        call :ErrorMessage "Archive file %ArchiveFile% does not exist."
        exit /B
    )
    
    rem Make sure path in archive ends with a backslash
    rem if a path to file in archive is defined at all.
    if not "%PathInArchive%" == "" (
        if not "%PathInArchive:~-1%" == "\" (
            set "PathInArchive=%PathInArchive%\"
        )
    )
    
    rem Extract the file to edit to directory for temporary files.
    "%ProgramFiles%\WinRAR\WinRAR.exe" e -cfg- -ibck -y -- "%ArchiveFile%" "%PathInArchive%%FileToEdit%" "%TEMP%\"
    if errorlevel 1 (
        call :ErrorMessage "Failed to extract file %PathInArchive%%FileToEdit%"
        exit /B
    )
    
    rem Start Windows Notepad to edit the temporary extracted file.
    start "" /wait %windir%\Notepad.exe "%TEMP%\%FileToEdit%"
    
    rem Define the option -ap with path in archive if needed at all.
    set "ArchivePath="
    if not "%PathInArchive%" == "" set "ArchivePath=-ap"%PathInArchive%""
    
    rem Update the edited file in archive and delete it on success.
    "%ProgramFiles%\WinRAR\WinRAR.exe" u %ArchivePath% -cfg- -df -ibck -ep -y -- "%ArchiveFile%" "%TEMP%\%FileToEdit%"
    if errorlevel 1 (
        del "%TEMP%\%FileToEdit%" 2>nul
        call :ErrorMessage "Failed to update file %PathInArchive%%FileToEdit%"
        exit /B
    )
    
    rem Exit batch processing.
    exit /B
    
    rem Subroutine to output an error message.
    :ErrorMessage
    echo Error detected by:  %BatchFile%
    echo On processing file: %ArchiveFile%
    echo.
    echo Error: %~1
    echo.
    endlocal
    pause
    exit /B
    

    The values assigned to the variables FileToEdit, PathInArchive and DefaultArchive at top of the batch file should be defined appropriately.

    By creating a shortcut (*.lnk) to this batch file in SendTo subfolder of folder %USERPROFILE% with a suitable name, right clicking on an archive file with the batch file to modify with fixed name and path inside archive and clicking in submenu Send To on this shortcut results in extracting the batch file to folder for temporary files, opening Windows Notepad for editing and after repacking finally the modified batch file into the archive file.

    Open Help topics in menu Help in WinRAR and click on tab Contents on item Command line mode. The help pages under this contents list item explain the used Commands e and u as well as the used Switches.

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • call /?
    • del /?
    • echo /?
    • endlocal /?
    • exit /?
    • if /?
    • pause /?
    • rem /?
    • set /?
    • setlocal /?
    • start /?
    • title /?