Reputation: 13
I am trying to add 2 different files to a zip-archive using tar.exe. The folder structure is as follows:
I'd like to add File1, File2 and all Files of SubDirectory4 to the archive with relative paths.
The command which I am using is while the current directory is RootFolder:
tar.exe -cf archive.zip -C SubDirectory1 File1.txt -C SubDirectory2\SubDirectory3 File2.txt SubDirectory4
After executing it gives the following error: tar.exe: could not chdir to 'SubDirectory2\SubDirectory3'
I couldn't find similiar problems or neither was the documentation of the -C flag good enough for me to understand the problem.
Does anyone have an idea why it doesnt work?
Upvotes: 1
Views: 1557
Reputation: 11940
Compo has shown correctly how -C can navigate relative to the Arc to store without folders, and thus his is the correct answer to the Original Posed Question
>tar -tf archive.zip
File1.txt
File2.txt
File3.txt
File4.txt
However for those that are looking how to store files relative to the root Arc
use this method or variation remember to use "double quotes" for spaces, either for "path name/file name"
NOTE a tar file is NOT a Zip so by default should never use that extension. I am correcting myself since tar can build a valid zip file. see later
Always name the archive in a meaning full way here I use root to show pedigree and .tar to indicate how it may be extracted.
tar -f root.tar -c SubDirectory1\File1.txt SubDirectory2\SubDirectory3\File2.txt SubDirectory4\*.*
on later extraction the subfolders and their contents will be preserved relative to the root
>tar -tf root.tar
SubDirectory1/File1.txt
SubDirectory2/SubDirectory3/File2.txt
SubDirectory4/File3.txt
SubDirectory4/File4.txt
It may be interesting to know, Tar can build a valid.zip and can list and extract them. (note the difference in reporting contents).
>tar -tf root.zip
SubDirectory1/File1.txt
SubDirectory2/SubDirectory3/
SubDirectory2/SubDirectory3/File2.txt
SubDirectory4/File3.txt
SubDirectory4/File4.txt
We can extract those subfolder simply (and without visible feedback)
errorlevel 0 All files were processed successfully.
errorlevel 1 An error occurred.
using >tar -xf root.zip
To build a zip file we need to add the -auto switch
tar -a -f root.zip -c SubDirectory1\File1.txt SubDirectory2\SubDirectory3\File2.txt SubDirectory4\*.*
Historic note for native answers prior to Windows 10 you could only use system wide early PowerShell archiving see Powershell one-liner not working from cmd from Windows 10 onwards Tar was available see Zip from command line with Windows 11
Upvotes: 1
Reputation: 38719
When you cd
'd into SubDirectory1
that became the current directory within the command, so in order to cd
into SubDirectory3\Subdirectory4
you need to make that relative to SubDirectory1
not the original directory. Then when you cd
into SubDirectory4
, that will need to be relative to SubDirectory3
.
Given the above, and with RootFolder
as your current directory, try this instead:
%SystemRoot%\System32\tar.exe -cf "archive.zip" -C ".\SubDirectory1" "File1.txt" -C "..\SubDirectory2\SubDirectory3" "File2.txt" -C "..\..\SubDirectory4" "File3.txt" "File4.txt"
If you wanted the entire content of SubDirectory4
then change the last glob accordingly:
%SystemRoot%\System32\tar.exe -cf "archive.zip" -C ".\SubDirectory1" "File1.txt" -C "..\SubDirectory2\SubDirectory3" "File2.txt" -C "..\..\SubDirectory4" "*"
I have used double-quotes here as best practice, although in the case of the provided file and directory names you've used, those can be omitted if preferred. I have also used the full path and extension to the default Windows 10 version of tar.exe
, if you do not care about robust code, and wish to gamble that the %Path%
and %PATHEXT%
variables have not been corrupted, then feel free to change %SystemRoot%\System32\tar.exe
to tar
.
Upvotes: 4