7zip command line - archive name from source file name - 7zip

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"

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

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

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!!!

Related

Power shell Copy/move daily multiple .txt/.cvs files from network drive to network drive, local to network drive or vice versa

Condition
network drive (origin-f: destination O:)
file is above 50mb.
-only mentioned specific words as new file is genrated everyday with $currentdate in the filename Eg: "xxxx202206141010"
Get-ChildItem -Path "F:\*" -Include *AppliedInnovations*.txt |
where { $_.Length -gt 40mb}
Copy-Item -Destination "O:\Applied Innovations Database\" -force**
Result
OUTPUT
Directory: F:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 6/14/2022 10:50 AM 57798073 AppliedInnovations-202206141010.txt
cmdlet Copy-Item at command pipeline position 1
Supply values for the following parameters:
Path[0]:
The outcome is to transfer this file .txt FROM F: to O: and these are network drive, was given an error for UNC path so used regular path.
ANY HELP/SUGGESTIONS please thank you

cmd command seporator broken in Windows 10

This is unrelated to Java, however the error occurs when I try to compile my code.
The semicolon in the command is causing Windows cmd to fail
Example
c:\>cd c:\windows <- no error
c:\>cd c:\windows; <- the semicolon is not being used a command separator but as part of the path
The system cannot find the path specified.
cmd doesn't use semicolon as a command separator. If you want to have two commands on the same line, you need to use & or | (depending on what you're trying to do).
; is used as an argument separator on Windows, but it's not supported by most applications. It has nothing to do with cmd, and cmd isn't supposed to deal with it (applications parse their command lines, not cmd). It also doesn't apply to cmd intrinsics like cd (cd is not an application).
If you have a directory with a semicolon in the name try wrapping it in "double quotes."
Can't exactly verify your problem, but see below.
Z:\[]>dir
Volume in drive Z has no label.
Volume Serial Number is CAC7-078B
Directory of Z:\
11/27/2019 08:53 <DIR> .
11/27/2019 08:53 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 871243935744 bytes free
Z:\[]>md zz
Z:\[]>md zz;
A subdirectory or file zz already exists.
Z:\[]>md "zz;"
Z:\[]>dir
Volume in drive Z has no label.
Volume Serial Number is CAC7-078B
Directory of Z:\
11/27/2019 08:54 <DIR> .
11/27/2019 08:54 <DIR> ..
11/27/2019 08:54 <DIR> zz
11/27/2019 08:54 <DIR> zz;
0 File(s) 0 bytes
4 Dir(s) 871243935744 bytes free
Z:\[]>cd zz;
Z:\zz;[]>cd ..\zz
Z:\zz[]>cd "..\zz;"
Z:\zz;[]>
The issue has been resolved with the following
Adding -J-Djdk.lang.Process.allowAmbiguousCommands=true to netbeans_default_options= in etc/netbeans.conf
The output from the compile command has not changed, however it now finds the directories???

How to search folders and reconfigure any matching paths as output through PowerShell

I'm a newbie in windows scripting, so bear with me.
I want to find "test" folder from multiple folders. Such as
Search test folders from multiple paths:
/rbl.com/mailroot/test
/pml.com/mailroot/test
/pax.com/mailroot/
Want to get output like that:
test#rbl.com
test#pml.com
I have run this command to get results dir /S test > c:\results.txt
Output:
Volume in drive C has no label.
Volume Serial Number is 4A9E-06D4
Directory of C:\Program Files (x86)\Parallels\Plesk\Mail Servers\Mail Enable\Postoffices\rbl.com\MAILROOT
08/05/2016 12:36 AM <DIR> test
0 File(s) 0 bytes
Directory of C:\Program Files (x86)\Parallels\Plesk\Mail Servers\Mail Enable\Postoffices\pml.com\MAILROOT
08/05/2016 12:36 AM <DIR> test
0 File(s) 0 bytes
But how to extract the information like:
test#rbl.com
test#pml.com
This should do the trick
ls test -Directory -Recurse | % { "$($_.Name)#$($_.Parent.Parent.Name)" }
It searches your folders recursively for test folders, then prints out the folder's parent, its parent, and the folder's name as you want it formatted.

Uncompress recursively using 7-Zip from command line

