1.

Solve : Rar files by folder and by month to new destination.?

Answer»

I'm looking to create a batch that will take a set of files created in each month/year and rar them to an archive folder, labeling the rar by date and folder path.

Here's the situation - I have a server that has 12 years of ftp files, uploaded to three directories by each user. The user would upload the files every day. Because they were never moved, each folder has thousands of files.

I would like to move the files by month and year, keeping them grouped in an Archive folder of the same structure, for later retrieval if needed.



There are basically 3 variables: , , and .


Examples:

Source: \\networkserver\\\ (50 files Created between 01/01/01 and 01/31/01) 
Dest: \\networkserver\\ARCHIVE\\__01-2001.rar  (containing all fifty files from that month).
Source: \\networkserver\\\ (50 files Created between 01/01/01 and 01/31/01) 
Dest: \\networkserver\\ARCHIVE\\__01-2001.rar  (containing all fifty files from that month).


Source: \\networkserver\\\ (50 files Created between 01/01/01 and 01/31/01) 
Dest: \\networkserver\\ARCHIVE\\__01-2001.rar  (containing all fifty files from that month).
Source: \\networkserver\\\ (50 files Created between 01/01/01 and 01/31/01) 
Dest: \\networkserver\\ARCHIVE\\__01-2001.rar  (containing all fifty files from that month).

Real Example:

Source: \\uploader500gb\mary001\newpdfs\edit001.pdf (345 files created between 01/01/01 and 01/31/01) 
Dest: \\uploader500gb\mary001\ARCHIVE\newpdfs\mary001_newpdfs_01-2001.rar  (containing all 345 from that month and folder of that user).

Source: \\uploader500gb\mary001\reusedpdfs\reused001.pdf (95 files created between 01/01/01 and 01/31/01) 
Dest: \\uploader500gb\mary001\ARCHIVE\reusedpdfs\mary001_resusedpdfs_01-2001.rar  (containing all 95 from that month and folder of that user).

Source: \\uploader500gb\mary001\errors\pdffail001.pdf (11 files created between 01/01/01 and 01/31/01) 
Dest: \\uploader500gb\mary001\ARCHIVE\errors\mary001_errors_01-2001.rar  (containing all 11 files from that month and folder of that user).


IT would group the files into RAR'd archives, repeat for every of every for every within every folder.

I spent the last 6 hours or so looking at how to CHOP up the date of a file to get DOS to recognize it; I've looked at winRar's command line structure to see how to grab files and format the naming convention.. But I'm an OS X guy - I understand bash, I get applescript. I understand how to read DOS batch, and how to write the BASICS, but I've never been good at the FOR command and how to use it to grab only certain files, how to loop, how to apply variables to it...


I'm guessing that I would have to do this in a few steps:


1. Get the folder as a variable
2. Get the folder as another variable
3. Get the and as variables
4. Create the \ARCHIVE\\
4a. Create a _log.txt of archive creation for each folder structure:  "Created rar \ for -"
5. Pass the variables to Win rar:
5a. Grab all files from \\*.* in same and
5b. Create RAR of \ARCHIVE\\_-.rar
5c. Apply errorlevel LOGIC to verify rar creation.
5d. Continue or repeat, based on success or failure
6. Upon success, Delete the files that were placed into the RAR, after completion, to avoid accidental deletion.
6a. Add to log: "Completed rar and deleted original files of \ for -"
7. Stop within 60 days of current date (leaving -60 days of files untouched in each directory)
8. Repeat with next directory, and repeat inside that with each and each and


I'm not asking for a handout - I'd be more that willing to put the pieces together. I just don't know where to start with all this - beyond the outline. I am going to keep looking at how to build this, but I would welcome some pointers on this, and maybe I can get a skeleton of it working?

I'll keep posting my code as I get the pieces in place, but I could really use some help. Thanks in advance!








That is a really tall order, especially for an OS X guy, so good on you for trying to take it on. It would take anyone a little while to build this, but I think I'll give you some basics and then see how you do at putting them all together.

First, you are going to want to map your drives: Code: [Select]net use z: \\networkserver
This allows you to break things up a little easier. To grab the user variable, try this: Code: [Select]for /f "DELIMS=\ tokens=2" %%A in ('dir /s /b z:') do set user=%%A (This will actually grab the last user in the file first, but working backwards is just the same as working forwards so long as it gets done)

