I have been trying to write a .bat program which will find all the files with a given extension, for example *.doc, and will put them into a rar archive.

I want all the files to be copied even if they have the same name (I have files with the same name in different directories) I don't care if the files will be renamed as long as I keep all of them. For example, if I have a new.doc in Desktop and another new.doc in D:, I don't care if one of the two or even both are renamed new1.doc or new2.doc or any other way. Alternatively, the program can save them using the path it found them in. The use of rar over 7-zip or any other compression program is preferred but if really necessary, using another program is OK. Last but not least, I want the program to search the entire computer (all the hard disks). For example, I have C:, D: and F: and I want to search all 3 and to create the archive D:\test.rar. I have tried several solutions and have gotten as far as

for %%i in (c: d: f:) do for /f "delims=" %%j in ('dir /s /a-d /b %%i\*.doc') do "C:\Program Files\WinRAR\Rar.exe" a d:\test.rar "%%~j"

Now my problem is that in some PCs it works OK but in some it finds fewer files than actually exist. I find the total number of files searching for *.doc and compare the number found to the number of .doc files in the archive. I have no idea why some files are not found. Any insights or comments will be greatly appreciated.

  • Something i forgot i want to use this and for jpg so the size of the rar might need to be huge like 8 -10 gb – user2237380 Apr 14 '13 at 13:48
  • Do the function and non-function one have different operating system? Are they are different patch levels? – cybernard Apr 14 '13 at 17:26
  • Hmm no idea for some of them i ll check it and post.My and my gf have win7 64-bit – user2237380 Apr 14 '13 at 20:15

In this case you don't even need to use for, since RAR itself supports recursion (not to mention using for is horribly inefficient since your command is opening and closing the archive each time in order to update it and add the next file). Use the following command instead (you can substitute WinRAR.exe with RAR.exe if you prefer):

WinRAR a -ep2 -r D:\Test.rar C:\*.doc D:\*.doc F:\*.doc

Replace -ep2 with -ep3 if you want to store drive letters as well in the archive. Also, if you somehow still find less matching files in the archive than exist on your system, add the -e+rash switch and then compare. This should ensure no files are missed, no matter what their attributes.

As for the final size of the archive, just save to an NTFS (or exFAT) formatted drive and you should be fine. If a single huge file is of concern to you, take a look at the -v switch to create multiple volumes i.e. a split archive.

  • I tried your program mate works pretty ok i have 2 problems only.First when i try with doc i found 169 in my pc your program returns 173 for some reason and when i tried it with pdf i had 2919 it returned 2922.The -e+rash gives me the same result for doc i guess its the same for pdf i ll test it.Secondly as soon as i run it i get a pop up window(from winrar) saying Diagnostic messages: Cannot read contents of C:\Windows\CSC\v2.0.6*.doc Access is denied.(and it continues) For a total errors of 572.Can i skip this somehow?Btw i really liked the -ep3 option it rocks :) – user2237380 Apr 14 '13 at 20:41
  • For the -e+rash switch i wrote it like this "C:\Program Files\WinRAR\WinRar.exe" a -ep3 -r -e+rash D:\Test.rar C:*.doc D:*.doc F:*.doc Is it correct? Also for wma files i found 47 with the program i found 77 with and without the e+rash – user2237380 Apr 14 '13 at 21:07
  • Well obviously if it's returning more results than you expect then there are files in locations you don't even know about! Also, if you don't have permissions to access a file then how do you expect the program to do so? Try running it from an elevated/admin command prompt instead. If you have access to all the files then rar shouldn't display any errors, but if you really want to suppress errors you can use the -inul switch. Yes, you used the -e+rash option correctly, but make sure you specify C:\*.doc and not just C:*.doc. – Karan Apr 14 '13 at 21:30
  • The -inul is really nice it works perfectly thank you.One last question.I am using a vbs to hide the program cause when i chose specific extension like pdf the program takes more than an hour cause i have huge amount of pdf's(2919 with a total size of 13Gb) VBS scrpit : Set WshShell = CreateObject("WScript.Shell") WshShell.Run chr(34) & "C:\Users\A\Desktop\vi\nove.bat" & Chr(34), 0 Set WshShell = Nothing Is there a way to make it run hidden and as admin in the same time?Also the rar that will created can it become hidden at once or it has to finish before i make it hidden? – user2237380 Apr 15 '13 at 8:58
  • First of all you are right about the accepted i totally forgot it.Thank you for the clarification for the hidden.And sorry for the double posting i won't repeat it again i just wasn't sure if it was ok to ask it here since the main question was different.With that being said you are right 100% and i apologize and i wont repeat it again :) – user2237380 Apr 15 '13 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.