Help - Search - Members - Calendar
Full Version: Need Simple Batch File for RAR
The CD Forum > After Hours > Rest of the World
DigiWiz



OK, maybe it can't be done, but reading everything about command line switches, help file, Google have all lead to a dead end. Constant errors. What I'd like to do seems simple enough, but I just cannot for the life of me figure it out - I must be doing something stupid. Here's the situation:

I have a folder, and in that folder are HUNDREDS of rar files. I need to add ONE file to each of the hundreds of achives. The one file I want to add is a plain text file, though that shouldn't matter. For the sake of example, let's call that file test.txt

So quite simply, what code could I place into a batch file, that would simply add one file into hundreds of pre-existed rar's?

Typically, the errors I receive are simlilar to:

Cannot create TestRarFile1.rar
The filename, directory name, or volume label syntax is incorrect.


It seems like I've tried every iteration, but now have to turn to the experts! HELP!

Yes, I do have the full version of WinRAR, and XPsp3


hilander999
Are you using sub folders where the rar files are located?
Is the file your adding in the same directory?
Is there a password on the rar's ?
DigiWiz
QUOTE (hilander999 @ Jul 28 2009, 06:55 PM) *
Are you using sub folders where the rar files are located?
Is the file your adding in the same directory?
Is there a password on the rar's ?


  1. No subfolders (though I typically use the recursive switch in almost all my batches just in case)
  2. The file I want to add is in the same folder, but I've also tried moving it to a different folder and referencing it directly, as in c:\add.file\*
  3. No passwords.


And no joy sad.gif
DigiWiz
An example of what I've tried is:

CODE
SET WINRAR="d:\Program Files\WinRAR"

%WINRAR%\WinRAR.exe a -u *.rar null.txt

pause


while also defining the location of null.txt in another folder by using:

CODE
SET WINRAR="d:\Program Files\WinRAR"

%WINRAR%\WinRAR.exe a -u *.rar E:\add.file\null.txt

pause


with the same error messages. I'm purplexed as I'm not trying to CREATE a rar file as the errors say, just trying to add to them, or "update" with the -u switch.
hilander999
You have to define each rar separately or it will fail.

You can parse the folder with a FOR /F loop and solve that.

Give me a few minutes and I'll throw something together for you, but I must eat first.
hilander999
CODE
@echo on
SETLOCAL ENABLEEXTENSIONS

FOR /F "tokens=* delims=" %%A IN ('DIR /B /N *.rar') do (call :add_file "%%A")
PAUSE & EXIT

:add_file
call "C:\Program Files (x86)\WinRAR\WinRAR.exe" a %1 added.txt
GOTO :EOF

This assumes the rar's, this script & the file being added are in the same directory.
DigiWiz
Holy crap - I just figured it out (trial and error) by borrowing some elements from others scripts I've saved... here's what I have that it working... I didn't let it go through all the files, so maybe it'll try and repreat itself, but it did work on the first ten files I let it do before shutting it down.

I'll also try yours and see how it goes. Thanks!

Here's what I just used that works:

CODE
SET WINRAR="d:\Program Files\WinRAR"

for /R %1 %%a in (*.rar) do %WINRAR%\WinRAR.exe a -u %%a null.txt  "%%a"


I'll let it do all of them, and see if it tries to do them ad infinitum, or maybe it'll stop when it finishes doing the files - gonna take a while as I only have a 500mHz PIII sad.gif
DigiWiz
I just did a test on only 37 files in a "test" folder, and the script I posted above DOES work. It adds and/or updates the null.txt file, and ends when it reaches the end of the rar's in that folder!

Yee Freakin' Haw!!!!!

Now I'll try yours to ses if it works too... always good to have examples of good little scripts to play with, when you "kinda" know what you're doing, but not really wink.gif
hilander999
I would not have posted it if it did not pass the test.

Yours might choke on strange symbols and spaces in the filename and/or path due to how winrar processes the files.
DigiWiz
Happy to report that yours works also, with a couple of tiny tweeks - my path to WinRAR is on D:, and I changed the filename to match what I had been using, and added the update switch (-u) - otherwise works great also.

CODE
@echo on
SETLOCAL ENABLEEXTENSIONS

FOR /F "tokens=* delims=" %%A IN ('DIR /B /N *.rar') do (call :add_file "%%A")
PAUSE & EXIT

:add_file
call "D:\Program Files\WinRAR\WinRAR.exe" a -u %1 null.txt
GOTO :EOF



Thanks for you help and work!!!



DigiWiz
QUOTE (hilander999 @ Jul 28 2009, 08:31 PM) *
Yours might choke on strange symbols and spaces in the filename and/or path due to how winrar processes the files.


When I download/have mass quantities of rar's, I always use a "bulk renamer" to get rid of all spaces and other characters - I only allow letters, numbers periods and dashes in my rar filenames. So far, WinRAR has been able to handle those criteria without problem. I use Rapidshare a lot, and it doesn't allow spaces or other characters, so I make sure and remove mine, before letting Rapidshare subsitute an underscore for characters RS doesn't like.

Thanks again. Now I'm off to add files to hundreds of rar's - that ought to take the rest of the night sad.gif
PatrickMc
Nice scripts so far. As a possible alternative, here is an alternate script that

1. makes the code more explicit, thus less cryptic
2. handles spaces, other special characters, etc.
3. can run interactively or from task scheduler with the same syntax.


CODE
# Script rar.txt
# Change current directory to where .rar files and null.txt are located.
cd "C:\rarfolder"
# Collect list of .rar files. Using the -r option to handle even subfolders.
var str list; lf -rn "*.rar" > $list
# One .rar file at a time.
while ($list <> "")
do
    # Get the next .rar file.
    var str rarfile; lex "1" $list > $rarfile
    # Add null.txt. Full path for the null.txt file can be specified if it is not in the same directory.
    system "D:\Program Files\WinRAR\WinRAR.exe a -u " ("\""+$rarfile+"\"") " " ("\""+"null.txt"+"\"")
done


Note that we are wrapping $rarfile and null.txt in double quotes to allow for spaces, etc. Script is in biterscripting ( http://www.biterscripting.com ) . To try, save the script as C:\Scripts\rar.txt, start biterscripting, enter the following command

CODE
script rar.txt


Don't forget to change "C:\rarfolder" to appropriate path before executing the script. Also test it first - I have not been able to test it.

Patrick

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.