Monthly Archives: November 2012

Clear folder (subfolders and files) in BATCH

To delete all files and subfolders in given directory, you can use next snippet:

    @echo off  
    setlocal enableextensions enabledelayedexpansion  

    set pathDirToClear=C:\Tmp  
      
    for /d %%A in (%pathDirToClear%) do (  
        if exist %%A (  
            del /F /Q %%A\*  
            for /d %%B in (%%A\*) do (  
                rd /S /Q %%B  
            )  
        )  
    )  

Dynamically render content in columns


It is need to render some content in columns often if you know columns count.

writer.startElement("table", this);
writer.writeAttribute("border", "0", null);
writer.writeAttribute("style", "width: 100%;", null);
writer.startElement("tr", this);

int nColCount = 2;
int nRowCount = properties.size() / nColCount;
int nFirstRowCount = nRowCount + (properties.size() - (nRowCount * nColCount));

int nCnt = 0;
boolean isFirstColWrited = false;
for (Entry> entry : properties.entrySet()) {
	if (nCnt == 0) {
		writer.startElement("td", this);
		writer.writeAttribute("valign", "top", null);
	} else {
		if (!isFirstColWrited && nCnt % nFirstRowCount == 0) {
			writer.endElement("td");
			writer.startElement("td", this);
			writer.writeAttribute("valign", "top", null);
			isFirstColWrited = true;
		} else if (isFirstColWrited && nCnt % nRowCount == 0) {
			writer.endElement("td");
			writer.startElement("td", this);
			writer.writeAttribute("valign", "top", null);
		}
	}
	// Payload
	String groupName = entry.getKey();
	List groupProperties = entry.getValue();
	writeGroup(writer, groupName, groupProperties);

	nCnt++;
}

writer.endElement("tr");
writer.endElement("table");

properties.size() – some map with data
writer – javax.faces.context.ResponseWriter

Get Java Server Info


To get info about your java server env, use snippet:

Server:
<%=application.getServerInfo()%>
Servlet Specification: <%=application.getMajorVersion()%>.<%=application.getMinorVersion()%>
JSP: <%=JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion()%>

Get work time of execution in BATCH

If you wish to see a time taked by some app execution, use next:

@echo off
setlocal
set hours_before=%time:~0,2%
set minutes_before=%time:~3,2%
set seconds_before=%time:~6,2%

REM Payload here:
timeout /T 5

set hours_after=%time:~0,2%
set minutes_after=%time:~3,2%
set seconds_after=%time:~6,2%
set /a before=%hours_before% * 3600 + %minutes_before% * 60 + %seconds_before%
set /a after=%hours_after% * 3600 + %minutes_after% * 60 + %seconds_after%
set /a timediff=%after%-%before%
if %errorlevel%==0 ( 
	echo Time spent: %timediff% seconds
)
endlocal

pause
exit

JBoss Server restart in BATCH

When JBoss is restarting, it is useful to delete all temporary files. Here the script do it:

@echo off
setlocal enableextensions enabledelayedexpansion

sc stop JBoss
timeout /T 60

set pathJBossProfile=D:\jboss-5.1.0.GA\server\default

set pathDirToClear=
set pathDirToClear=%pathDirToClear%, %pathJBossProfile%\tmp
set pathDirToClear=%pathDirToClear%, %pathJBossProfile%\work\jboss.web

for /d %%A in (%pathDirToClear%) do (
	if exist %%A (
		REM echo %%A
		del /F /Q %%A\*
		for /d %%B in (%%A\*) do (
			rd /S /Q %%B
		)
	)
)

sc start JBoss

[2012-12-10]
New impoved version:

@echo off
setlocal enableextensions enabledelayedexpansion

set checkTimeInterval=5
set svcName=JBoss
set pathJBossProfile=D:\jboss-5.1.0.GA\server\default

for /F "tokens=3 delims=: " %%H in ('sc query %svcName% ^| findstr "        STATE"') do (
	if /I "%%H" EQU "RUNNING" (
		echo Stopping %svcName% service...
		sc stop %svcName%
	)
)

:checkStop
for /F "tokens=3 delims=: " %%H in ('sc query %svcName% ^| findstr "        STATE"') do (
	if /I "%%H" NEQ "STOPPED" (
		timeout /T %checkTimeInterval%
		goto :checkStop
	)
)

set pathDirToClear=
set pathDirToClear=%pathDirToClear%, %pathJBossProfile%\tmp
set pathDirToClear=%pathDirToClear%, %pathJBossProfile%\work\jboss.web

for /d %%A in (%pathDirToClear%) do (
	del /F /Q %%A\*
	for /d %%B in (%%A\*) do (
		echo Deleting: %%B
		rd /S /Q %%B
	)
)

echo Starting %svcName% service...
sc start JBoss

Copy files by mask in destination in BATCH

If you want to copy some file from known location to location by mask (unknown), use next:

@echo off
setlocal enableextensions enabledelayedexpansion

set pathFrom=D:\ScriptCopyTest\my1.java
set pathTo=D:\ScriptCopyTest\Test*

set copy_opts=/I /Y /R /V /F /H

for /d %%f in (%pathTo%*) do ( xcopy %pathFrom% %%f %copy_opts% )

pause

Get current dir path in BATCH file


How to get current path in batch file. Here some example shows the different ways:

@echo off
setlocal enableextensions enabledelayedexpansion

set currDir1=%~dp0
set currDir2=%CD%

echo %currDir1%
echo %currDir2%

pause

And the output:

C:\Windows\System32>D:\Run\1.bat
D:\Run\
C:\Windows\System32
Press any key to continue . . .

So, as you can see, method with %~dp0 is more preferable if you run from anywhere but script placed.