Friday, June 11, 2010

Batch script to delete file older than N days

There is a requirement to remove log files generated before N days. Initially, I plan to develop it using java program, and create a job to run the java program via 'Task Scheduler'. But, DOS batch script is easier than java program.

The following script is what I did.

@echo off

REM ******************************************************
REM target driver where target folder locates
REM ******************************************************

set td=c:

REM ******************************************************
REM target folder where log files need to be removed
REM ******************************************************

set tf=c:\tmp\test

REM ******************************************************
REM set how many days, -30 means log files generated before 30 days will be removed
REM ******************************************************

set expiryDate=-30

REM ******************************************************
REM execute removal
REM ******************************************************

CD %td%
CD %tf%

echo The following files will be removed.
FORFILES /M oblog.log.* /D %expiryDate% /C "cmd /c echo @path"

FORFILES /M oblog.log.* /D %expiryDate% /C "cmd /c del @path"

@echo on

------------------------------------------

Syntax

forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c "<Command>"] [/d [{+|-}][{<Date>|<Days>}]]

Parameters

 

Parameter Description
/p <Path>
Specifies the path from which to start the search. By default, searching starts in the current working directory.
/m <SearchMask>
Searches files according to the specified search mask. The default search mask is *.*.
/s
Instructs the forfiles command to search into subdirectories recursively.
/c "<Command>"
Runs the specified command on each file. Command strings should be enclosed in quotation marks. The default command is "cmd /c echo @file".
/d [{+|-}][{<Date>|<Days>}]
Selects files with a last modified date within the specified time frame.
  • Selects files with a last modified date later than or equal to (+) or earlier than or equal to (-) the specified date, where Date is in the format MM/DD/YYYY.
  • Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified.
  • Valid values for Days include any number in the range 0–32,768. If no sign is specified, + is used by default.
/?
Displays help at the command prompt.

Remarks

  • Forfiles is most commonly used in batch files.
  • Forfiles /s is similar to dir /s.
  • You can use the following variables in the command string as specified by the /c command-line option.

     

    Variable Description
    @FILEFile name.
    @FNAMEFile name without extension.
    @EXTFile name extension.
    @PATHFull path of the file.
    @RELPATHRelative path of the file.
    @ISDIREvaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE.
    @FSIZEFile size, in bytes.
    @FDATELast modified date stamp on the file.
    @FTIMELast modified time stamp on the file.
  • With forfiles, you can run a command on or pass arguments to multiple files. For example, you could run the type command on all files in a tree with the .txt file name extension. Or you could execute every batch file (*.bat) on drive C, with the file name "Myinput.txt" as the first argument.
  • With forfiles, you can do any of the following:

    • Select files by an absolute date or a relative date by using the /d parameter.
    • Build an archive tree of files by using variables such as @FSIZEand @FDATE.
    • Differentiate files from directories by using the @ISDIRvariable.
    • Include special characters in the command line by using the hexadecimal code for the character, in 0xHH format (for example, 0x09 for a tab).
  • Forfiles works by implementing the recurse subdirectories flag on tools that are designed to process only a single file.

Examples

To list all of the batch files on drive C, type:
forfiles /p c:\ /s /m *.bat /c "cmd /c echo @file is a batch file"
To list all of the directories on drive C, type:
forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==true echo @file is a directory"
To list all of the files in the current directory that are at least one year old, type:
forfiles /s /m *.* /d -365 /c "cmd /c echo @file is at least one year old."
To display the text "File is outdated" for each of the files in the current directory that are older than January 1, 2007, type:
forfiles /s /m *.* /d -01/01/2007 /c "cmd /c echo @file is outdated." 
To list the file name extensions of all the files in the current directory in column format, and add a tab before the extension, type:
forfiles /s /m *.* /c "cmd /c echo The extension of @file is 0x09@ext" 

Wednesday, June 9, 2010

replicate session in clustering weblogic v10.3

Even set 2 weblogic instances as clustering servers, the session can not be replicated by default.

let's we have the code to get session as follows.
HttpSession session = request.getSession();
Object myObj = session.getAttribute("myObjKey");

 If want to replicate the session during clustering weblogic instances, need to modify weblogic.xml as follows.

<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">

    <session-descriptor>
        <persistent-store-type>replicated_if_clustered</persistent-store-type>
        <sharing-enabled>true</sharing-enabled> 
    </session-descriptor>

    <container-descriptor>
        <prefer-web-inf-classes>true</prefer-web-inf-classes>
    </container-descriptor>

    <context-root>/mycontext</context-root>

</weblogic-web-app>

-- persistent-store-type
    Sets the persistent store method to one of the following options:
  • memory—Disables persistent session storage.
  • replicated—Same as memory, but session data is replicated across the clustered servers.
  • replicated_if_clustered—If the Web application is deployed on a clustered server, the in-effect persistent-store-type will be replicated. Otherwise, memory is the default.
  • async-replicated—Enables asynchronous session replication in an application or web application. See Configuring Session Persistence.
  • async-replicated-if-clustered—Enables asynchronous session replication in an application or web application when deployed to a cluster environment. If deployed to a single server environment, then the session persistence/replication defaults to in-memory. This allows testing on a single server without deployment errors.
  • file—Uses file-based persistence (See also persistent-store-dir).
  • async-jdbc—Enables asynchronous JDBC persistence for HTTP sessions in an application or web application. See Configuring Session Persistence.
  • jdbc—Uses a database to store persistent sessions. (see also persistent-store-pool).
  • cookie—All session data is stored in a cookie in the user’s browser.
--  sharing-enabled
     Enables Web applications to share HTTP sessions when the value is set to true at the application level.
     This element is ignored if turned on at the Web application level. 
 
--  prefer-web-inf-classes
     if set to true, will cause classes located in the WEB-INF directory of a Web application to be loaded in    preference to classes loaded in the application or system classloader. The default value is false. A value specified in the console will take precedence over a value set manually. 
 
--  context-root
The context-root element defines the context root of this stand-alone Web application. If the Web application is part of an EAR, not stand-alone, specify the context root in the EAR’s META-INF/application.xml file. A context-root setting in application.xml takes precedence over context-root setting in weblogic.xml.
Note that this weblogic.xml element only acts on deployments using the two-phase deployment model.
The order of precedence for context root determination for a Web application is as follows:
  1. Check application.xml for context root; if found, use as Web application’s context root.
  2. If context root is not set in application.xml, and the Web application is being deployed as part of an EAR, check whether context root is defined in weblogic.xml. If found, use as Web application’s context root. If the Web application is deployed standalone, application.xml does not come into play and the determination for context-root starts at weblogic.xml and defaults to URI if it is not defined there.
  3. If context root is not defined in weblogic.xml or application.xmll, then infer the context path from the URI, giving it the name of the value defined in the URI minus the WAR suffix. For instance, a URI MyWebApp.war would be named MyWebApp.
Note: The context-root element cannot be set for individual Web applications in EAR libraries. It can only bet set for Web application libraries.