4

I'll try and make this as short as i can. I'm looking for a .bat file to rar up and password folders with files in them, but the problem is a little more tricky than i thought, each folder can have anything from 1 to 400 files in it, i have two .txt files, one with the desired number of.rar file names and one with the desired number of .rar passwords, so txt doc's look like this, first is filenames.txt:

FAD01    
FAD02    
FAD03

and so on up to FAD110

and another .txt file

GAP01
GAP02
GAP03
GAP04
GAP05

and so on upto GAP50

the 1st password .txt file look like this

vF2RiQMof6HSWy8MSEIO
13qIsZ7e9RIzBwaQ3UO6
KfTMk9wPfPQKMNh4XzD8

and so on, 110 random passwords in total

2nd password .txt file like this

al9p4O2vkiqcFKfgpWh7
U0jkx05nQoTKPAAPEYiW
43bliny0TU2R4CKob62H

and so on, 50 random passwords in total

What I want to do is apply the 1st list of 50 file names in the .txt document to 50 sub folders sat within a main folder, and the 1st list of 50 passwords to be used for each of those .rar's created, so my end result would be > inside 1st main folder 50 rars each passworded differently, I also want the .rar files passwords to be encrypted so you cannot see the contents of the any .rar file unless you enter the password.

End result would look like this:

FAD01.rar > password = vF2RiQMof6HSWy8MSEIO
FAD02.rar > password = 13qIsZ7e9RIzBwaQ3UO6
FAD03.rar > password = KfTMk9wPfPQKMNh4XzD8

and so on up to FAD110.rar

I am willing to pay someone if they think they can do it, or if you just fancy the challenge and have a big heart then that's fine too :) and thank you for taking the time to read my question

Extra Info

I created file names & password lists to help with the process of generating the passworded rar files, but if you can do it without needing predefined password/file name lists then that would be less work to do, but i would need some sort of file name / password list creating by the .bat file so i know what .rar has what password.

the lists are easy to generate, so i was hoping the bat file could read the lists 1 line at a time and apply the file name & password to the folder to be rar'd up

Basically I'm looking for it to take a group of folders and rar them up individually in some form of alphabetical order and apply random passwords to the rar's (these passwords should be 20 characters long and contain numbers and upper/lower case letters), but i want the rar encrypting which is an option in winRAR which stops users seeing contents of the rar until a password is entered

50 to 300 folders each containing numerous files, folders need to be rar'd up and passworded with random encrypted passwords, i am currently doing them 1 at a time, very time consuming indeed

SeanC
  • 15,695
  • 5
  • 45
  • 66
David Axwell
  • 41
  • 1
  • 4
  • I'm definitely willing to have a go at it because I fancy the challenge :) but can you reword why you have 4 different txt files, and what you want passwords on please? – Bali C Oct 09 '12 at 13:24
  • It's still not clear why there are *four* text files, or, more exactly, two pairs (filenames + passwords) of them. I mean, it's all right if both (or however many) pairs need to be processed, that's just doesn't seem definite from your description. And then, it's not clear if the names of these list files follow any pattern for the script to be able to recognise them. – Andriy M Oct 09 '12 at 20:22
  • More confusion is added by the fact that the first text file in your example is said to contain *110* items, but later you are referencing it as one containing *50* items. Perhaps, you just confused the number with that in the other list (the second one which you have indeed said contains 50 items). Or did you really mean 50? Like, the first 50 of 110? But that, again, would beg explanation ‘as to the whyness’. – Andriy M Oct 09 '12 at 20:24
  • There are hundreds of folders with directory's in them, each folder can contain between 50-400 directory's, so i have made dozens of txt files, two for each folder, one contains file names (line by line) and the other contains passwords (line by line), i use the lists to rename the directory's and password them, all one at a time – David Axwell Oct 10 '12 at 00:43
  • The text files are not the issue, if you can just create a bat file the will rar all directory's within a folder and password each of them with randomly generated passwords (20 characters in length, at least 1 upper 1 lower case and a number in the password and encrypted so contents of rar's cannot be viewed without the password being entered), that is what i'm after :) – David Axwell Oct 10 '12 at 00:48
  • Is there a way to do this without haveing the root dir? So lets say you have ten folders 1-10, it parses all ten folders and dumps the rar(s) in the same "root" folder that the batch exists in? –  Oct 12 '12 at 14:28

1 Answers1

3

Assuming this directory structure:

\
└─SomeRootFolder
  ├─SuperfolderA
  │ ├─filenames.txt
  │ ├─passwords.txt
  │ ├─A1
  │ │ └─...
  │ ├─A2
  │ │ └─...
  │ └─...
  ├─SuperfolderB
  │ ├─filenames.txt
  │ ├─passwords.txt
  │ ├─B1
  │ │ └─...
  │ ├─B2
  │ │ └─...
  │ └─...
  └─...

