Decompress zip/rar/gzip in subdirectories and filter files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Decompress zip/rar/gzip in subdirectories and filter files

#1 Post by glob5 » 27 Feb 2013 01:07

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: need help with a bat script

#2 Post by foxidrive » 27 Feb 2013 02:05

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.

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: need help with a bat script

#3 Post by glob5 » 27 Feb 2013 10:06

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: need help with a bat script

#4 Post by foxidrive » 27 Feb 2013 13:33

1) You didn't provide the details for gzip...
2) when you say the filename contains .txt does that mean .txt is the extension?

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: need help with a bat script

#5 Post by glob5 » 27 Feb 2013 22:33

1)name format of each gz file is filename_Sequence#.gz.
2)some uncompressed file names have extension of .txt.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: need help with a bat script

#6 Post by foxidrive » 28 Feb 2013 00:38

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.

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

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

#7 Post by glob5 » 28 Feb 2013 09:09

"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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#8 Post by foxidrive » 28 Feb 2013 16:48

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
)

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

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

#9 Post by glob5 » 28 Feb 2013 22:50

Fantastic a!.

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

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

#10 Post by glob5 » 01 Mar 2013 19:11

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"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#11 Post by foxidrive » 01 Mar 2013 22:44

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.

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

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

#12 Post by glob5 » 02 Mar 2013 23:00

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.

Post Reply