Saturday, March 29, 2014

How do you connect to remote desktop automatically via batch file?

@echo off
set domain=<DOMAIN NAME>
set password=<PASSWORD>
set server=<SERVER NAME or IP>
set user=<USER NAME>
cmdkey /generic:TERMSRV/%server% /user:%domain%\%user% /pass:%password%
start mstsc /v:%server% /f
exit /b

Monday, March 24, 2014

How can you delete files older than 7 days?

my $log_dir = "C:\perlwork\temp";
foreach my $file ( grep {-M > 7} glob("$log_dir/*.log") ) {
        unlink $file;
}

Thursday, March 13, 2014

How do I list the specific types of files in current directory in Perl on windows?

use Cwd;

my $directory = getcwd();

opendir (DIR, $directory) or die "Can't open directory \"$directory\" - $!";
my @filelist = grep(/\.csv$/i, readdir(DIR));;

foreach my $file (@filelist) {
print "$file\n";
}

closedir(DIR);

How can you store contents of config file into a hash-of-array?

my %conf = ();
open (INI, "<config.ini") || die "Can not open - $!" ;
while (my $line = <INI>) {
if (($line=~/^#/) || ($line=~/^;/)) {
#ignore these lines coz they are comments!
}
else {
if($line =~m/^(.*?)=(.*?)$/) {
push @{$conf{NAME}}, $1;
push @{$conf{SIZE}}, $2;
}
}
}
close INI;

# To access each array in hash-of-array
foreach my $key ( keys %conf )  {
    print "$key = @{$conf{$key}}\n";
}

# To access each element in hash-of-array
foreach my $key ( keys %conf )  {
    foreach ( @{$conf{$key}} )  {
        print "$key = $_\n";
    }
}

# To access single element of particular array in hash-of-array
print "${$conf{NAME}}[0]\n";

#config.ini :
# ';' or '#' can be used to comment any pair!
;[ATTRIBUTE=WIDTH]
First Name=15
Last Name=20
Middle Name=10
Member ID=20
SSN=9