I am trying to create a command line to compress as RAR file using password through command line in Windows 7. I have installed WinRAR 5.31 x64.
The following command works for me:
rar a -r -m0 -hp"!(/!$!#!#=)\%" C:\files1.rar" *.*
The password is !(/!$!#!#=)\%
.
My problem occurs if I wanted to put double quotes "
inside my password, for example at the beginning:
rar a -r -m0 -hp""!(/!$!#!#=)\%" C:\files1.rar" *.*
The password should be "!(/!$!#!#=)\%
.
That does not work for me, I tried putting \
before of "
, but this is also not working.
Could anyone guide me through it in order to figure it out this special character in my password?
The Windows command interpreter cmd.exe
and Rar.exe
itself determine how arguments specified on command line are interpreted on parsing the command line. Argument strings containing a space or one of these characters &()[]{}^=;!'+,`~<|>
must be enclosed in double quotes. This makes it very difficult to pass a double quote character as part of an argument string to a console application, especially at begin of an argument string.
But there is a solution for this very uncommon and very specific problem caused by a password/passphrase starting with a straight double quote character which marks usually begin/end of an argument string within all characters between are interpreted literally.
The manual of console version of WinRAR is the text file Rar.txt
in program files folder of WinRAR. It can be read in this manual that Rar.exe
supports reading switches from an environment variable RAR
. By using this environment variable and special parsing of Windows command line interpreter on a SET command line it is possible to create a RAR archive from command line with a password starting with a single straight double quote character.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RAR=-hp""!(/!$!#!#=)\%%""
"%ProgramFiles%\WinRAR\Rar.exe" a -r -m0 -x"%~f0" "%USERPROFILE%\Desktop\files1.rar" *.*
endlocal
The switch -hp
is read from environment variable RAR
in addition to the other switches specified directly on RAR command line as explained by the manual.
The environment variable RAR
is defined using syntax set "variable=value"
as explained in detail by answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?
A password/passphrase with space or one of these characters &()[]{}^=;!'+,`~<|>
needs to be enclosed in double quotes on Windows command line. For that reason Rar.exe
removes from the passed password/passphrase the first and last double quote if there is one at begin and/or end. So it is not possible to define the password with "!(/!$!#!#=)\%
. The password must be defined with two additional double quotes using ""!(/!$!#!#=)\%"
to let really used password start with a straight double quote character.
In a batch file %
marks begin/end of an environment variable reference except it is escaped with one more %
.
So finally the command line set "RAR=-hp""!(/!$!#!#=)\%%""
defines the environment variable RAR
with switch -hp
passing the string "!(/!$!#!#=)\%
to Rar.exe
as password to use on encryption.
The RAR archive files1.rar
is created on user's desktop by this code as root of directory C:
is usually write-protected.
Note: Rar and WinRAR interpret *.*
different to *
as also explained in manual in comparison to Windows kernel functions interpreting them identical. Rar adds only files containing a dot in name of file into the RAR archive file on using *.*
. So you might better use just *
as wildcard.
The switch -x"%~f0"
prevents adding the batch file also into the RAR archive file if being stored in current directory on execution of the batch file. Run in a command prompt window call /?
for an explanation of %~f0
– full name of argument 0 which means batch file name with extension and full path.