|
Answer» Hi,
I want to create a BAT file which will detect file size for specific file and rename it if file size exceeds 10mb.
file name is OraEss.log and new name should be OraEss.old
Please help me with this.
Thank you.Some concerns... its a log file and is it always in use by the service that write to this log file?
I see what your trying to accomplish, but it might only be able to be accomplished if the log file is not in use by the program/service that has it 'file open' in the programs code. If the program/service has a 'file close' statement in it code then a batch could rename it while its closed. However you will get an error if trying to rename a file that is in use.
Your batch might require running a DIR command and PARSING for file size and then when file size exceeds 10MB then kill the service/program which could have negative impacts such as database corruption if a database is writing data to tables etc when the program/service is killed and then rename file and then restart the program/service.
I had a similar issue with a TeamPOS point of sale system where server logs were climbing in size. The fix was to stop the service at 23:59 when the database was quiet with no ACTIVITY, create a folder named that days date, stop the mysql database service properly, then stop the program from writing to log file, and move the log file to a folder. Then restart the services at MIDNIGHT and have this as a scheduled task on the server. This then caused log files to be easier to manage and only 500k in size vs growing into the 20+ meg range when notepad freaks out when trying to view them etc.
What SOFTWARE or service is writing so we know its nature to know best way to assist with this?
* If a database is involved which most of the time is these days you will need to gracefully stop the database before killing the program/service to avoid problems.A question: what do you want to happen if OraEss.old already exists?
Something along these lines ought to work...
Enclosed Code removed upon Authors request...
Do NOT USE the above code! I made a BAD ERROR! I used the overwrite operator instead of the append one in the file-in-use test. This would be destructive of data! Please see below.
@echo off REM You can find a file size using FOR for %%A in (OraEss.log) do set fsize=%%~zA REM use 10000000 if you want decimal megabytes REM use 10485760 if you want binary megabytes if %fsize% LSS 10000000 goto end REM if the file is not in use you can append a newline to it :loop echo. >> OraEss.log && ( ren OraEss.log OraEss.old goto end ) REM wait 5 to 6 seconds PING 1.1.1.1 -n 1 -w 60000 >NUL goto loop :endImproved file-in-use detection (does not alter file):
@echo off for %%A in (OraEss.log) do set fsize=%%~zA REM use 10000000 if you want decimal megabytes REM use 10485760 if you want binary megabytes if %fsize% lss 10000000 goto end REM if the file is not in use you can append to it REM Append NUL (i.e. nothing) :loop (type nul >> OraEss.log) 2>nul && ( ren OraEss.log OraEss.old goto end ) REM wait 5 to 6 seconds PING 1.1.1.1 -n 1 -w 60000 >NUL goto loop :end
|