NEED HELP writing a batch file for the following task:
|
|
aweathe
Junior Member
|
9. May 2007 @ 08:25 |
Link to this message
|
Hello,
Does anyone know if it is possible to use a batch file for the following action?:
Decompress a large number of WinRAR files stored in a list of folders and subfolders.
Thanks so much for any help offered! Also, I?m a windows XP user.
Andrew
|
Advertisement
|
  |
|
Indochine
Senior Member
|
9. May 2007 @ 08:47 |
Link to this message
|
How are the WinRAR files stored? Ae they all over the place, or are they in subfolders of one folder on on drive, or a mixture? Is it possible to get a list that might look like this -
c:\archive\scripts\script1.rar
c:\archive\scripts\script2.rar
c:\archive\scripts\script3.rar
c:\archive\docs\doc1.rar
c:\archive\docs\doc2.rar
c:\archive\docs\doc3.rar?
I don't mean post it on here, I mean is it theoretically possible? Do you know how to do that?
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
|
aweathe
Junior Member
|
9. May 2007 @ 09:39 |
Link to this message
|
How the WinRAR files are stored:
There is a folder containing about 30 subfolders. In each of these 30 subfolders there is between 5-10 different folders, and in these folders is where the WinRAR files are stored. There are about 1-3 WinRAR files in each of these folders.
As for the list format you suggested. Yes it is possible to get it in that format (I'm just learning batch files and don't think it would be too hard)... However I need the folders in the original format at the end. If the WinRAR files were in the format you suggested then I would just select them all the decompress...
|
Indochine
Senior Member
|
9. May 2007 @ 09:53 |
Link to this message
|
Quote: However I need the folders in the original format at the end. If the WinRAR files were in the format you suggested then I would just select them all the decompress...
So you want to extract the contents of the RAR archives somewhere else, leaving the archive folders as they are?
How about another folder tree whose structure follows the original one, except each rar archive is replaced by a subfolder named after it?
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
This message has been edited since posting. Last time this message was edited on 9. May 2007 @ 09:57
|
aweathe
Junior Member
|
9. May 2007 @ 09:56 |
Link to this message
|
Decompress in the folder they're in now. (in-place)
|
aweathe
Junior Member
|
9. May 2007 @ 10:02 |
Link to this message
|
Well it would be most useful to have them decompressed in place... is this possible?
|
Indochine
Senior Member
|
9. May 2007 @ 10:28 |
Link to this message
|
In place, somewhere else, it's easy either way (famous last words!)... Do you want to keep the archives or delete them once their contents are extracted?
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
|
aweathe
Junior Member
|
9. May 2007 @ 10:35 |
Link to this message
|
I would like to keep the archives where they are.
|
Indochine
Senior Member
|
9. May 2007 @ 10:42 |
Link to this message
|

So the layout is like this?
Spec check...
Are you hoping for a batch file which could be run from Top Folder, which would result in all the RAR contents being extracted to the same sub-sub-folder where they are located? Without deleting the RARs?
Expect trial version tomorrow...
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
|
aweathe
Junior Member
|
9. May 2007 @ 10:45 |
Link to this message
|
Yes that's the folder structure exactly! And "yes" is the answer to your last two questions. ie: I would like to keep the archives in the same place and have them extracted in the same place.
Thanks! I'll just wait for your post!
|
aweathe
Junior Member
|
9. May 2007 @ 10:58 |
Link to this message
|
and also the file can be run from the Top folder in the diagram you posted.
|
Indochine
Senior Member
|
9. May 2007 @ 12:13 |
Link to this message
|
I am presuming that you have Rar.exe, the command-line program that comes along with WinRAR, and that its location is
C:\Program Files\WinRAR\Rar.exe
The version number would be helpful
C:\Program Files\WinRAR>rar /?
RAR 3.70 beta 1 Copyright (c) 1993-2007 Alexander Roshal 8 Jan 2007
Registered to Mike
(etc)
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
This message has been edited since posting. Last time this message was edited on 9. May 2007 @ 12:16
|
aweathe
Junior Member
|
9. May 2007 @ 13:43 |
Link to this message
|
Yes the directory where WinRAR is located is correct. my version is 3.62, but could download a more recent one if necessary. I have only the evaluation copy as well.
|
Indochine
Senior Member
|
10. May 2007 @ 02:56 |
Link to this message
|
copy this to Notepad & save as a batch file in the "Top folder" we discussed earlier.
@echo off
REM place this batch file in the top folder
REM and run it from there
if exist "%temp%\rardirlist.txt" del "%temp%\rardirlist.txt" > nul
if exist "%temp%\subdirlist.txt" del "%temp%\subdirlist.txt" > nul
REM remember where we are
set topfolder=%CD%
REM get a list of all the subfolders
REM under this one and put it in
REM a temp file
dir /b /s /ad > "%temp%\subdirlist.txt"
REM in a loop check each one for presence of
REM rar files, write folder name to another list
REM if any found
for /F "delims==" %%a in (%temp%\subdirlist.txt) do (
cd %%a
if exist *.rar echo %%a >> "%temp%\rardirlist.txt"
)
REM for each folder that contains RARs
REM cd to that folder and extract
for /F "delims==" %%a in (%temp%\rardirlist.txt) do (
cd %%a
"c:\program files\winrar\rar.exe" e *.rar
)
REM clean up
del "%temp%\subdirlist.txt" > nul
del "%temp%\rardirlist.txt" > nul
REM go back to where we were
cd %topfolder%
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
This message has been edited since posting. Last time this message was edited on 10. May 2007 @ 03:22
|
aweathe
Junior Member
|
10. May 2007 @ 06:48 |
Link to this message
|
unfortunately it didn't work on the first trial... hmmm I'm reading it and trying to figure it out so that I can fix it, but I'm just not there yet in the understanding!
Maybe this with clarify; I have copied the output that I recieved in the command promt after running the file from there. (I named your file "winRAR.bat"). OK here it is:
C:\Documents and Settings\ANDREW\Desktop\Assign 1>winRAR.bat
The system cannot find the file C:\DOCUME~1\ANDREW\LOCALS~1\Temp\rardirlist.txt.
Could Not Find C:\DOCUME~1\ANDREW\LOCALS~1\Temp\rardirlist.txt
C:\Documents and Settings\ANDREW\Desktop\Assign 1>
|
Indochine
Senior Member
|
10. May 2007 @ 06:57 |
Link to this message
|
try this... creates and destroys temp files in the TOP folder
@echo off
REM place this batch file in the top folder
REM and run it from there
if exist "rardirlist.txt" del "rardirlist.txt" > nul
if exist "subdirlist.txt" del "subdirlist.txt" > nul
REM remember where we are
set topfolder=%CD%
REM get a list of all the subfolders
REM under this one and put it in a temp file
dir /b /s /ad > "subdirlist.txt"
REM in a loop check each one for rar files
REM write folder name to another list if any found
for /F "delims==" %%a in (subdirlist.txt) do (
cd %%a
if exist *.rar echo %%a >> "rardirlist.txt"
)
REM for each folder that contains RARs
RE enter folder and extract
for /F "delims==" %%a in (rardirlist.txt) do (
cd %%a
"c:\program files\winrar\rar.exe" e *.rar
)
REM clean up
del "subdirlist.txt" > nul
del "rardirlist.txt" > nul
REM go back to where we were
cd %topfolder%
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
|
aweathe
Junior Member
|
10. May 2007 @ 07:16 |
Link to this message
|
I'm looking over this myself right now... but here's the error message I got this time in the cmd promt (this time your file is labeled "winRAR2.bat" :
C:\Documents and Settings\ANDREW\Desktop\Assign 1>winRAR2.bat
'RE' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the file rardirlist.txt.
Could Not Find C:\Documents and Settings\ANDREW\Desktop\Assign 1\9-Dec-2006\I57U
S\subdirlist.txt
Could Not Find C:\Documents and Settings\ANDREW\Desktop\Assign 1\9-Dec-2006\I57U
S\rardirlist.txt
C:\Documents and Settings\ANDREW\Desktop\Assign 1>
|
aweathe
Junior Member
|
10. May 2007 @ 07:22 |
Link to this message
|
Oh and I fixed the "RE" to "REM"
|
aweathe
Junior Member
|
10. May 2007 @ 07:27 |
Link to this message
|
I'm not sure of the difference (or if it matters) but it's written ">>" instead of ">" about halfway down the script.
|
Indochine
Senior Member
|
10. May 2007 @ 07:41 |
Link to this message
|
yes, >> appends to file if it already exists, or creates a new one if it doesn't, whereas > always creates a new one.
Getting there!
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
|
Indochine
Senior Member
|
10. May 2007 @ 07:52 |
Link to this message
|
This is a debugging version with lots of messages and progress updates...
@echo off
REM place this batch file in the top folder
REM and run it from there
REM remember where we are
set topfolder=%CD%
if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
if exist "%topfolder%\rardirlist.txt" del "%topfolder%\rardirlist.txt" > nul
echo.
echo (1) creating list of all sub folders...
echo.
REM get a list of all the subfolders
REM under this one and put it in a temp file
dir /b /s /ad > "%topfolder%\subdirlist.txt"
echo all sub folders...
echo.
type "%topfolder%\subdirlist.txt"
echo.
pause
echo.
echo (2) finding folders with rars in them
echo.
REM in a loop check each one for rar files
REM write folder name to another list if any found
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
cd %%a
if exist *.rar echo %%a >> "%topfolder%\rardirlist.txt"
)
echo all sub folders with Rars in...
echo.
type "%topfolder%\rardirlist.txt"
echo.
pause
echo.
echo (3) extracting from Rar archives
echo.
echo ready to start...
echo.
pause
echo.
REM for each folder that contains RARs
REM enter folder and extract
for /F "delims==" %%a in ('type "%topfolder%\rardirlist.txt"') do (
echo.
echo expanding rars in %%a
echo.
cd %%a
"c:\program files\winrar\rar.exe" e *.rar
)
REM clean up
rem del "%topfolder%\subdirlist.txt" > nul
rem del "%topfolder%\rardirlist.txt" > nul
REM go back to where we were
cd %topfolder%
echo.
echo (4) Finished
echo.
pause
echo.
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
This message has been edited since posting. Last time this message was edited on 10. May 2007 @ 07:52
|
aweathe
Junior Member
|
10. May 2007 @ 08:40 |
Link to this message
|
I'm working through it. Almost works I think... going to have to take a break for lunch tho! Thanks so much for the help. I'll write you again when I solve it or if I get stuck.
Just a couple (simple) questions so that I can understand the code and make it work:
1. "for /F" means for all folders in the current directory?
2. "dir /b /s /ad" : in this statement I believe "/s" means include all subfolders, however I don't understand "/b" and "/ad".
Finally, the part of the code that has the problem is copied here:
""
REM in a loop check each one for rar files
REM write folder name to another list if any found
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
cd %%a
if exist *.rar echo %%a > "%topfolder%\rardirlist.txt"
)
""
The problem is that the folder "rardirlist.txt" doesn't get created...?
talk to you again soon...
|
Indochine
Senior Member
|
10. May 2007 @ 09:20 |
Link to this message
|
Originally posted by aweathe: 1. "for /F" means for all folders in the current directory?
Putting /F after the FOR indicates to FOR that whatever is between the parentheses is the source of a series of lines of data to be read and processed. Single quotes around what is in the parentheses indicate that the source is the output of a command.
In this line,
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
FOR is told to TYPE the file and deal with it line by line.
* without the "/f", what is between the parentheses is interpreted as a series of ITEMS.
for "delims==" %%f in (apple orange banana) do (
echo %%f
)
the result would be
apple
orange
banana
* single-quotes within the parentheses indicates that the data lines are sourced from the output of a command. Without the single-quotes, FOR /F tries to read a (text-)file named (whatever is within the parentheses)
2. "dir /b /s /ad" : in this statement I believe "/s" means include all subfolders, however I don't understand "/b" and "/ad".
Dir /? will give full info.
Dir /b gives a bare output, just the file names, no date or size info or header or footer. Necessary for processing in batch files.
Dir /ad --- /(a)ttribute (d)irectory just finds directories.
Dir /ah --- /(a)ttribute (h)idden finds just hidden files
Dir /ar --- /(a)ttribute (r)ead only finds just read only files
Dir /as --- /(a)ttribute (s)ystem finds just system files.
A minus sign eg dir /a-d inverts the effect.
Quote:
Finally, the part of the code that has the problem is copied here:
""
REM in a loop check each one for rar files
REM write folder name to another list if any found
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
cd %%a
if exist *.rar echo %%a > "%topfolder%\rardirlist.txt"
)
""
The problem is that the folder "rardirlist.txt" doesn't get created...?
Does subdirlist.txt get created?
Until our next chat...
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
|
Indochine
Senior Member
|
10. May 2007 @ 10:28 |
Link to this message
|
New greatly simplified version - no temp files, it just goes ahead and does it.
[original post 14:28 hrs updated 20:20 hrs]
@echo off
REM place this batch file in the top folder and run it from there
set topfolder=%CD%
for /F "delims==" %%a in ('dir /b /s *.rar') do (
cd "%%~Da%%~pa"
"c:\program files\winrar\rar.exe" e "%%~Da%%~pa%%~nxa" | findstr "Extracting from"
)
cd %topfolder%
On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
This message has been edited since posting. Last time this message was edited on 10. May 2007 @ 11:21
|
Advertisement
|
  |
|
aweathe
Junior Member
|
10. May 2007 @ 12:13 |
Link to this message
|
RE:Quote: Does subdirlist.txt get created?
yes it does. and it looks fine.
|