Solved

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

Posted on 2014-03-03
2,002 Views
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.

regards,
skumar
0
Question by:Skumar_CCSA
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 7
• 3
• +1

LVL 54

Accepted Solution

Bill Prew earned 500 total points
ID: 39900253
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"

~bp
0

LVL 13

Expert Comment

ID: 39900256
append this..

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

Author Comment

ID: 39900304
hi gupta....
you mean to append the above command line in my batch file itself ?
0

LVL 54

Expert Comment

ID: 39900339
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

ID: 39900360
@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

ID: 39900407
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

ID: 39900754
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

ID: 39900765
Hi Gupta...

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

LVL 54

Expert Comment

ID: 39901158
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 54

Expert Comment

ID: 39901193
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"

~bp
0

LVL 27

Expert Comment

ID: 39901771
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

ID: 39902220
hi bp,

the first one works great....for single file.
but source folder has more then one folder then it did not work...
0

LVL 54

Expert Comment

ID: 39902290
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

ID: 39902408
Hi Bp..

Apologies....
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

ID: 39902660
initial question for single file move.
it worked....
faster response...
thank to help.
0

LVL 54

Expert Comment

ID: 39903063
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

ID: 39903076
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 54

Expert Comment

ID: 39903091
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 27

Expert Comment

ID: 39906149
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

ID: 39906153
Understood....
thanks for info..
Actually I  need to raise another request for zip matters....
0

LVL 27

Expert Comment

ID: 39906212
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…