use File::Compare;
# Function Name : compareFile
# Input : Two files to compare.
# Output : Pass or Fail.
# Description : This function will compare two given files. If both files are same then it will return Pass
# else it will return Fail.
#
sub compareFile($$)
{
my ($file, $exp_file) = @_;
$file = convertSlash($file);
$exp_file = convertSlash($exp_file);
my $result = "";
if (compare($file, $exp_file) == 0)
{
$result = "Pass";
}
else
{
$result = "Fail";
}
return $result;
}
In this blog, I write about useful technical stuffs like perl's ready to use subroutines, Agile Methodology, etc.
Monday, July 18, 2011
Convert unix path to windows path by converting frontslash to backslash
# Function Name : windowsSlash
# Input : String with or without slash
# Output : Path with double backslash
# Description : This function will convert all forward slash (/) to double backward slash(\\). Also,
# extra slashes will be removed.
#
sub windowsSlash($)
{
my ($str) = @_;
$str =~ s/\/\//\//g;
$str =~ s/\//\\\\/g;
return $str;
}
# Input : String with or without slash
# Output : Path with double backslash
# Description : This function will convert all forward slash (/) to double backward slash(\\). Also,
# extra slashes will be removed.
#
sub windowsSlash($)
{
my ($str) = @_;
$str =~ s/\/\//\//g;
$str =~ s/\//\\\\/g;
return $str;
}
Convert windows path to unix path by converting backslash to frontslash
# Function Name : convertSlash
# Input : String with or without backslash
# Output : String without backslash
# Description : This function will convert all backslash (\) to front slash(/). Also, extr slashes
# will be removed. For Ex: Suppose given string is
# C://rinkesh//bansal//abc.pl then it will be converted to C:/rinkesh/bansal/abc.pl
#
sub convertSlash($)
{
my $str = $_;
$str =~ s/\\/\//g;
$str =~ s/\/\//\//g;
return $str;
}
# Input : String with or without backslash
# Output : String without backslash
# Description : This function will convert all backslash (\) to front slash(/). Also, extr slashes
# will be removed. For Ex: Suppose given string is
# C://rinkesh//bansal//abc.pl then it will be converted to C:/rinkesh/bansal/abc.pl
#
sub convertSlash($)
{
my $str = $_;
$str =~ s/\\/\//g;
$str =~ s/\/\//\//g;
return $str;
}
Renaming a file
use File::Copy;
# Function Name : renameFile
# Input : Name of the Source file and destination file name
# Output : None
# Description : Renames source file to destination file name
# See my other blogs for FileExists function
sub renameFile()
{
my ($source, $destination) = @_;
FileExists($source);
rename($source,$destination);
}
# Function Name : renameFile
# Input : Name of the Source file and destination file name
# Output : None
# Description : Renames source file to destination file name
# See my other blogs for FileExists function
sub renameFile()
{
my ($source, $destination) = @_;
FileExists($source);
rename($source,$destination);
}
Check if File Exists
# Function Name : FileExists
# Input : Filename with path
# Output : None
# Description : Check if file exists
sub FileExists($)
{
my $filename = $_;
unless (-e $filename)
{
die "Error Could not find $filename\n";
}
}
# Input : Filename with path
# Output : None
# Description : Check if file exists
sub FileExists($)
{
my $filename = $_;
unless (-e $filename)
{
die "Error Could not find $filename\n";
}
}
Subscribe to:
Posts (Atom)