Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

can somebody tweak/edit this batch command so that it will go to the root directory of each folder before it archive it.

for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"

basically i have many folders which i want to archive separately. but the batch command above archives the individual folder on the root/main directory. so extracting them will cause doubled folder.

i need it to be like this. main directory with many folders, then go to individual folders then archive them. hope somebody helps.

share|improve this question
add comment

1 Answer 1

Well, your description of desired results isn't exactly clear. Below is a hypothetical layout of a directory tree.

C:\TopDir
├───Dir1
│   └───[Dir1 files]
│
├───Dir2
│   ├───SubDir2.1
│   │   └───[files]
│   │
│   ├───SubDir2.2
│   │   └───[files]
│   │
│   ├───SubDir2.3
│   │   └───[files]
│   │
│   └───[Dir2 files]
│
├───More Dirs...
│  
└───[TopDir files]

Below is what you have after you have run the command in question (note that you are not removing any of the files you archived, so they still remain). You have a bunch of .zip files from the archiving process in the root folder (which I understand is what you don't want).

C:\TopDir
├───Dir1
│   └───[Dir1 files]
│
├───Dir2
│   ├───SubDir2.1
│   │   └───[files]
│   │
│   ├───SubDir2.2
│   │   └───[files]
│   │
│   ├───SubDir2.3
│   │   └───[files]
│   │
│   └───[Dir2 files]
│
├───More Dirs...
│
├───Dir1.zip
├───Dir2.zip
├───Dirx.zip...
└───[TopDir files]

If you extract these Zip files from this same folder (the root folder), they will extract into the existing directory structure, and prompt you when when they encounter a file overwrite situation. It will not create "double folder". I assume by double folder, you mean a structure of this type:

C:\TopDir
├───Dir1
│   ├───[Dir1 files]
│   └───Dir1
│       └───[Dir1 files]

Now if you move the zip files into their respective folders and unzip them, then you will end up with double folders.

Here is the command that should do what I think you want - place a zip file of each folder's contents into that folder:

for /d %%x in (*) do cd %%x && 7z a -tzip -r %%x.zip && cd..

The -tzip option makes sure the file is a .zip file, .7z is 7zip's default zip format. Since this is in a batch file, you can also construct it this way:

for /d %%x in (*) do (
  cd %%x
  7z a -tzip -r %%x.zip
  cd..
)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.