script to search and archive using winrar

596 views Asked by At

I am new to scripting, can some please assist me, I have batch file that

1- Looks at the first 8 characters in the file name, creates and moves those files to new folder with first 8 characters as folder name.

2- Then looks at folder created in step 1 for next four series of character (9,10,11,12)and create and move to another subfolder with next 4 characters as folder name.

3- Then looks at folder created in step 2, for extension of every file and create and move to a new folder with extension as folder name.

For example, I have files that look like this

ABCEFGHI0703xyz.pdf
STUVWXYZ0805xyz.pptx

Move to folder
ABCEFGHI\0703\PDF
STUVWXYZ\0805\PPTX

Keeping in mind first 8 characters are random, next 4 character are year and month, and 9 types of extensions.

I am using this batch script to create these folders:-

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\2BW00885"
SET "destdir=C:\2BW00885"
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*" '
) DO (
 SET name=%%~na
 SET ext=%%~xa
 SET name=!name:~0,8!\!name:~8,4!\!ext:~1!
 MD "!name!" 2>nul
 MOVE "%sourcedir%\%%a" "!name!\" >nul
)

GOTO :EOF

This will result in creating many subfolders folders like doc, pdf, txt…

Now I want to create an archive script to search any folder called pdf within destination folders and archive it. I am using this command to archive in DOS:

winrar A -ep1 -df "C:\ ABCEFGHI\0703\PDF" " C:\ ABCEFGHI\0703\PDF\*"

-df to delete the parent folder, - ep1 and * to ignore the folder directory just the content within.

I am thinking to add this

FOR /F "DELIMS==" %%D in ('search for destination folder) DO ( 
 "C:\Program Files\WinRAR\WinRAR.EXE" A -ep1 -df  "destination.rar" "%%D"  
)

Issue is destination folder directory is unknown and random depending on the first script it will create them, I need to script it in such a way that it will search for all destination folders for any folder called pdf and archive it.

Any help is greatly appreciated.

Abdul

1

There are 1 answers

3
Magoo On BEST ANSWER

Somewhat familiar-looking code.

FOR /f "delims=" %%a IN (
 'dir /b /s /ad "%sourcedir%\pdf" '
 ) DO (
 ECHO winrar A -ep1 -df "%%a\destination.rar" "%%a\*"  
)

should work for you. I'm unfamiliar with winrar syntax, so I'll leave that part to you...

It would be appreciated if you were to mark answers as accepted if they solve your problem.


edit : for multiple extensions:

For %%x in (pdf xls txt) do (
 ECHO winrar A -ep1 -df "%%x\destination.rar" "%%x\*"  
)

but, as I say, I'm unfamilair with winrar syntax or what you are attempting to archive in which directory.