Thursday, September 18, 2014

How do you get timestamp in batch file?

@echo off
set timestamp=%date:~-4%-%date:~4,2%-%date:~7,2%:%time:~0,2%-%time:~3,2%-%time:~6,2%
echo %timestamp%

Friday, August 22, 2014

How do you execute PostgreSQL scripts whose names start with 'p_'?

my $path = 'C:\TestSQL';

my @scripts = `dir /B /s $path`;

foreach my $psqlfile (@scripts) {
   if ( $psqlfile =~ /^(.+\\p\_[^\\]+\.sql)\s*$/i ) {
        `psql -q -h <Hostname> -p <Port> -d <Database> -U <Username> -f $psqlfile.sql -o outputfile`;
   }
}

How do you execute all sql files in a directory?

my $path = 'C:\TestSQL';

my @scripts = `dir /B /s $path`;

foreach my $sqlfile (@scripts) {
   if ( $sqlfile =~ /^(.+\.sql)\s*$/i ) {
       `mysql <db_name> -u <user_name> -p <password> < $sqlfile.sql`;
   }
}

Wednesday, August 20, 2014

How do you add path of perl to environment variable on command prompt?

1. Open "cmd.exe" with Administrator Privilege (on right click on app) and type the command below.
     setx path "%path%;C:\Perl64\bin;"
2. Restart "cmd.exe".

Wednesday, July 30, 2014

Monday, July 7, 2014

Move N number of files from one directory to other.

@echo off

set Source=C:\PerlDrift_Bat\IN
set Target=C:\Perl\Drift_Bat\OUT

set MaxLimit=20

for /f "tokens=1* delims=[]" %%G in ('dir /A-D /B "%Source%\*.*" ^| find /v /n ""') do (
move /y "%Source%\%%~nxH" "%Target%"
if %%G==%MaxLimit% exit /b 0
)

Sunday, June 8, 2014

How do you count number of lines in a file?

my $msg;

open(IN, "<", "$0") or die "Can't open $0 - $!";
while (<IN>) {
$msg = $msg . $_;
}
close IN;

$count = () = $msg =~ /\n/g;
print "Line Count - $count\n";

#or

$lines = () = $msg =~ m/\n/g;
print "Line Count - $lines\n";

How do you replace either of the words with a new word?

if ($line =~ m/O(RC|BR)/) {
$line =~ s/$&/\n\n$&/ig;
}

will replace either ORC or OBR with "\n\nORC" or "\n\nOBR".

Friday, May 16, 2014

How can you detect comment or blank line in a file?

open (FH, "$File") || die "Can't open $File - $!\n";
while (<FH>) {
    chomp;
    if ($_ =~ /^(#|;|$)/) {
        print "Found Blank-Line or Commented-Line @ line - #$.\n";
    }
}
close (FH);