Monday, October 13, 2014

Create predefined folder & sub-folders on Windows.

@echo off

set PWD=%cd%
set dlist=CLASS01 CLASS02 CLASS03
set slist=PHY CHE BIO GEO MATH

for %%i in ( %dlist% ) do (
   if not exist %PWD%\%%i ( mkdir %PWD%\%%i )
   for %%j in ( %slist% ) do (
       if not exist %PWD%\%%i\%%j ( mkdir %PWD%\%%i\%%j )
   )
)

Rename multi files in a folder on Windows.

@echo off

set count=1
set basedir=C:\old_data\report
for /f "tokens=*" %%G in ('dir /a-d /b %basedir%') do (call :subroutine %%G)
goto :eof

:subroutine
for /F "delims=_" %%a in ("%1") do (
   ren "%basedir%\%1" "%%a_SPRW_Light_%count%.txt"
)
set /a count+=1
goto :eof

Example:
"20141013_Test_Rept.dat" will be renamed to "20141013_SPRW_Light_1.txt"

Write colorful text on console in Perl on Windows.

use Win32::Console;

# get current console attributes
my $console = Win32::Console->new(STD_OUTPUT_HANDLE);
my $cons_attrib = $console->Attr();

$console->Attr($FG_LIGHTGREEN | $BG_BLACK);
print "Hi, Welcome to Arabian Nights";
$console->Attr($cons_attrib); #Restore original attributes