Winrar CommandLine & Powershell : Exclude Full Directory Path Structure

Saurabh Wahile picture Saurabh Wahile · Jun 16, 2014 · Viewed 7.9k times · Source

Suppose I have a directory structure like

C:\Users\Desktop\abc\d

I want to rar archive the abc folder so that the structure of rar is:

abc\d

When I try using powershell to archive, winrar replicates the full path inside the archive, like:

\Users\Desktop\abc\d

I dont want the full path to be created inside the archive Here's the script: https://gist.github.com/saurabhwahile/50f1091fb29c2bb327b7

What am I doing wrong?

Answer

Mofi picture Mofi · Jul 4, 2014

Use the command line:

Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc"

Rar.exe is the console version of WinRAR stored in same directory as WinRAR.exe. You can use this command line also with WinRAR.exe if you want to see the compression process in a graphic window.

a is the command and means add files to archive.

-r is a switch to recursively add all files and subdirectories including empty subdirectories to the archive.

-ep1 is another switch which results in execluding base directory.

For this command line the base directory is "C:\Users\Desktop\" and therefore the created archive Test.rar contains only abc and all files and subdirectories in directory abc which is what you want.

One more hint: Using the command line

Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc\"

results in adding all files and subdirectories of directory abc to the archive, but without directory name abc being also stored in the archive. The backslash at end makes this difference.

In other words: On using switch -ep1 everything up to last backslash in file/directory specification is not added to the archive.

For more information about available switches see the text file Rar.txt in the program files directory of WinRAR.