Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Batch script to extract .7z files

1,170 views
Skip to first unread message

Dwight Army of Champions

unread,
Dec 29, 2011, 6:43:40 AM12/29/11
to
Suppose I have a folder that contains several .7z files. I want to
write a batch script that will do the following:

for each file abc.7z in the current directory:
create a new folder abc in current directory if it does not exist
extract all files in abc.7z and store in folder abc
for each file to be extracted
if any of the extracted files begin with string abc, then
remove abc from filename
end if
end for
end for

How do I do this?

foxidrive

unread,
Dec 29, 2011, 7:20:16 AM12/29/11
to
On 29/12/2011 22:43, Dwight Army of Champions wrote:
> Suppose I have a folder that contains several .7z files. I want to
> write a batch script that will do the following:
>
> for each file abc.7z in the current directory:

Use a for in do loop for this part:

@echo off
for /f "delims=" %%a in ('dir "*.7z" /b') do (
echo "%%a"
rem the rest of your code goes here.
)


> create a new folder abc in current directory if it does not exist

This will create the folder regardless and send any harmless error message to nul so it isn't displayed.

md "%%a" 2>nul


You haven't specified what you need to do if the folder already exists.


> extract all files in abc.7z and store in folder abc

Use the 7z command line tool to do this.


> for each file to be extracted
> if any of the extracted files begin with string abc, then
> remove abc from filename

This is an unusual requirement.

Will it be ok to process the entire tree?
What happens if the 7z files have embedded subdirectories? Would you want to remove abc from the start of every file in every folder?
What should happen if there is filename collision with an existing filename? (EG abcten.txt and ten.txt)


> end if
> end for
> end for
>
> How do I do this?

There's a start. See what you can construct and feel free to post further questions.


--
Regards,
Mic

Dwight Army of Champions

unread,
Jan 2, 2012, 11:53:48 AM1/2/12
to
On Dec 29 2011, 7:20 am, foxidrive <foxidr...@gotcha.woohoo.invalid>
wrote:
Unfortunately this doesn't work.

md "%%a" 2>nul

tries to create a directory with the same name as the .7z file (which
produces an error since a file with that name already exists). I want
to create a directory whose name is just the root of the filename
(everything up to but not including the extension)

Todd Vargo

unread,
Jan 2, 2012, 3:52:20 PM1/2/12
to
On 1/2/2012 11:53 AM, Dwight Army of Champions wrote:
>
> Unfortunately this doesn't work.
>
> md "%%a" 2>nul
>
> tries to create a directory with the same name as the .7z file (which
> produces an error since a file with that name already exists). I want
> to create a directory whose name is just the root of the filename
> (everything up to but not including the extension)

I think that should have been

md "%%~na" 2>nul

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

foxidrive

unread,
Jan 2, 2012, 11:02:49 PM1/2/12
to
md "%%~nxa" 2>nul

--
Regards,
Mic

foxidrive

unread,
Jan 2, 2012, 11:03:06 PM1/2/12
to
On 3/01/2012 07:52, Todd Vargo wrote:
> On 1/2/2012 11:53 AM, Dwight Army of Champions wrote:
>>
>> Unfortunately this doesn't work.
>>
>> md "%%a" 2>nul
>>
>> tries to create a directory with the same name as the .7z file (which
>> produces an error since a file with that name already exists). I want
>> to create a directory whose name is just the root of the filename
>> (everything up to but not including the extension)
>
> I think that should have been
>
> md "%%~na" 2>nul
>

Thanks Todd.

--
Regards,
Mic
Message has been deleted

Dwight Army of Champions

unread,
Jan 5, 2012, 7:40:28 AM1/5/12
to
On Jan 3, 12:44 pm, Nomen Nescio <nob...@dizum.com> wrote:
> Dwight Army of Champions wrote:
>
> > for each file abc.7z in the current directory:
> >      create a new folder abc in current directory if it does not
> >      exist extract all files in abc.7z and store in folder abc
>
> for %%a in (*.7z) do 7z x "%%a" -o"%%~na"

Thanks, that worked well.

