I have a script that does the following
'Create a bat file
Set objBatFile = objFS.CreateTextFile("X:\" & strType & "\closeit.bat",True)
intExitCode = objBatFile.Write("for /f " & chr(34) & "skip=4 tokens=1" & chr(34) & " %%a in ('net files') do net files %%a /close")
objBatFile.Close
'Run the bat file to close all files in the directory
intExitCode = objShell.Run("X:\" & strType & "\closeit.bat", intWindowStyle, blnWaitOnReturn)
StopStart
'Delete the bat file
Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("X:\" & strType & "\closeit.bat") 'Deletes the file throught the DeleteFile function
I was wondering if any one knew of a way to use vbscript to replace the .bat file command I am using to close open files on the server? The line I want to replace is below for better clarity:
for /f " & chr(34) & "skip=4 tokens=1" & chr(34) & " %%a in ('net files') do net files %%a /close
Execute cmd:
intExitCode = objShell.Run("CMD.EXE /C for /f ""skip=4 tokens=1"" %a in ('net files') do net files %a /close"), blnWaitOnReturn)
Note that FOR outside batch expects single % before variable.
You can replace "+Chr(34)+" with just "" (at least in VBA and VB.NET).
Related
I use the following code VBS to read a text file. How can I save the contents of this text file to my computer clipboard to paste elsewhere?
Option Explicit
Dim objFileToRead, strFileText, content
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\1.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Here is an easy way to do it using clip command line :
Option Explicit
Dim Title : Title = "Copy the contents of a text file to the clipboard"
Dim File2Read : File2Read = "C:\1.txt"
If CreateObject("Scripting.FileSystemObject").FileExists(File2Read) Then
CreateObject("WScript.Shell").Run "cmd.exe /c clip < "& File2Read &"",0,TRUE
MsgBox "The contents of file : " & chr(34) & File2Read & chr(34) & vbCrlf & _
" is copied to the clipboard !",vbInformation,Title
Else
MsgBox "Check if the " & chr(34) & File2Read & chr(34) &" exists !",vbExclamation,Title
End If
EDIT : Save a ping test into file and copy it to clipboard !
Option Explicit
Dim Title : Title = "Copy the contents of a text file to the clipboard"
Dim File2Read : File2Read = "E:\Yahoo_Ping.txt"
CreateObject("WScript.Shell").Run "CMD /C Ping www.yahoo.com > "& File2Read &"",1,True
If CreateObject("Scripting.FileSystemObject").FileExists(File2Read) Then
CreateObject("WScript.Shell").Run "CMD /U /C clip < "& File2Read &"",0,TRUE
MsgBox "The contents of file : " & chr(34) & File2Read & chr(34) & vbCrlf & _
" is copied to the clipboard !",vbInformation,Title
Else
MsgBox "Check if the " & chr(34) & File2Read & chr(34) &" exists !",vbExclamation,Title
End If
With Batch and Powershell :
#echo off
Title Set-Clipboard and Get-ClipBoard with Powershell and Batch
Set "InputFile=%Temp%\%~n0.txt"
echo Please be patient ....
Ping www.stackoverflow.com > "%InputFile%"
Set "OutPutFile=%userprofile%\Desktop\MyClipBoard.txt"
Call :RunPS-Get-ClipBoard "%InputFile%" "%OutPutFile%"
Cls & Type "%OutPutFile%"
Pause & Exit
REM ----------------------------------------------------------------------
:RunPS-Get-ClipBoard <InputFile> <OutPutFile>
Set psCmd="&{Set-Clipboard -Value (GC '"%~1"') | Get-ClipBoard}"
If Exist "%~2" Del "%~2"
#for /F "tokens=*delims=" %%i in ('Powershell %psCmd%') do >> "%~2" echo %%i
Goto:eof
REM ----------------------------------------------------------------------
I wrote a vbscript to determine what are the vbscripts running in background but when I execute my script . it only opens the folder of my script not the other scripts location or folder . What should I do?? Please help
Myscript.vbs
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strPath = "explorer.exe /e," & strFolder
objShell.Run strPath
Please help guys . I am very new to vbscript .
I have for you an old vbscript since 2015 that can tell you what vbscript is running in the background with its command line of course to get their paths and you have a possiblity to choose what vbscript you want to kill and you get in the end of the script a log file for this.
So you can give a try for this first, and i will edit and update it for any modifications in order to answer your question.
Option Explicit
Dim Titre,Copyright,fso,ws,NomFichierLog,temp,PathNomFichierLog,OutPut,Count
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,_
"There is an existing proceeding !"
WScript.Quit
Else
Copyright = "[Hackoo "& chr(169) & " 2015]"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject( "Wscript.Shell" )
NomFichierLog="Killed_Process.txt"
temp = ws.ExpandEnvironmentStrings("%temp%")
PathNomFichierLog = temp & "\" & NomFichierLog
Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,1)
Call Find("wscript.exe")
Call Explorer(PathNomFichierLog)
End If
'***************************************************************************************************
Function Explorer(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run "Explorer "& File & "\",1,True
end Function
'***************************************************************************************************
Sub Find(MyProcess)
Dim colItems,objItem,Processus,Question,Msg
Titre = " Process(es) "& DblQuote(MyProcess) &" running "
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like " & CommandLineLike(WScript.ScriptFullName) & "",,48)
Count = 0
For Each objItem in colItems
Count= Count + 1
Processus = objItem.CommandLine
Question = MsgBox ("Would do you like to kill this script : " & DblQuote(Processus) & " ?" ,VBYesNO+VbQuestion,Titre+Copyright)
If Question = VbYes then
objItem.Terminate(0)'To kill the process
OutPut.WriteLine Processus
else
Count= Count - 1 'decrementing the count of -1
End if
Next
OutPut.WriteLine String(100,"*")
If Count > 1 Then
Msg = " were killed"
Else
Msg = " was killed"
End if
OutPut.WriteLine count & Titre & Msg
OutPut.WriteLine String(100,"*") & VbCrLF
End Sub
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'**************************************************************************
EDIT : Extract_CommandLine.bat
Copy and paste this code as batch file in order to extract the command line of each process.
Set ProcessNames="cmd.exe" "mshta.exe" "powershell.exe" "cscript.exe" "wscript.exe"
You can add or remove the process name from the variable ProcessNames between the double quotes.
#echo off
Title Extract CommandLine Of Running Processes by Hackoo 2020
Mode 110,10 & color 0A
Set "TmpFile=%~n0_Abs_cmdline.txt"
Set "LogFile=%~n0_cmdline.txt
If Exist "%TmpFile%" Del "%TmpFile%"
If Exist "%LogFile%" Del "%LogFile%"
Set ProcessNames="cmd.exe" "mshta.exe" "powershell.exe" "cscript.exe" "wscript.exe"
SetLocal EnableDelayedExpansion
for %%A in (%ProcessNames%) Do (
Call :GetCommandLine %%A ProcessCmd
If defined ProcessCmd (
echo !ProcessCmd!>con
echo !ProcessCmd!>>"%TmpFile%"
)
)
Timeout /T 3 /NoBreak>nul
If Exist "%TmpFile%" Call :Extract "%TmpFile%" "%LogFile%"
If Exist "%LogFile%" Start "" "%LogFile%"
If Exist "%LogFile%" Call :ExplorerIT "%LogFile%"
Exit
::---------------------------------------------------------------------------------------------------------------
:GetCommandLine <ProcessName> <ProcessCmd>
Set "ProcessCmd="
for /f "tokens=2 delims==" %%P in (
'wmic process where caption^="%~1" get commandline /format:list ^| findstr /I "%~1" ^| find /I /V "%~nx0" 2^>nul'
) do (
if not defined %2 Set "%2=%%P"
)
Exit /b
::---------------------------------------------------------------------------------------------------------------
:Extract <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Data = Extract(Data,"(^?^!.*(\x22\w^)^)\b.*(\w^).*(\.ps1^|\.hta^|\.vbs^|\.vbe^|\.cmd^|\.bat^|\.lnk^)"^)
echo WScript.StdOut.WriteLine Data
echo Function Extract(Data,Pattern^)
echo Dim oRE,oMatches,Match,Line
echo set oRE = New RegExp
echo oRE.IgnoreCase = True
echo oRE.Global = True
echo oRE.Pattern = Pattern
echo set oMatches = oRE.Execute(Data^)
echo If not isEmpty(oMatches^) then
echo For Each Match in oMatches
echo Line = Line ^& chr(34^) ^& Trim(Match.Value^) ^& chr(34^) ^& vbcrlf
echo Next
echo Extract = Line
echo End if
echo End Function
)>"%tmp%\%~n0.vbs"
cscript /nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::-----------------------------------------------------------------------------------------------------------
:ExplorerIT <LogFile>
#For /f "delims=" %%a in ('Type "%~1"') do (
Start "SelectFile" Explorer /select,"%%~a"
)
Exit /B
::-----------------------------------------------------------------------------------------------------------
I have a batch and a vbs file to zip a folder with a specific directory name and copy it to another folder.
How could I zip only the folder containing today's date?
If it is not possible how could I zip only the folder having today's date as "Date Modified" column?
bat:
#echo off
set "mypath=C:\TEMP\zip\source\JEAR_20190115"
for /f %%i in ('dir /b /a-d "%mypath%"') do set "last=%%~ni"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip
vbs:
'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile,
True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
'Required!
wScript.Sleep 2000
For example the following file is older than today so it shouldn't be zipped.
set "mypath=C:\TEMP\zip\source\JEAR_20190115"
If it would be equal to today's date the script should run:
set "mypath=C:\TEMP\zip\source\JEAR_20190117"
Previous question: https://stackoverflow.com/a/54216776/7420833
The explanations remains the same for the rest, as per the previous question, but we simply add some vbs code to get the correct date in the required format.
#echo off
echo >"%temp%\%~n0.vbs" s=DateAdd("d",0,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
if exist "C:\TEMP\zip\source\JEAR_%result%" (
set "mypath=C:\TEMP\zip\source\JEAR_%result%"
) else (
exit
)
if "%mypath:~-1%"=="\" set "mypath=%mypath:~0,-1%"
for %%i in (%mypath%) do set "last=%%~nxi"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip
The reason for using
Is it possible to run any type of file (like,. mp3, doc, exe..) with a Vbscript file at a specific time?
I looked in many places but there were no success..
Give a try for this example that can schedule Notepad to be executed everyday at 10:00 AM
So, you need just to modify those 3 arguments in this script to yours :
TaskName
App_FullPath
strTime
Option Explicit
Dim TaskName,App_FullPath,strTime
TaskName = "Execute_Notepad"
App_FullPath = "C:\windows\notepad.exe"
strTime = "10:00"
Call CreateTask(TaskName,App_FullPath,strTime)
'*****************************************************************
Sub CreateTask(TaskName,App_FullPath,strTime)
Dim ws,strtask,exitcode
Set ws = CreateObject("Wscript.Shell")
strtask = "schtasks /create /sc Daily /tn "& qq(TaskName) & _
" /tr "& qq(App_FullPath) & _
" /st " & strTime & " /f"
exitcode = ws.Run(strtask, 0, True)
If exitcode <> 0 Then
WScript.Echo "External command failed: " & Hex(exitcode)
Else
wscript.echo "Success !"
End If
End Sub
'*****************************************************************
Function qq(str)
qq = chr(34) & str & chr(34)
End Function
'*****************************************************************
Edit : Batch file to show or delete a numbered task names
#echo off
Mode 100,3 & color 0A
Title Delete Tasks with their time tasks execution by Hackoo 2017
:Menu
Mode 100,3 & color 0A
cls & echo(
echo Type the time with this format "hh:mm" or a task name to show or delete for scheduled Tasks
set /a "count=0"
set /p "strTime="
cls & echo(
echo Please wait a while ... looking for a scheduled Tasks for "%strTime%"
Setlocal EnableDelayedExpansion
#for /f "tokens=1 delims=," %%a in ('schtasks /query /fo csv ^| find /I "%strTime%"') do (
set /a "Count+=1"
set "TaskName[!Count!]=%%~a"
)
Rem Display numbered Task Names
Mode 90,30 & cls & echo(
#for /L %%i in (1,1,%Count%) do (
set "Task=[%%i] - !TaskName[%%i]:~1!"
echo !Task!
)
If not defined Task (
Mode 90,3 & Color 0C
echo(
echo No Scheduled Tasks found with this criteria
Timeout /T 3 /nobreak >nul & goto Menu
)
echo(
Rem Asking user if he wants to delete or not the numbered task names
echo Type the number of the task to delete !
set /p "Input="
#for /L %%i in (1,1,%Count%) Do (
If "%INPUT%" EQU "%%i" (
schtasks /delete /tn "!TaskName[%%i]:~1!"
)
)
echo(
echo Type any key to show and delete another task !
Pause>nul
Goto Menu
In this vbscript you can change 4 arguments :
TaskName
AppFullPath
StartTime
Frequency
Option Explicit
Dim TaskName,AppFullPath,StartTime,Frequency
'************* Four params can be changed here********************
TaskName = "Execute Notepad by Hackoo"
AppFullPath = "C:\windows\notepad.exe"
StartTime = "10:00"
Frequency = "Minute"
REM The value of frequency can be taken
Rem as "MINUTE", "HOURLY", "DAILY", "WEEKLY" or "MONTHLY"
REM https://technet.microsoft.com/en-us/library/bb490996.aspx
REM Don't change anything under this line
'************************** Main *********************************
Call CreateTask(TaskName,AppFullPath,StartTime,Frequency)
'*****************************************************************
Sub CreateTask(TaskName,AppFullPath,StartTime,Frequency)
Dim ws,strtask,exitcode
Set ws = CreateObject("Wscript.Shell")
strtask = "schtasks /create /sc "& Frequency &" /tn "& qq(TaskName) & _
" /tr "& qq(AppFullPath) & _
" /st " & StartTime & " /f"
exitcode = ws.Run(strtask, 0, True)
If exitcode <> 0 Then
WScript.Echo "External command failed: " & Hex(exitcode)
Else
wscript.echo "The Task "& qq(TaskName) & " is created successfully !"& vbcrlf &_
"to be run "& qq(Frequency) &" with a StartTime at " & qq(StartTime) & ""
End If
End Sub
'*****************************************************************
Function qq(str)
qq = chr(34) & str & chr(34)
End Function
'*****************************************************************
How can I rotate the Apache Access and Error logs on a Window 2000 box?
I include my batch file below as an answer.
Is there a way of doing this directly via the Apache config file? I'm currently using the following customlog command to generate daily logs.
CustomLog '|" "*Apache-Path/bin/rotatelogs.exe" "*Apache-Path/logs/backup/internet_access_%d-%m-%y.log" 86400' combined
Here's the DOS batch file, modified as annotated.
I run it weekly and it keeps 8 weeks of zipped backups.
You'll need to install 7 zip.
I haven't parametrised the paths, feel free to.
#echo off
:: Name - svrlogmng.bat
:: Description - Server Log File Manager
::
:: History
:: Date Authory Change
:: 22-May-2005 AGButler Original
:: 14-Jan-2008 AIMackenzie Changed net stops and paths where necessary
:: ========================================================
:: setup variables and parameters
:: ========================================================
:: generate date and time variables
for /f "tokens=2,3,4 delims=/ " %%i in ('date /T') do set trdt=%%k%%j%%i
for /f "tokens=1,2 delims=: " %%i in ('time /T') do set trtt=%%i%%j
set nftu=%trdt%%trtt%
:: set the Number Of Archives To Keep
set /a noatk=8
:: ========================================================
:: turn over log files
:: ========================================================
:: change to the apache log file directory
cd /D "D:\Program Files\Apache Software Foundation\Apache2.2\logs\"
:: stop Apache Service, Move log files and restart Apache Service
"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k stop
echo %nftu% >> access.log
move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\access.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_access.log"
echo %nftu% >> error.log
move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_error.log"
"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k start
:: ========================================================
:: zip todays Access and Error log files, then delete old logs
:: ========================================================
:: zip the files
"D:\Program Files\7-Zip\7z.exe" a -tzip %nftu%_logs.zip %nftu%_access.log %nftu%_error.log
:: del the files
del /Q %nftu%_*.log
:: ========================================================
:: rotate the zip files
:: ========================================================
:: make list of archive zip files
type NUL > arclist.dat
for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_logs.zip ^| find /N "_logs.zip"') do echo %%i = %%j>> arclist.dat
:: count total number of files
for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_logs.zip"') do set tnof=%%i
:: setup for and create the deletion list
set /a negtk=%noatk%*-1
set /a tntd=%tnof% - %noatk%
type NUL>dellist.dat
for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat
:: del the old files
for /F "tokens=3 delims= " %%i in ('find "_logs.zip" dellist.dat') do del /Q %%i
:: remove temp files
del /Q arclist.dat
del /Q dellist.dat
I've a little bit extend the the bat-script. You can use it for english and german dates. You will need 7za.exe at the same directory like script. The log-dir and -files for rotation are explicitly settable.
#echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: ========================================================
:: setup variables and parameters
:: ========================================================
:: USE Service Display Name, Services are space-separated -> Syntax: "Display Name 1" "Display Name 2"
SET ROTATE_SERVICES="Apache2.2"
:: setting LOG-directory, log-files in this directory should be rotate
SET ROTATE_LOGDIR=F:\xampp\apache\logs
:: files which should rotate (space separated)
SET ROTATE_FILES=access.log error.log ssl_request.log
:: SET the Number Of Archives To Keep
SET /a keptarchives=5
:: SET delimiter for date format (english "/", german ".")
SET DATEDEL=.
:: ========================================================
:: DO NOT CHANGE ANYTHING
:: ========================================================
:: Check for existing Log-directory
IF NOT EXIST "%ROTATE_LOGDIR%" (
CALL :LOG Please check your paths to Log Directory
PAUSE
GOTO :EOF
)
:: Check for existing Log-files
FOR %%d IN (%ROTATE_FILES%) DO (
IF NOT EXIST "%ROTATE_LOGDIR%\%%d" (
CALL :LOG File %ROTATE_LOGDIR%\%ROTATE_LOGFILES% does not exist!
PAUSE
GOTO :EOF
)
)
:: generate date and time variables for execution
FOR /f "tokens=1,2,3 delims=%DATEDEL% " %%i IN ('date /T') DO SET execdate=%%k%%j%%i
FOR /f "tokens=1,2 delims=: " %%i IN ('time /T') DO SET exectime=%%i%%j
SET fullexectime=%execdate%_%exectime%
:: ========================================================
:: ========================================================
:: Operations
:: ========================================================
FOR %%d IN (%ROTATE_SERVICES%) DO (
NET STOP %%d
)
FOR %%d IN (%ROTATE_FILES%) DO (
cd /d %ROTATE_LOGDIR%
IF NOT EXIST OLD (MKDIR OLD)
move %%d %ROTATE_LOGDIR%\OLD\%fullexectime%_%%d
)
FOR %%d IN (%ROTATE_SERVICES%) DO (
NET START %%d
)
:: ========================================================
:: ZIP - LOGFILES
:: ========================================================
cd /d %ROTATE_LOGDIR%\OLD
CALL "%~dp0\7za.exe" a %ROTATE_LOGDIR%\OLD\%fullexectime%_log.zip %ROTATE_LOGDIR%\OLD\%fullexectime%_*.log
IF %ERRORLEVEL% NEQ 0 (
CALL :LOG Error while compressing log-file. Log will not deleted and not rotated. Check your OLD-directory!
PAUSE
GOTO :EOF
)
del /Q %fullexectime%_*.log
:: ========================================================
:: ROTATE - ZIPPED LOGFILES
:: ========================================================
:: make list of archive zip files
type NUL > arclist.dat
for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_log.zip ^| find /N "_log.zip"') do echo %%i = %%j>> arclist.dat
:: count total number of files
for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_log.zip"') do set tnof=%%i
:: setup for and create the deletion list
set /a negtk=%keptarchives%*-1
set /a tntd=%tnof% - %keptarchives%
type NUL>dellist.dat
for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat
:: del the old files
for /F "tokens=3 delims= " %%i in ('find "_log.zip" dellist.dat') do del /Q %%i
:: remove temp files
del /Q arclist.dat
del /Q dellist.dat
GOTO :EOF
:LOG
SET MSG=[%DATE%, %TIME: =0%] %*
ECHO.%MSG%
SET MSG=
GOTO :EOF
pause
I wrote a vbs for a Windows 2003 box once. It uses zip.exe for compression (found at info-zip.org) and rotates apache logs and php logs. Logs are rotated when larger than MAX_SIZE. It deletes the oldest logs if log count passes MAX_ROTATIONS.
Please provide feedback on the script.
option explicit
const DEBUG_MODE = false
const MAX_ROTATIONS = 10
const MAX_SIZE = 2097152 ' 2MB
const WEB_LOGS = "c:\path\to\site\logs"
const PHP_LOG = "c:\path\to\phplog"
const APACHE_LOGS = "C:\path\to\Apache2\logs"
const APACHE_SERVICE ="Apache2.2" ' Name of the apache service for restart
const ZIP_APP = "c:\path\to\zip.exe"
const LOGROTATE_LOG = "c:\tmp\logrotate.log"
dim aLogs
aLogs = Array("\error.log","\transfer.log","\sec.log","\phperror.log")
dim oFSO
set oFSO = CreateObject("Scripting.FileSystemObject")
if (not DEBUG_MODE) then
dim oLogFile
set oLogFile = oFSO.CreateTextFile(LOGROTATE_LOG, 2, True)
end if
dim bHasRotated
bHasRotated = false
Print "Starting log rotation"
Print "====================="
ManageWebLogs()
ManageApacheLogs()
ManagePhpLog()
if (bHasRotated = true) then
Print "====================="
RestartService APACHE_SERVICE
end if
Print "====================="
Print "Log rotation finished"
if (not DEBUG_MODE) then
oLogFile.Close
set oLogFile = nothing
end if
set oFSO = nothing
'*****************************************************************************
' Loop through all the subfolders in the weblog directory
sub ManageWebLogs()
dim oLogDirs
set oLogDirs = oFSO.GetFolder(WEB_LOGS)
dim oLogDir
for each oLogDir in oLogDirs.SubFolders
Print "In " & oLogDir.Name
RotateLogs(oLogDir)
next
set oLogDir = nothing
end sub
'*****************************************************************************
' Loop through the log files in the Apache logs directory
sub ManageApacheLogs()
dim oLogDir
set oLogDir = oFSO.GetFolder(APACHE_LOGS)
Print "In " & oLogDir.Name
RotateLogs(oLogDir)
set oLogDir = nothing
end sub
'*****************************************************************************
' Loop through the log files in the Apache logs directory
sub ManagePhpLog()
dim oLogDir
set oLogDir = oFSO.GetFolder(PHP_LOG)
Print "In " & oLogDir.Name
RotateLogs(oLogDir)
set oLogDir = nothing
end sub
'*****************************************************************************
' Traverse through each of the log file types and check if they need rotation
sub RotateLogs(ByVal oFolder)
dim sLog
dim oLog
for each sLog in aLogs
if oFSO.FileExists(oFolder.Path & sLog) then
set oLog = oFSO.GetFile(oFolder.Path & sLog)
if (oLog.Size > MAX_SIZE) then
RotateLog oFolder.Path & sLog
ArchiveLog oFolder.Path & sLog
bHasRotated = true
end if
end if
next
set oLog = nothing
end sub
'*****************************************************************************
' Rotates the given log, by incrementing the file name
sub RotateLog(ByVal sLog)
dim i
dim sOldFile, sNewFile
for i = MAX_ROTATIONS to 1 step -1
sOldFile = sLog & "." & i & ".zip"
sNewFile = sLog & "." & (i+1) & ".zip"
if oFSO.FileExists(sOldFile) and i = MAX_ROTATIONS then
' Delete zipfile
Print "-- Deleting " & sOldFile
oFSO.DeleteFile(sOldFile)
elseif oFSO.FileExists(sOldFile) then
' Rename zipfile
Print "-- Renaming " & sOldFile & " to " & sNewFile
oFSO.MoveFile sOldFile, sNewFile
end if
next
end sub
'*****************************************************************************
' Zips the current log
sub ArchiveLog(ByVal sLog)
Dim oShell
Set oShell = CreateObject("WScript.Shell")
dim sZipFile
sZipFile = sLog & ".1.zip"
Print "-- Archiving " & sLog & " to " & sZipFile
oShell.Run "cmd /c " & ZIP_APP & " -jq " & sZipFile & " " & sLog, 0, true
oFSO.DeleteFile(sLog)
set oShell = nothing
end sub
' ****************************************************************************
' Restarts a given service (in our case Apache)
private sub RestartService( _
ByVal sService _
)
Dim oShell
Set oShell = CreateObject("WScript.Shell")
' Service stopped with 'Net' command
oShell.Run "cmd /c net stop " & sService, 0, true
Print sService & " service stopped"
' Service started with 'Net' command
oShell.Run "cmd /c net start " & sService, 0, true
Print sService & " service restarted"
set oShell = nothing
end sub
'*****************************************************************************
' Echoes out the given message if in debug mode
sub Print(ByVal sMsg)
if (DEBUG_MODE) then
wscript.echo sMsg
else
oLogFile.WriteLine sMsg
end if
end sub
As stated I don't think this is strictly a programming question, but AFAIK there is no built-in functionality in Apache to support log rotation. There's a standard utility on unix systems called logrotate, so that functionality would be redundant on those systems.
If you rephrase your question to ask how you can write a batch file to handle Apache log rotation on Windows 2000, then answer yourself with your batch file source, that might be helpful to others.