dzilupl
dzilupl

Reputation: 67

7zip command line - archive name from source file name

How can I read name of source file and send it as archive name in 7zip using cmd archive option.

Now I use:

7z a -t7z V:\archive.7z V:\Backup\*.bak

I want to check bak namefile in V:\Backup (there is always only 1 file) and send it as archive.7z - for example if in V:\Backup is 1 file named "20131028_1100.bak" I want to name archive "20131028_1100.7z"

Upvotes: 3

Views: 8291

Answers (3)

Hein du Plessis
Hein du Plessis

Reputation: 3357

I found this to work:

Get-ChildItem *txt | ForEach-Object {.\7zr.exe a -t7z $($_.Name).replace('.txt','.7z') $_.FullName}

Example:

PS C:\tools> ls


    Directory: C:\tools


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022/08/01     11:00         578048 7zr.exe
-a----        2022/08/01     11:02              7 hello.txt


PS C:\tools> Get-ChildItem *txt | ForEach-Object {.\7zr.exe a -t7z $($_.Name).replace('.txt','.7z') $_.FullName}

7-Zip (r) 22.01 (x86) : Igor Pavlov : Public domain : 2022-07-15

Scanning the drive:
1 file, 7 bytes (1 KiB)

Creating archive: hello.7z

Add new data to archive: 1 file, 7 bytes (1 KiB)


Files read from disk: 1
Archive size: 133 bytes (1 KiB)
Everything is Ok
PS C:\tools> ls


    Directory: C:\tools


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022/08/01     11:00         578048 7zr.exe
-a----        2022/08/01     11:20            133 hello.7z
-a----        2022/08/01     11:02              7 hello.txt


PS C:\tools> ls

Upvotes: 1

DimKag
DimKag

Reputation: 1

7zip keep source file name to archive

As "capa" said, it is necessary to use shell commands, because 7zip does not provide a command for this case. The following edited command is available in order to operate with *.bak files and it is working for "7z" compression extension. It is more efficient than "zip" format in compression rate.

7z Format FOR %%I IN (.bak) DO "c:\Program Files\7-Zip\7z.exe" a -t7z %%~nI.7z %%~nI.

zip Format FOR %%I IN (.bak) DO "c:\Program Files\7-Zip\7z.exe" a -tZip %%~nI.zip %%~nI.

In the above commands the " * " symbol before .bak is not appeared. Please add it! Also add the " * " to the end of each command.

*Create a "bat" file for example "compressbak.bat" on *.bak files directory, copy and paste the preferred command(7z or zip), "Save" the content on "compressbak.bat"! This will create named archives of *.bak files in the same folder that *.bak files saved. Also if you running on Windows x64 and you have a x86 version of 7-Zip you have to follow the right path in order to call the 7z.exe file. "c:\Program Files (x86)\7-Zip\7z.exe"

Folder and bat file content!

I hope that it helps you!!!

Upvotes: 0

capa
capa

Reputation: 21

you need a .bat script that execute a FOR command like this: for %%X in (*) do "7zip\App\7-Zip\7z.exe" a "%%~nX.zip" "%%X"

%%X will catch the file name of every file contained in the folder where you will execute the script (in this case V:\Backup\ and only 1 file would be processed), so %%~nX.zip will take that file name

Upvotes: 0

Related Questions