(the subfolders A1, A2, ... B1, B2, ... being the ones that need be put into their own archives), the following worked for me:

@ECHO OFF
SET arch="C:\Program Files (x86)\WinRAR\WinRAR.exe"
SET "rootdir=D:\path\to\folders"
SET "namestxt=filenames.txt"
SET "passtxt=passwords.txt"

FOR /D %%D IN ("%rootdir%\*") DO (
  <"%%D\%passtxt%" (
    FOR /F "usebackq delims=" %%N IN ("%%D\%namestxt%") DO (
      SET /P "pass="
      SETLOCAL EnableDelayedExpansion
      %arch% a -r -ep1 -hp!pass! "%%D\%%N.rar" "%%D\%%N"
      ENDLOCAL
    )
  )
)

The first part merely sets up some variables, used later in the script. (As it happens, every one of them is used in the script just once, so you could easily substitute the values for the corresponding variable expansions. I only declared those variables habitually, I often use variables like that in my batch scripts.)

The outer loop, the FOR /D one, iterates over your ‘superfolders’, those containing subfolders to archive and the text files.

The loop's body reads two files in this way:

  • the filenames.txt is read by the inner loop instruction itself, the FOR /F command;

  • the password file is read by one of the commands in the inner loop's body, namely SET /P. SET /P reads the standard input stream, which is the console by default, but the standard input in this case is redirected for the entire inner loop to be from the passwords.txt instead.

