Solved

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

Posted on 2014-03-03
21
1,896 Views
Last Modified: 2014-03-05
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
Comment
Question by:Skumar_CCSA
  • 8
  • 7
  • 3
  • +1
21 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
 
LVL 13

Expert Comment

by:Santosh Gupta
Comment Utility
append this..

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

Author Comment

by:Skumar_CCSA
Comment Utility
hi gupta....
you mean to append the above command line in my batch file itself ?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 13

Expert Comment

by:Santosh Gupta
Comment Utility
@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
 

Author Comment

by:Skumar_CCSA
Comment Utility
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
 
LVL 13

Expert Comment

by:Santosh Gupta
Comment Utility
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
 

Author Comment

by:Skumar_CCSA
Comment Utility
Hi Gupta...

Thanks for help....
Is it possible if .gz zip format ?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
 

Author Comment

by:Skumar_CCSA
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:Skumar_CCSA
Comment Utility
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
 

Author Closing Comment

by:Skumar_CCSA
Comment Utility
initial question for single file move.
it worked....
faster response...
appreciated your kind...
thank to help.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:Skumar_CCSA
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
 

Author Comment

by:Skumar_CCSA
Comment Utility
Understood....
thanks for info..
Actually I  need to raise another request for zip matters....
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now