问题
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
回答1:
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, theFOR /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 thepasswords.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 isx
, or Update, which isu
);-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
.
来源:https://stackoverflow.com/questions/12796665/packing-winrar-with-a-password-on-a-group-of-files