I'm attempting to uncompress several .gz files using 7-Zip from the command line. My files are in directories like so:
Desktop/copyto/1/
file1.gz
file2.gz
Desktop/copyto/2/
file1.gz
file2.gz
file3.gz
I would like to recursively uncompress all the .gz files into each's orginal location and as well as deleting the remaining .gz files when they are done uncompressing.
I have tried the following command with no luck:
7z.exe x C:\Users\MYUSERNAME\Desktop\copyto\*\*.gz -r
I assumed that this would extract recursively. I get the error:
Processing archive: C:\Users\MYUSERNAME\Desktop\copyto\1\file1.gz
Can not open output file file1
Sub items Errors: 1
Any idea what's going on?
Given your command line, my guess is that your current working directory isn't any subdirectory of your home directory (C:\Users\MYUSERNAME) or the public user directory (C:\Users\Public), which means you probably don't have access rights. For example, if I run the following from C:\Program Files\7-Zip, I get the same error with a 7-Zip file:
C:\Program Files\7-Zip>7z x C:\Users\MYUSERNAME\Desktop\migrated\annex_k.7z -r
7-Zip [64] 9.38 beta Copyright (c) 1999-2014 Igor Pavlov 2015-01-03
Processing archive: C:\Users\MYUSERNAME\Desktop\migrated\annex_k.7z
ERROR: Can not open output file : .\annex_k\include\annex_k\errno.h
Skipping annex_k\include\annex_k\errno.h
ERROR: Can not open output file : .\annex_k\include\annex_k\handler.h
Skipping annex_k\include\annex_k\handler.h
...
Extracting annex_k\include\annex_k
Extracting annex_k\include
Extracting annex_k
Sub items Errors: 10
Archives with Errors: 1
Sub items Errors: 10
Kernel Time = 0.031 = 39%
User Time = 0.031 = 39%
Process Time = 0.062 = 78% Virtual Memory = 3 MB
Global Time = 0.080 = 100% Physical Memory = 4 MB
Notice that not even an annex_k directory was created:
C:\Program Files\7-Zip>dir /b
7-zip.chm
7-zip.dll
7-zip32.dll
7z.dll
7z.exe
7z.sfx
7zCon.sfx
7zFM.exe
7zG.exe
descript.ion
History.txt
Lang
License.txt
readme.txt
The solution is to extract to a directory in which you have access rights. You can specify an output directory using something like -oC:\Users\MYUSERNAME\Desktop\copyto\1. If you absolutely need to do this in a directory in which you don't have write access ordinarily, you'd need to run the command prompt as an administrator and extract the file as usual.

