• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2166
  • Last Modified:

scripts, batch file, windows 2008, auto delete file, file generation, file rotate every week

Hi Experts.

request your help for file rotation.

i have batch file, it move the file from one location to another location in the same computer.

@echo off
set datetime=%date:~-4%_%date:~-7,2%_%date:~-10,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetime%
move "D:\Program\rsa\key\log\*.audit" "D:\Program\Backup\to HP\AUDIT_TEXT_Files\PRI_RSA_Audit_Text_File_%datetime%.audit"

It move and rename the file with yyyy_mm_dd_hh_mm_ss

i want help for further batch file for keeping last 30 created files from the latest file create.

please help.

regards,
skumar
0
Skumar_CCSA
Asked:
Skumar_CCSA
  • 8
  • 7
  • 3
  • +1
1 Solution
 
Bill PrewCommented:
Give this a try, it should do what you are looking for.

@echo off

REM Specify folders and files to operate on
set BaseDir=D:\Program\rsa\key\log
set DestDir=D:\Program\Backup\to HP\AUDIT_TEXT_Files
set ExtFilter=audit

REM Current date time in YYYY_MM_DD_hh_mm_ss format for timestamp on moved files
set datetime=%date:~-4%_%date:~-7,2%_%date:~-10,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetime%

REM Move all matching files to destination folder renaming with date/time stamp
move "%BaseDir%\*.%ExtFilter%" "%DestDir%\PRI_RSA_Audit_Text_File_%datetime%.%ExtFilter%"

REM Only keep 30 newest files in destination folder
for /F "tokens=* skip=30" %%A in ('dir /b /a-d /tc /o-d "%DestDir%\*.%ExtFilter%"') do del "%DestDir%\%%~A"

Open in new window

~bp
0
 
Santosh GuptaCommented:
append this..

forfiles /p D:\Program\Backup\to HP\AUDIT_TEXT_Files\ /m *.audit /d -30 /c "cmd /c del @file"
0
 
Skumar_CCSAAuthor Commented:
hi gupta....
you mean to append the above command line in my batch file itself ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bill PrewCommented:
In case it matters, FORFILES looks at the last modified date, not the created date which was requested.  That's part of the reason I used DIR /TC for this...

~bp
0
 
Santosh GuptaCommented:
@Skumar_CCSA
yes, you just need to put it at the end of your script. But as  billprew stated, it will work on ast modified date, not the created date.
0
 
Skumar_CCSAAuthor Commented:
Actually these log files will be created by application automatically. If the file has been moved from the log folder. Application did not have log rotation.

What I want...a batch file to move to the file to the backup folder, from their the HP DP agent will take do the folder level backup.

So I want batch file to move the file everyday, rename the file name and keep 30 latest files.

I guessed created date will be better for file rotation instead modified date. Becz if someone open just do little charge in some then the modification date will change it will become latest but the content inside will be old and it did not consistent date logs...please advise if any other way to compare.

Also like to check if possible of zip....

Move the file name with today date and time.
Zip the file in the same name...it will solve some more space issues in the local server.
Thanks to help.
0
 
Santosh GuptaCommented:
Hi,

Below script will do following but on LAST MODIFY date.

1. rename with date
2. Zip it.
3. Move to other location
4. delete files older then 30 day (based on last modify date)


---------------------------------------------------
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c

d:
cd D:\Program\rsa\key\log\
rename filename.audit filename_%XDate%.audit

"C:\Program Files\WinRAR\WinRAR.exe" a D:\Program\rsa\key\log\filename_%XDate%.rar filename_%XDate%.audit
move /y D:\Program\rsa\key\log\filename_%XDate%.rar D:\Program\Backup\to HP\AUDIT_TEXT_Files\


forfiles /p D:\Program\Backup\to HP\AUDIT_TEXT_Files\ /m *.audit /d -30 /c "cmd /c del @file"

------------------------------
0
 
Skumar_CCSAAuthor Commented:
Hi Gupta...

Thanks for help....
Is it possible if .gz zip format ?
0
 
Bill PrewCommented:
What tool do you want to use for creating the .GZ files?  I see that WinRAR was proposed, but I don't think that is free.  7-ZIP can also create .GZ archives and is free.  Or do you have WinZIP or some other tool you want to use?

~bp
0
 
Bill PrewCommented:
Here's an approach using 7-Zip from http://www.7-zip.org/

@echo off

