Page 1 of 1

Decompress zip/rar/gzip in subdirectories and filter files

Posted: 27 Feb 2013 01:07
by glob5
I need a bat script that handles compressed file as follows: I have a directory that has bunch of .zip, .gzip and rar files in it and maybe even its subfolders. bat file should go though each compressed file and 1)if file is xyz-1.zip or xyz-1.rar or xyz-1.gzip use its own utility to uncompress it. 2)create a folder based on each compressed file i.e., xyz-1 and put its uncompressed content into the folder but not the compressed file itself. inside the xyz-1 folder if a file name contains .txt and LPI copy it to C:\folder1 and if contains LJM and .txt to C:\folder2.
appreciate any help.

Re: need help with a bat script

Posted: 27 Feb 2013 02:05
by foxidrive
Please supply:
1) the path to the decompressors
2) and the command line switches required to decompress the three archive types to a target folder.

Re: need help with a bat script

Posted: 27 Feb 2013 10:06
by glob5
Let's say we use this for extracting files in zip and rar file in the currect directory:
"C:\Program Files\7-zip\7z.exe" x "%%f"
Let's uncompress all the compressed files in the current and subdirectories. Then, search through all the extract files and folders in the current and subdirectories. if a file name contains .txt and LPI copy it to C:\folder1 and if contains LJM and .txt to C:\folder2.

Re: need help with a bat script

Posted: 27 Feb 2013 13:33
by foxidrive
1) You didn't provide the details for gzip...
2) when you say the filename contains .txt does that mean .txt is the extension?

Re: need help with a bat script

Posted: 27 Feb 2013 22:33
by glob5
1)name format of each gz file is filename_Sequence#.gz.
2)some uncompressed file names have extension of .txt.

Re: need help with a bat script

Posted: 28 Feb 2013 00:38
by foxidrive
Earlier I said this:

foxidrive wrote:Please supply:
1) the path to the decompressors
2) and the command line switches required to decompress the three archive types to a target folder.


You provided the syntax and path for two of them but not for gzip.

Do you have that detail?

Also, you want to copy files that contain a certain keyword and .txt
I asked if these file always end in a .txt extension or could be a name like "file.LJM.004.txt.2013.02.28.bin"
where the extension is not .txt but it contains .txt - see?

Do these files with the keywords always have a .txt extension?
It matters in the code used.

Re: Decompress zip/rar/gzip in subdirectories and filter fil

Posted: 28 Feb 2013 09:09
by glob5
"C:\Program Files\7-zip\7z.exe e "%%f" may work for .gz files and is the only one I know. Also .txt is only the extension. It won't be in the middle. Best is to look at the last three letter of the file name to find extension.

Re: Decompress zip/rar/gzip in subdirectories and filter fil

Posted: 28 Feb 2013 16:48
by foxidrive
Untested:

Code: Select all

@echo off
md "c:\folder1" 2>nul
md "c:\folder2" 2>nul
for /f "delims=" %%a in ('dir *.zip *.rar *.gz /b /s /a-d') do (
pushd "%%dpa"
md "%%~na" 2>nul
if /i "%%~xa"==".zip" "C:\Program Files\7-zip\7z.exe" x "%%a" -o"%%~na"
if /i "%%~xa"==".rar" "C:\Program Files\7-zip\7z.exe" x "%%a" -o"%%~na"
if /i "%%~xa"==".gz" "C:\Program Files\7-zip\7z.exe" x "%%a" -o"%%~na"
 for /f "delims=" %%b in ('dir "%%~na\*.txt" /b /a-d') do (
   for /f "delims=" %%c in ('echo "%%b"^|findstr /i "LPI .txt" ') do copy "%%~na\%%b" "C:\folder1"
   for /f "delims=" %%c in ('echo "%%b"^|findstr /i "LJM .txt" ') do copy "%%~na\%%b" "C:\folder2"
 )
popd
)

Re: Decompress zip/rar/gzip in subdirectories and filter fil

Posted: 28 Feb 2013 22:50
by glob5
Fantastic a!.

Re: Decompress zip/rar/gzip in subdirectories and filter fil

Posted: 01 Mar 2013 19:11
by glob5
this line looks for the word LPI and .txt in the file name. How this line changes to satisfy the condition if 1)LPI is either found in the file name or found inside the file itself with the file name having the extension of .txt?. 2)look for LPI only inside the file itself with the file name having the extension of .txt?.
for /f "delims=" %%c in ('echo "%%b"^|findstr /i "LPI .txt" ') do copy "%%~na\%%b" "C:\folder1"

Re: Decompress zip/rar/gzip in subdirectories and filter fil

Posted: 01 Mar 2013 22:44
by foxidrive
Changing batch code to cater for different tasks often involves large changes.

If you had stated what you wanted in the first place it could have been catered for while designing the code.

Re: Decompress zip/rar/gzip in subdirectories and filter fil

Posted: 02 Mar 2013 23:00
by glob5
yes, indeed. In general, what is the best way to search through a directory (c:\dir1) and its subs and find files that contain multiple words such Word1, word2 and word3?. I have implemented a way and works but it may not be the best way. I do not have the code handy.