So, the %%D loop variable (the outer loop's one) contains the complete path to a ‘superfolder’, %%N is assigned a subfolder's name and pass= is set to a corresponding password. It only remains at this point to call the archiver.

These the parameters used in the archiver command line:

  • a – the Add command proper (as opposed to commands like Extract, which is x, or Update, which is u);

  • -r – process the specified folder recursively (if you don't need that, remove this option);

  • -ep – exclude the base folder from names;

  • -hp… – encrypt the archive with the specified password (which is different from -p…, which merely protects the extraction; -hp won't let you see the contents of the archive without the password, just like you seem to want).

You can see that before calling the archiver, the delayed expansion is enabled. If you aren't aware of the effects of ‘immediate’ expansion in bracketed blocks of commands (which includes loop bodies), I'll just say that it doesn't work like you want it if the variable is both assigned and expanded in the same bracketed block. Hence delayed expansion. It uses !s instead of %s and you can see the pass variable expanded that way.

There's one more thing that needs to be mentioned. If you generate fewer passwords than there are names in the other file, the script will still work fine but the names lacking their ‘own’ passwords would be paired with the last password in the list. (It is also no problem for the script if names are fewer than the passwords. In this case, the ‘orphaned’ passwords just won't be used.)


UPDATE

If you would like to read subfolder names directly from the directory, thus abandoning the filenames.txt files, you could change the script like this:

@ECHO OFF
SET arch="C:\Program Files (x86)\WinRAR\WinRAR.exe"
SET "rootdir=D:\path\to\folders"
SET "namestxt=filenames.txt"
SET "passtxt=passwords.txt"

FOR /D %%D IN ("%rootdir%\*") DO (
  <"%%D\%passtxt%" (
    FOR /D %%N IN ("%%D\*") DO (
      SET /P "pass="
      SETLOCAL EnableDelayedExpansion
      %arch% a -m0 -r -ep1 -hp!pass! "%%N.rar" "%%N"
      ENDLOCAL
    )
  )
)

UPDATE 2

If you want to generate filenames.txt files on the fly reading the names from the directory, insert just one command into the first script's outer loop:

@ECHO OFF
SET arch="C:\Program Files (x86)\WinRAR\WinRAR.exe"
SET "rootdir=D:\path\to\folders"
SET "namestxt=filenames.txt"
SET "passtxt=passwords.txt"

FOR /D %%D IN ("%rootdir%\*") DO (
  DIR /AD /B "%%D" >"%%D\%namestxt%"
  <"%%D\%passtxt%" (
    FOR /F "usebackq delims=" %%N IN ("%%D\%namestxt%") DO (
      SET /P "pass="
      SETLOCAL EnableDelayedExpansion
      %arch% a -r -ep1 -hp!pass! "%%D\%%N.rar" "%%D\%%N"
      ENDLOCAL
    )
  )
)

The /AD switch of DIR makes sure only folder names are included.

Note that you could generate the passwords in a similar way. If there's a single command that accepts the output file name as a parameter and just does the job without interruptions, you could insert that command just after the DIR.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • The system cannot find the file specified. – David Axwell Oct 10 '12 at 20:03
  • the pass.bat filenames and passwords are all in the same dir as the folders i want to rar up – David Axwell Oct 10 '12 at 20:05
  • can you lay this out in more layman's terms, as i've tried copying your text to a .txt file & renamed it pass.bat , my winrar.exe is in c:/program files/winrar/winrar.exe and my working folder with directory's in it is f:/new folder/folders to be archived/ , the first folder has 5 directoy's in it to archive so i made filenames.txt with 5 filenames in it, and passwords.txt with 5 passwords in it, altered the following lines in your code SET arch="C:\Program Files\WinRAR\WinRAR.exe" and SET "rootdir=F:/new folder/folders to be archived/", ran pass.bat in a cmd window and got 5 errors back – David Axwell Oct 10 '12 at 20:08
  • @DavidAxwell: Please have a look at my update where I've described the directory structure the script assumes. Please let me know if it was an incorrect assumption. Also, did you actually use forward slashes in the path assigned to `rootdir`? The Windows command shell doesn't really like them as path delimiters and prefers backslashes instead. Forward slashes do work sometimes but not always. – Andriy M Oct 10 '12 at 21:15
  • what am i doing wrong, here is a snap shot of what i've got - http://i34.servimg.com/u/f34/16/04/19/70/captur19.jpg – David Axwell Oct 10 '12 at 22:21
  • what am i doing wrong, here is a snap shot of what i've got - http://i34.servimg.com/u/f34/16/04/19/70/captur20.jpg – David Axwell Oct 10 '12 at 22:28
  • You probably wasn't doing anything wrong, it seems to be one of my assumptions that was wrong. From one of your comments it appeared that there was more than one *set* of subfolders that need to be archived. My assumption there was that the folders containing those subfolders did, in their turn, belong to the same root folder. Applying to your test setup, if you specified the root folder as `D:\path\to` instead of `D:\path\to\folders`, the script would work for the subfolders in `folders` (it would create separate archives 114.rar, 115.rar etc.). – Andriy M Oct 11 '12 at 05:42
  • The script would then proceed to reading other folders in `D:\path\to` and looking for the two text files in them. I'm sorry about this confusion that I caused. Please let me know if my script needs to be fixed to avoid this additional level of folder nesting. – Andriy M Oct 11 '12 at 05:43
  • ok, that appears to be working, but it is slow, is there something you can alter in the script to make the rar compression method quicker by using "store" mode i.e. no compression applied to file, just a straight rar, thanks – David Axwell Oct 11 '12 at 09:51
  • also, is it possible to add in the beginning of the script, for it to create it's own filenames.txt file by scanning the folders, and creating the file names from it, this way i only need to create the passwords list for passwords.txt, or maybe do away with the filesnames all-together and just rar each folder by it's own name and use the passwords.txt file for the passwords? thanks – David Axwell Oct 11 '12 at 10:03
  • -m Set compression method: -m0 store do not compress file when adding to archive "If this switch is not specified, RAR uses -m3 method (normal compression)." – David Axwell Oct 11 '12 at 10:15
  • ok, i'm starting to get to grips with this now, after reading up on rar dos commands, so i changed this line to - %arch% a -r -ep1 -m0 -hp!pass! "%%D\%%N.rar" "%%D\%%N" and the process zooms along nicely – David Axwell Oct 11 '12 at 10:27
  • now i just need to do away with the filenames.txt and just rar each item by it's own name – David Axwell Oct 11 '12 at 10:28
  • Yes, it is possible to do away with filenames.txt. Are you certain you want to do that, though? How would you later know for sure which password was used for which archive? Anyway, the change is rather trivial for this, please see my update. – Andriy M Oct 11 '12 at 10:46
  • @ECHO OFF CD d:\path\to\folders DIR /B/P > filenames.txt > this makes my filenames.txt list, can that be incorporated into your 1st designed batch file ? thanks – David Axwell Oct 11 '12 at 10:59
  • ok, that works great, but if i move the bat file to another drive, say F:\New Folder\folders and change SET "rootdir=D:\path\to" to SET "rootdir=F:\New Folder\folders" it doesn't work, in F:\New Folder\folders i have place the bat file with the rootdir changes and a password.txt list, but it doesn't do anything, i don't want to have to move every folder over to my D: drive every time i want to run the script – David Axwell Oct 11 '12 at 11:58
  • You don't need to move the batch file. (Well, not if you didn't add any `CD` commands of your own. When testing the script, I was running it from an entirely different folder and it worked fine.) But I understand the issue has to do with that assumed additional level of subfolders' nesting. I'll update my answer one more time in about an hour. Meanwhile, you could try replacing `SET "rootdir=F:\New Folder\folders"` with `SET "rootdir=%~1"` and running the script like this: `yourscriptname.bat "F:\New Folder"` (note the absence of the final `\folders`). `passwords.txt` needs to be in `folders`. – Andriy M Oct 11 '12 at 12:10
  • it's ok, i've figured it out, by just creating a dir called "folders" inside the dir(s) i want to archive, and then simply cut & past all files into "folders" dir, this is a fantastic time saving script you have written, and i want to thank you most sincerely my friend – David Axwell Oct 11 '12 at 12:26
  • Glad you figured it out, and happy to be of help! – Andriy M Oct 11 '12 at 13:03