To get the Directory variable we can use this: Code: [Select]for /f "delims=\ tokens=3" %%B in ('dir /s /b z:\%user%') do set directory=%%B
The date variables might end up being a little TRICKIER to capture, depending on what your regional date/time settings expand out to. For instance, my date/time settings have the file creation expanding to 11/08/2011 10:24 AM. Obviously we don't care about the time in this instance, but if your date expands differently, it throws the whole thing off. You will need to use setlocal enabledelayedexpansion in order to use this next line to capture the date variables.
Code: [Select]for /f "tokens=*" %%C in ('dir /s /b /o:d') do (
  set datevar=%%~tC
  set year=!datevar:~6,4!
  set month=!datevar:~0,2!
)
One other part to this, testing for the 60 day threshold, can be done as long as you expand out the %date% variable and see how it looks. Mine is like this: Thu 01/26/2012 so in order to test I would need to parse the date variable out and check it with the variables set in the line above.

See how far you get with this, and let us know if you get stuck.Ok, so I think i have a solution in place:

Using WinRar, RoboCopy, and Dos Batch, I was able to do it.




Code: [Select]
ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cls
 
:: Insert Calls Here
 
 
set client=User1

:: Call (loops)
:: dothis is the process.
:: %client% is the foldr name
:: Example: call:dothis %client% 01-2001 20010101 20010131
:: Example: call:dothis %client% foldrname startdate enddate
 
call:dothis %client% 01-2001 20010101 20010131
call:dothis %client% 02-2001 20010201 20010228
call:dothis %client% 03-2001 20010301 20010331
call:dothis %client% 04-2001 20010401 20010430
call:dothis %client% 05-2001 20010501 20010531
::etc...



:dothis
 
set theorigpath=\\servername\users\ftpFiles\%~1\FromCust\original
set theerrorpath=\\servername\users\ftpFiles\%~1\FromCust\error
set theformattedpath=\\servername\users\ftpFiles\%~1\FromCust\formatted
set thecompletepath=\\servername\users\ftpFiles\%~1\FromCust\complete
 
set theorigarchivepath=\\servername\users\ftpFiles\%~1\FromCust\archive\%~2-original
set theerrorarchivepath=\\servername\users\ftpFiles\%~1\FromCust\archive\%~2-error
set theformattedarchivepath=\\servername\users\ftpFiles\%~1\FromCust\archive\%~2-formatted
set thecompletearchivepath=\\servername\users\ftpFiles\%~1\FromCust\archive\%~2-complete
set thebasearchivepath=\\servername\users\ftpFiles\%~1\FromCust\archive\
 
 
 
Echo -- Making Archive Dirs --
Echo.
mkdir %theorigarchivepath%
mkdir %theerrorarchivepath%
mkdir %theformattedarchivepath%
mkdir %thecompletearchivepath%
Echo.
Echo -- Made Archive Dirs --
 
 
 
Echo ------------------------------
Echo -- Copying %theorigpath% to %theorigarchivepath% for files between %~3 and %~4
 
cd C:\Program Files\Windows Resource Kits\Tools
 
robocopy %theorigpath% %theorigarchivepath% *.* /MOV /MAXAGE:%~3 /MINAGE:%~4
robocopy %theerrorpath% %theerrorarchivepath% *.* /MOV /MAXAGE:%~3 /MINAGE:%~4
robocopy %theformattedpath% %theformattedarchivepath% *.* /MOV /MAXAGE:%~3 /MINAGE:%~4
robocopy %thecompletepath% %thecompletearchivepath% *.* /MOV /MAXAGE:%~3 /MINAGE:%~4
 
 
 
Echo Copied %theorigpath% to %theorigarchivepath% for files between %~3 and %~4 >> %thebasearchivepath%\Log.txt
 
cd C:\Program Files\WinRAR
 
rar a -df %theorigarchivepath%.rar %theorigarchivepath%
rar a -df %theerrorarchivepath%.rar %theerrorarchivepath%
rar a -df %theformattedarchivepath%.rar %theformattedarchivepath%
rar a -df %theorigarchivepath%.rar %theorigarchivepath%
rar a -df %thecompletearchivepath%.rar %thecompletearchivepath%

... IT's not completely automatic, I did have to use excel to mass create the date ranges, but it is working.



Discussion

No Comment Found