Now on to the next part: I want to cd into the new directory and
remove from each filename the name of the directory. For example, in
directory abc, I have a lot of
extracted file like abc (EN),txt, abc (FR).txt, abc (DE).txt, etc. I
just want to rename each file to (EN).txt, (FR).txt, (DE).txt, etc.
Remove the directory name from the extracted filename. How do I do
that? I've got the directory name as %%~na, so how do I remove that
from each filename?

billious

unread,
Jan 5, 2012, 8:18:19 AM1/5/12
to
Theoretically,

for %%a in (*.7z) do (
7z x "%%a" -o"%%~na"
pushd "%%~na"
for /f "tokens=1*delims= " %%i in ( ' dir /b /a-d *.* ' ) do (
if /i "%%i"=="%%~na" ECHO ren "%%i %%j" "%%j"
)
popd
)

where the ECHO will siply show the rename on the screen. If you deem it
correct, remove the ECHO keyword to execute the rename.

- Assuming that the files ou want to rename have names following the pattern
"abc something" as indicated.
- No attempt is made here to handle the situation where the new filename
already exists.


Dwight Army of Champions

unread,
Jan 7, 2012, 5:39:53 PM1/7/12
to
> already exists.- Hide quoted text -
>
> - Show quoted text -

Unfortunately, that only worked for folders that don't have spaces in
their names. Forexample, for a file "Of Mice and Men (EN).7z" in a
folder "Of Mice and Men", the .7z file only gets renamed to "Mice and
Men (EN).7z"

How about this:

Foreach file in the current directory
if the entire text of the parent directory is in the current file
remove the text of the parent directory from the current file
else
do nothing
end if
end for

Is this possible?

billious

unread,
Jan 7, 2012, 7:13:33 PM1/7/12
to
You need to be very precise in your specification.

...
for /f "tokens=1*delims=(" %%i in ( ' dir /b /a-d *.* ' ) do (
if /i "%%i"=="%%~a" ECHO REN "%%i(%%j" "(%%j"
...

may work for you. Without knowing what you expect to happen - like

rename "Of Mice and Men (EN).7z" in a folder "Of Mice and Men" to "(EN).7z"

for instance; saying what it does and complaining that it's wrong doesn't
tell us what is right. Your original question did not make it clear whether
the crucial factor was the space preceding the parenthesis or the
parenthesis itself.

You revised specification does not tell us whether the file

"Supplemental Of Mice and Men (EN).7z" should be renamed for instance, and
if so to what. Implicitly, it would need to be changed to "Supplemental
(EN).7z"

Is the critical issue a match precisely on [begins-directoryname]something
to be renamed to something, or is the string-in-parenthesis-pair (critical,
unique, fixed-length, irrelevant?)

The above should work if the filename is precisely

directoryname(something

to be renamed to

(something

How would a filename

Of (Mice and) Men (EN).7z

in directoryname

Of (Mice and) Men

be handled for instance?


Todd Vargo

unread,
Jan 8, 2012, 12:30:38 AM1/8/12
to
On 1/7/2012 5:39 PM, Dwight Army of Champions wrote:
<snip...>
> Unfortunately, that only worked for folders that don't have spaces in
> their names. Forexample, for a file "Of Mice and Men (EN).7z" in a
> folder "Of Mice and Men", the .7z file only gets renamed to "Mice and
> Men (EN).7z"
>
> How about this:
>
> Foreach file in the current directory
> if the entire text of the parent directory is in the current file
> remove the text of the parent directory from the current file
> else
> do nothing
> end if
> end for
>
> Is this possible?

Try this and see if the output is what you want. If the output looks
correct, remove the ECHO to enable the batch.

@echo off
call :process "Of Mice and Men" "Of Mice and Men (EN).txt"
goto :eof

for %%a in (*.7z) do (
7z x "%%a" -o"%%~na"
pushd "%%~na"
for /f "delims=" %%i in ( ' dir /b /a-d "%%~na*.*" ' ) do (
call :process "%%~na" "%%~i"
)
popd
)
goto :eof
:process
setlocal enabledelayedexpansion
set "file=%~2"
set "file=!file:%~1=!"
if "%file:~0,1%" equ " " set "file=%file:~1%"
ECHO ren %2 "%file%"
0 new messages