Tag Archives: batch

ConEmu. Just a great software!


Many times i’ve tried to find free console emulator for windows without bugs. And its occurred!
Thanx to author http://code.google.com/p/conemu-maximus5/

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  
            )  
        )  
    )  

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.