how to zip files by group which is uncertain substring in filenames, using DOS batch script

StackOverflow https://stackoverflow.com/questions/17842263

سؤال

I have a bunch of DOS files in a folder that I want to zip by group. The filenames are coded like this:

devXXXXdbt01.log

qasXXXXwb01.log

devYYYap01.log

qasYYYdb02.log

the first 3 letters are either dev or qas the following 3-5 letters (XXXX or YYY) are dynamic group name that I want to use as zip file name the following 2 letters (db, wb, ap) are one of the 3: db, wb, or ap the following optional letter t means it's temporary file. in some case the filename does not have the letter t the last 2 digits are serial number

so I'd like to create zip file XXXX.zip that contains files like the first 2, zip file YYY.zip that contains files like the last 2.

How can I script a DOS batch to do it automatically, without hard coding group names?

هل كانت مفيدة؟

المحلول

@ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
SET destdir=u:
PUSHD "%sourcedir%"
FOR /f %%i IN ('dir /b /a-d dev*.* qas*.*') DO CALL :zipme %%i
popd
GOTO :EOF

:zipme
SET destzip=%~n1
SET "num="
SET /a num=1%destzip:~-2% 2>nul
IF NOT DEFINED num ECHO(reject %1&GOTO :EOF 
FOR %%g IN (db wb ap) DO (
 IF /i %%g==%destzip:~-4,2% SET destzip=%destzip:~3,-4%&GOTO zipthis
 IF /i %%gt==%destzip:~-5,3% SET destzip=%destzip:~3,-5%&GOTO zipthis
)
ECHO(reject %1
GOTO :EOF
:zipthis
ECHO wzzip -a "%destdir%\%destzip%" %1
GOTO :eof

Example source directory:

abcxxxdb01.log
devxxxdb01.log
devxxxdb02.log
devxxxdbt03.log
devxxxdbt0x.log
devxxxdbtx1.log
devyyyydbt03.log
devzzzzzap03.log
devzzzzzapt99.log
devzzzzzapt999.log
devzzzzzdb03.log
devzzzzzdbt03.log
devzzzzzjq03.log
devzzzzzwb03.log
devzzzzzxy03.log
qasxxxdb01.log
qasxxxdb02.log
qasxxxdbt03.log
qasxxxdbt0x.log
qasxxxdbtx1.log
qasyyyydbt03.log
qaszzzzzap03.log
qaszzzzzapt99.log
qaszzzzzapt999.log
qaszzzzzdb03.log
qaszzzzzdbt03.log
qaszzzzzjq03.log
qaszzzzzwb03.log
qaszzzzzxy03.log
zxzxxxdb01.log

Run results:

wzzip -a "u:\xxx" devxxxdb01.log
wzzip -a "u:\xxx" devxxxdb02.log
wzzip -a "u:\xxx" devxxxdbt03.log
reject devxxxdbt0x.log
reject devxxxdbtx1.log
wzzip -a "u:\yyyy" devyyyydbt03.log
wzzip -a "u:\zzzzz" devzzzzzap03.log
wzzip -a "u:\zzzzz" devzzzzzapt99.log
reject devzzzzzapt999.log
wzzip -a "u:\zzzzz" devzzzzzdb03.log
wzzip -a "u:\zzzzz" devzzzzzdbt03.log
reject devzzzzzjq03.log
wzzip -a "u:\zzzzz" devzzzzzwb03.log
reject devzzzzzxy03.log
wzzip -a "u:\xxx" qasxxxdb01.log
wzzip -a "u:\xxx" qasxxxdb02.log
wzzip -a "u:\xxx" qasxxxdbt03.log
reject qasxxxdbt0x.log
reject qasxxxdbtx1.log
wzzip -a "u:\yyyy" qasyyyydbt03.log
wzzip -a "u:\zzzzz" qaszzzzzap03.log
wzzip -a "u:\zzzzz" qaszzzzzapt99.log
reject qaszzzzzapt999.log
wzzip -a "u:\zzzzz" qaszzzzzdb03.log
wzzip -a "u:\zzzzz" qaszzzzzdbt03.log
reject qaszzzzzjq03.log
wzzip -a "u:\zzzzz" qaszzzzzwb03.log
reject qaszzzzzxy03.log

All you essentially need to do is set your source and destination directorynames.

The routine :zipme is called passing each filename from the source directory that matches the filemasks specified. The name part alone is assigned to destzip and num first deleted and then assigned to 100+the last 2 characters of the name. If the result is not a number, nun remains unassigned (the 2>nul suppresses the error) and the filename rejected (the display is optional, of course.)

Next, the three target character combinations are matched against the string from the fourth-last character for two characters, and again with t appended to the fifth-last for 3 characters. Any match causes a transfer to ZIPTHIS and if none match, then the reject is (optionally) reported.

From there, it's simply a matter of invoking your zip program - I've just ECHOed the WZZIP command-line.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top