REM Specify folders and files to operate on
set BaseDir=D:\Program\rsa\key\log
set DestDir=D:\Program\Backup\to HP\AUDIT_TEXT_Files
set ExtFilter=audit
set ZipFilter=gz
set Zip=c:\program files\7-zip\7za.exe

REM Current date time in YYYY_MM_DD_hh_mm_ss format for timestamp on moved files
set datetime=%date:~-4%_%date:~-7,2%_%date:~-10,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetime%

REM Move all matching files to destination folder renaming with date/time stamp
"%Zip%" a -tgzip "%DestDir%\PRI_RSA_Audit_Text_File_%datetime%.%ZipFilter%" "%BaseDir%\*.%ExtFilter%"
del "%BaseDir%\*.%ExtFilter%" /y

REM Only keep 30 newest files in destination folder
for /F "tokens=* skip=30" %%A in ('dir /b /a-d /tc /o-d "%DestDir%\*.%ZipFilter%"') do del "%DestDir%\%%~A"

Open in new window

~bp
0
 
skullnobrainsCommented:
you may want to look into logrotate equivalents for windows

logrotate native reimplementation
http://sourceforge.net/projects/logrotatewin/

python implementation
http://sourceforge.net/directory/security-utilities/logging/log-rotation/os:linux/freshness:recently-updated/

bundled cygwiin ddls and original logrotate program
https://www.itefix.no/i2/logwot8
0
 
Skumar_CCSAAuthor Commented:
hi bp,

the first one works great....for single file.
but source folder has more then one folder then it did not work...
can you please help..
0
 
Bill PrewCommented:
Okay, I'll need a little more info on exactly where the files are in the source folder(s).  It sounds like there are subfolders under the main source folder?  And you want all the files in each subfolder to be moved?  Are there subfolders in the subfolders, or just one level?

But you don't want the files in subfolders in the dest folder, right?  Can there be multiple files with the same name in different subfolders, that will be a problem.

Do you want me to modify the version of my script that zipped, or the other one with no zipping?

By the way, my script did handle multiple files in the source folder, so I assume you meant that it didn't handle multiple folders?

~bp
0
 
Skumar_CCSAAuthor Commented:
Hi Bp..

Apologies....
Your script is excellent....
I made mistake in last comment, not folders.
Multiple files....
If I have one file as nx.audit then it works.
If I have more then one file in the same ext then it did not work.
I am trying  the batch file. Without. Zip
0
 
Skumar_CCSAAuthor Commented:
initial question for single file move.
it worked....
faster response...
appreciated your kind...
thank to help.
0
 
Bill PrewCommented:
The problem with multiple files lies in your original base code that you posted.  It included:

move "D:\Program\rsa\key\log\*.audit" "D:\Program\Backup\to HP\AUDIT_TEXT_Files\PRI_RSA_Audit_Text_File_%datetime%.audit"

Notice that you are moving all files matching *.audit to a single file with the date time stamp in it.  So yes, many files become one, or you will get rename errors due to duplicate destination names.

When you have multiple matching files, how do you want to distinguish them in the destination folder?  Do you want to keep the same base name as the source folder and just ad the date stamp to that?  Or some other approach?

~bp
0
 
Skumar_CCSAAuthor Commented:
Actually I needed for current file to move and rename which is solved using your previous script. Also noticed when admin opens the config window and close then the service restart and it create new file.

So it would be nice if you can help me to rename when more then one file found in the folder, so that it will compress the file and rename which will help to reduce.
0
 
Bill PrewCommented:
Well, when we compress, we can actually add more than one file to a single gzip archive.  This is what my script above that archives does.

Give it a test with multiple source audit files and check that they are all in the single gzip archive.

~bp
0
 
skullnobrainsCommented:
gzip does not handle the storage of multiple files in a single archieve. you need to use one of the other formats supported by 7zip such as 7z, zip, bzip2...
0
 
Skumar_CCSAAuthor Commented:
Understood....
thanks for info..
Actually I  need to raise another request for zip matters....
0
 
skullnobrainsCommented:
if it's only about this, don't bother : just replace "-tgzip" with "-tzip" or whatever other. btw, 7z (lzma) and bzip2 usually produce better compression ration than zip or gzip

if it's about zipping using the stuff builtin windows, i answered to that in one of your other (closed) threads with a couple of links to working examples

you can also use filesystem-level compression (available in the properties of the corresponding files and folders). no idea how much space you will gain.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now