LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-09-2011, 04:49 AM   #1
niiiro
Member
 
Registered: Feb 2010
Posts: 63

Rep: Reputation: 1
Batch - Rar each file separately


Hey Guys,

I'd like to go over few directories, including sub-dirs and take each file and rar it separately.

so far I succeeded taking each dir/sub-dir and pack its all content into one big .rar file using the following code:

Code:
for /F %%f in ('dir /aD /B') do (rar a -r -m0 -ee -ep %%f.rar %%f)
A peek at the directories tree:

Code:
├───CP1 raw data
│   ├───K33789-10-CP1
│   ├───K33895-10-CP1
│   ├───K33897-10-CP1
│   ├───K33903-10-CP1
│   ├───K33933-10-CP1
│   └───KL0985-10-CP1
├───CP1 summary data
├───CP2
│   ├───K33786-10-CP2
│   ├───K33787-10-CP2
│   ├───K33789-10-CP2
│   ├───K33897-10-CP2
│   ├───K33905-10-CP2
│   └───K33933-10-CP2
├───Exe scripts
├───Finish Line
└───WAT

Thanks in advanced,

Nir.
 
Old 01-09-2011, 05:15 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Aha, after a couple of minutes trying to work out what shell you were using I realised you said "batch script" *HEADSLAP*

I think the problem is with your for loop. As far as I understand it, you're currently looping over all the folders in the current directory (with the 'dir /aD /B' command) and rar-ing them recursively. You're also trying to append to an archive... So my best guess is:
Code:
for /F %%f in ('dir /aD /B') do (
    for /F %%g in ('dir %%f /aD /B') do (
        rar a -r -m0 -ee -ep %%f.rar %%f/%%g
    )
)
I may have screwed up the syntax, but I think the procedure is right. What I was aiming for was:
Code:
For each folder in the current directory:
    For each subfolder in the folder:
        Add the subfolder and its contents to the archive called folder.rar (which will be in the current directory)
Hope this helps,
 
Old 01-09-2011, 09:31 AM   #3
niiiro
Member
 
Registered: Feb 2010
Posts: 63

Original Poster
Rep: Reputation: 1
Code:
Add the subfolder and its contents to the archive called folder.rar (which will be in the current directory)

Hey Shark and thanks for your reply.

But unfortunately it's not what i mean. what I need is that the script will go through directories and sub-directories and will rar each file inside these directories separately. the packed file name must be the same name as the original non-packed file name.
 
Old 01-09-2011, 09:55 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Right... So if you had:
Code:
folder1/subfolder1/file_a
                   file_b
        subfolder2/file_c
folder2/subfolder1/file_d
You would expect to have:
Code:
folder1/subfolder1/file_a
                   file_a.rar
                   file_b
                   file_b.rar
        subfolder2/file_c
                   file_c.rar
folder2/subfolder1/file_d
                   file_d.rar
after running your code?
Um... Why? You're not compressing the files (you told it not to) and you're not bundling them into an archive. However, I think what you're looking for is:
Code:
FOR /R %%f IN (.) DO (
    FOR /F %%g in ('dir %%f /A:-D /B') do (
        rar a -r -m0 -ee -ep %%f\%%g.rar %%f\%%g
    )
)
Again, same qualifiers on actual code correctness apply... What is was meant to do is:
Code:
For each folder recursively from the current directory:
    For each file in the folder which isn't a directory:
        Add the file to a rar archive in the directory the file is in
Is that any closer to what you wanted?

Hope this helps,
 
Old 01-10-2011, 03:54 AM   #5
niiiro
Member
 
Registered: Feb 2010
Posts: 63

Original Poster
Rep: Reputation: 1
We're almost there dude.. I still get to experience a few problems. this is how my code looks like now:

Code:
REM     *** Directory Variable ***

SET ROOT="C:\FTP_Data\Template\"
SET CP1SUM="C:\FTP_Data\Template\CP1 summary data"
SET CP1RAW="C:\FTP_Data\Template\CP1 raw data"
SET CP2="C:\FTP_Data\Template\CP2"
SET FL="C:\FTP_Data\Template\Finish Line"
SET WAT="C:\FTP_Data\Template\WAT"


REM 	*** Packing .txt to .rar ***

cd %CP1RAW%
for /R %%f in (.) do (
    for /F %%g in ('dir %%f /A:-D /B') do (
        rar a -r -m0 -ee -ep %%f\%%g.rar %%f\%%g
    )
)
why when I'm adding cd %CP1RAW% I get lots of errors:

