Solved

How to create a batch file that will rename a file when it reaches a certain size?

Posted on 2008-10-02
3
332 Views
Last Modified: 2010-05-18
I'd like to know how to create a batch file that will rename a particular file to the current date once it reaches a certain size limit.  For example, "logfile.log" reaches 50MB today, so it gets renamed to 10-2-08.log instead.

So far I've been working with the attached script that I found somewhere else, but every time I run it I get this message:
"No parameter given, or file does not exist, or file size is less than 50 MB" (Obviously that's the message I gave it, but why is it not working?)

Thanks to anyone who can help.



@echo off
if "%*"=="" goto Error
if not exist "%*" goto Error
 
set limit=75000
for /F %%a in ('echo %*') do if %%~za LSS %limit% goto error
for /F "tokens=2" %%a in ('echo %date%') do set MyDate=%%a
ren logfile.log "batch-%MyDate:/=%.*
goto :eof
 
:Error
echo.
echo No parameter given, or file does not exist,
echo or file size is less than 75 MBytes.

Open in new window

0
Comment
Question by:NJDfan1711
[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
  • Learn & ask questions
3 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 125 total points
ID: 22627632
are you calling the batch file with the filename as an argument?

filename.bat c:\logfile.log

??
0
 
LVL 8

Expert Comment

by:Shmoid
ID: 22627861
The code is fine except for the size limit (see below)

Aside from that, there are three checks performed:
1. Did you specify the filename when invoking the batch file, if not goto error.
2. If you did specify the filename, does that filename exist, if not goto error.
3. If you specified the filename and it exists, is it larger than 75000, if not goto error.
So, if you didn't specifiy the name OR the filename does not exist OR the file is smaller than 75000 it will print the error statement.

Also, the limit in your batch file is in bytes and will rename the file if it is larger than 75K you need to change it to 75000000 (6 zeros not 3)
0
 
LVL 3

Author Closing Comment

by:NJDfan1711
ID: 31502522
Believe it or not, I was not doing that.  So simple... thanks for helping out a newbie!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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