Why does the cmd.exe shell on Windows fail with paths using a forward-slash ('/'') path separator?

Just when I'd thought I'd seen it all with Windows path issues, I've now encountered a case that only fails when '/' (forward-slash) is used as the path separator is used:
C:\temp\tcbugs>mkdir "dir1 with spaces"
C:\temp\tcbugs>echo hi > "dir1 with spaces"\foo.txt
C:\temp\tcbugs>type "dir1 with spaces\foo.txt"
hi
C:\temp\tcbugs>type "dir1 with spaces/foo.txt"
The system cannot find the file specified.
What is particularly interesting about this is that it appears to be specific to the cmd.exe shell and doesn't occur in PowerShell (nor presumably in the win32 API):
PS C:\temp\tcbugs> type 'dir1 with spaces/foo.txt'
hi
Another point of interest is that changing directories with 'cd' and using '/' used as a path separator with cmd.exe does work:
C:\temp\tcbugs>mkdir dir2_no_spaces
C:\temp\tcbugs>cd ./dir2_no_spaces
C:\temp\tcbugs\dir2_no_spaces>cd ..
Yet, I can't find any reference to this particular problem anywhere online nor in MSDN's commonly cited documentation:
Naming Files, Paths, Namespaces
Which leads me to ask: why does this happen, and is there a definitive source which documents this quirk?
UPDATE:
dbenham points out that the problem is present regardless of whether spaces are in a directory name, so removed reference to it in the title and question body. Also added an example of 'cd ./' that works, while other commands don't.
Edited to remove opinion
Whether or not Windows CMD.EXE is supposed to support forward slashes in paths, the fact is sometimes it works, sometimes it doesn't, and sometimes it appears to work but gives the wrong result - AKA a bug.
It's time for some experiments :-)
All tests were run on Vista
C:\>md "c:/temp/"
C:\>REM The forward slash works with MD!
C:\>echo hello world 1>>"c:/temp/test.txt"
C:\>REM Redirection works with forward slashes!
C:\>type "c:\temp\test.txt"
hello world
C:\>REM Of course TYPE works with back slashes
C:\>type "c:/temp/test.txt"
The system cannot find the file specified.
C:\>REM But forward slash version fails
C:\>type "c:/temp\test.txt"
hello world
C:\>REM But TYPE works with forward slash as long as last slash is back slash
C:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
File Not Found
C:\>REM Note how DIR lists the directory with a \, yet fails to find any files
C:\>dir "c:/temp/*"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
File Not Found
C:\>REM DIR Still fails with forward slashes
C:\>dir "c:/temp/"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM <DIR> .
05/09/2012 09:58 PM <DIR> ..
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
2 Dir(s) 337,001,615,360 bytes free
C:\>REM But forward slash works if no file is specified!
C:\>dir "c:/temp\test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM And DIR works with forward slash as long as last slash is back slash
C:\>REM Now add another folder to the path hierarchy
C:\>md "c:/temp/temp/"
C:\>REM Still can create folder using forward slashes
C:\>copy "c:/temp/test.txt" "c:/temp/temp/"
The system cannot find the file specified.
0 file(s) copied.
C:\>REM Failed to copy with forward slashes
C:\>copy "c:/temp\test.txt" "c:/temp/temp/"
1 file(s) copied.
C:\>REM But forward slash works if last slash before file name is back slash
C:\>REM Rerun some past tests
C:\>type "c:/temp/test.txt"
The system cannot find the file specified.
C:\>REM Good - it still fails
C:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM What is going on?! :( Why did that seem to work now?
C:\>REM More on that later.
C:\>REM Now test the new folder
C:\>type "c:/temp/temp/test.txt"
The system cannot find the file specified.
C:\>REM Forward slashes still fail with TYPE
C:\>type "c:/temp/temp\test.txt"
hello world
C:\>REM But forward slash still works as long as last slash is back slash
C:\>dir "c:/temp/temp/*"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
File Not Found
C:\>REM Again, forward slashes fail, but directory path is listed properly
C:\>dir "c:/temp/temp/"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
05/09/2012 09:58 PM <DIR> .
05/09/2012 09:58 PM <DIR> ..
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
2 Dir(s) 337,001,615,360 bytes free
C:\>REM And again it works if no file is specified
C:\>dir "c:/temp/temp\test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM Again forward slashes work as long as last slash is back slash
Here is a case that clearly demonstrates a bug.
c:\>dir /s /a-d temp
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/10/2012 08:01 AM 13 test.txt
1 File(s) 13 bytes
Directory of c:\temp\temp
05/10/2012 07:57 AM 10 test.txt
1 File(s) 10 bytes
Total Files Listed:
2 File(s) 23 bytes
0 Dir(s) 337,325,191,168 bytes free
c:\>REM Note the different file sizes found in each directory
c:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/10/2012 07:57 AM 10 test.txt
1 File(s) 10 bytes
0 Dir(s) 337,325,191,168 bytes free
c:\>REM It is listing the wrong file!
One can debate whether Windows CMD is "supposed" to support forward slashes. But that last result is a bug! Even if there is operator error in using a forward slash, Windows should not give that result.
Wie had some strange behaviour with slash to and we traced it down to the fact, that a path with a leading slash isn't seen as an absolute path, so
C:\>cd /temp
C:\temp>rem works we are in the root directory
C:\temp>cd /temp
Das System kann den angegebenen Pfad nicht finden.
C:\temp>rem does't work but
C:\temp>cd \temp
C:\temp>rem \ indicates absolute path
C:\temp>cd ..
C:\>cd /temp
C:\temp> cd /ca
C:\temp\CA>rem qed
Perhaps it explains also the bug stated above - im not clear, at which directory the commands are executed.
I am not sure why the '/' is working in PS. Back to the history, DOS was based on UNIX and it is a small set of UNIX. In UNIX the path separator is '/', while in DOS it is '\'. I worked on some Windows and DOS apps before. In order to convert some UNIX pattern like commands or path and make sure they are valid DOS command or path, I wrote a small converter to transform '/' to '\' like this:
string fileNameFromWeb;
...
string windowsFile = fileNameFromWeb.repleace("/", #"\");
You may add this feature to tolerant '/' in your app in case of accessing to files in Windows. I guess PS may have this type converter to allow command or path using '/' or '\', or Windows will take '/' in file name.

Resources