Code:
C:\FTP_Data\Template\CP1 raw data>for /R %f in (.) do (for /F %g in ('dir %f /A:
-D /B') do (rar a -r -m0 -ee -ep %f\%g.rar %f\%g ) )

C:\FTP_Data\Template\CP1 raw data>(for /F %g in ('dir C:\FTP_Data\Template\CP1 r
aw data\. /A:-D /B') do (rar a -r -m0 -ee -ep C:\FTP_Data\Template\CP1 raw data\
.\%g.rar C:\FTP_Data\Template\CP1 raw data\.\%g ) )
The system cannot find the file specified.

C:\FTP_Data\Template\CP1 raw data>(for /F %g in ('dir C:\FTP_Data\Template\CP1 r
aw data\K33789-10-CP1\. /A:-D /B') do (rar a -r -m0 -ee -ep C:\FTP_Data\Template
\CP1 raw data\K33789-10-CP1\.\%g.rar C:\FTP_Data\Template\CP1 raw data\K33789-10
-CP1\.\%g ) )
The system cannot find the path specified.

C:\FTP_Data\Template\CP1 raw data>(for /F %g in ('dir C:\FTP_Data\Template\CP1 r
aw data\K33895-10-CP1\. /A:-D /B') do (rar a -r -m0 -ee -ep C:\FTP_Data\Template
\CP1 raw data\K33895-10-CP1\.\%g.rar C:\FTP_Data\Template\CP1 raw data\K33895-10
-CP1\.\%g ) )
The system cannot find the path specified.

C:\FTP_Data\Template\CP1 raw data>(for /F %g in ('dir C:\FTP_Data\Template\CP1 r
aw data\K33897-10-CP1\. /A:-D /B') do (rar a -r -m0 -ee -ep C:\FTP_Data\Template
\CP1 raw data\K33897-10-CP1\.\%g.rar C:\FTP_Data\Template\CP1 raw data\K33897-10
-CP1\.\%g ) )
The system cannot find the path specified.

C:\FTP_Data\Template\CP1 raw data>(for /F %g in ('dir C:\FTP_Data\Template\CP1 r
aw data\K33903-10-CP1\. /A:-D /B') do (rar a -r -m0 -ee -ep C:\FTP_Data\Template
\CP1 raw data\K33903-10-CP1\.\%g.rar C:\FTP_Data\Template\CP1 raw data\K33903-10
-CP1\.\%g ) )
The system cannot find the path specified.
Why is that? Which path it cannot find?

Last thing, is it possible to pack *.txt files only?


Many thanks dude.


Nir.
 
Old 01-10-2011, 10:03 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
About the .txts, yes, definitely:
Code:
for /R %%f in (.) do (
    for /F %%g in ('dir %%f/*.txt /A:-D /B') do (
        rar a -r -m0 -ee -ep %%f\%%g.rar %%f\%%g
    )
)
The docs for 'dir' says it accepts the wildcard, so we're hoping it should work

As for the error, what you could try doing is this:
Code:
for /R %%f in (.) do (
    for /F %%g in ('dir %%f /A:-D /B') do (
        echo Folder: %%f
        echo File  : %%g
        echo Rar   : %%f\%%g.rar
        rar a -r -m0 -ee -ep %%f\%%g.rar %%f\%%g
        echo "*****"
    )
)
My suspicion is that the backslashes need to be escaped, but I don't know... This should at least indicate what it's trying to RAR. If this doesn't solve it, I'll boot to windows myself and play about 'till I can actually get it work :P
 
1 members found this post helpful.
Old 01-10-2011, 04:45 PM   #7
niiiro
Member
 
Registered: Feb 2010
Posts: 63

Original Poster
Rep: Reputation: 1
Finally!!!

Code:
REM 	*** Packing .txt to .rar ***
@echo off

cd %CP1RAW%
for /R %%f in (.) do (
    for /F %%g in ('dir %%f\*.txt /A:-D /B') do (
        rar a -r -m0 -ee -ep %%f\%%g.rar %%f\%%g
    )
)
it was the spaces in the 'CP1 summary data' and 'CP1 Raw data' directories which distrusted the 'CD' command.


Snark, you're the man! thanks a million for the effort you put in this.


Nir.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to process each paragraph in a file separately nobtiba Programming 50 01-28-2011 07:52 AM
question on how to rar multiple files into there own rar file steve51184 Linux - Software 13 10-24-2007 04:23 PM
How to open unrar? file.part1.rar - file.part2.rar Maje Linux - Software 4 05-29-2006 01:05 AM
Rar file!!! shakespy Slackware 5 08-21-2004 12:00 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM


All times are GMT -5. The time now is 08:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration