Reader to Reader: Rename Old Logs

Downloads
20802.zip

I often run batch files on Windows NT and VMS. By default, VMS has file-version capability, which means that if I run a batch file that produces logs, VMS keeps and renames the old logs rather than overwrites them. To implement a similar system for NT, I created RenameLogs.bat. This script renames old logs the same way in which Microsoft SQL Server renames its error logs.

RenameLogs.bat accepts two parameters: the log's original filename and the number of old files to keep. (RenameLogs.bat works only with files whose filenames don't include spaces.) You pass in these parameters on the command line when you launch the script. For example, if you type the command

Call RenameLogs.bat
   C:\temp\test.log 5

RenameLogs.bat performs the following tasks:

  • splits the filename C:\temp\test.log into pathname, filename, and file extension
  • deletes C:\temp\test_5.log (if it exists)
  • renames C:\temp\test_4.log to C:\temp\test_5.log
  • renames C:\temp\test_3.log to C:\temp\test_4.log
  • renames C:\temp\test_2.log to C:\temp\test_3.log
  • renames C:\temp\test_1.log to C:\temp\test_2.log
  • renames C:\temp\test.log to C:\temp\test_1.log

Now you can create a new C:\temp\test.log output file.

Listing 2 contains RenameLogs.bat. You can use this script on Windows 2000 and NT systems.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish