How to give current directory as Path name while reading an Excel file? - perl

I am trying to update an already existing Excel file "Test.xls" Now, instead of giving the complete path of the Test.xls file, I wish to know how to read the Test.xls file from current directory. I am using Eclipse in Windows environment. Below is my code:
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $Book = $Excel->Workbooks->Open("Test.xls"); #This line throws an error but works if I give complete path name as D:/eclipse/workspace/testing/Test.xls
my $Sheet = $Book->Worksheets(1);
foreach my $data (#ifrules)
{
$Sheet->Cells($row,$col)->{'Value'} =$data;
$row++;
}
$Book->Close;
I have tried various options like ./Test.xls, .\Test.xls and others too.

From what I found, you can use getcwd() to get the current working directory. You can then concatenate that with the name of the file within $Excel->Workbooks->Open() to give the current directory plus the name of the file.
Source Link
Explanation: The basic issue here is that you are passing the file name to Excel, which doesn't know about Perl's current working directory. That is why you have to find out the full path name and give it to $Excel->Workbooks->Open().

Related

Powershell ps2exe config variable

I would like To store a variable in a config file for a .ps1(powershell script) converted to .exe using ps2exe
$LnkPath="...\" #Directory Path of .lnk
$LnkFile="\nnnn.lnk" #name of .lnk file
Invoke-Item $ScriptPath + $LnkFile
I was hoping to have $LnkFile and $LnkPath as config file variables so if the version of the lnk stops working, i can just point to a new lnk.
There is a reason why the version of the .lnk file stops working, but it is complicated, and not worth anyone's time.
edit:
The config file created with the optional -configFile switch isn't meant for use by the wrapped script - it merely contains runtime metadata for the generated executable (in the form of an XML file placed alongside the executable with additional extension .config).
However, you can create your own config file.
While PowerShell has a configuration-data format that uses hashtable-literal syntax, which can be read with Import-PowerShellDataFile, as of PowerShell 7.2.x there is no way to create this format programmatically.
A simple alternative that supports both reading and programmatic creation is to use a JSON file:
The following assumes that your script file is foo.ps1, to be converted to foo.exe, with a configuration file foo.json located in the same directory (which you'll have to bundle with your .exe file when deploying it):
First, create your JSON config file:
#{ LnkPath = '...\'; LnkFile = 'nnnn.lnk' } | ConvertTo-Json > foo.json
Now you can read this file from foo.ps1 / foo.exe as follows:
# Determine this script's / executable's full path.
$scriptOrExePath =
if ($PSCommandPath) { # Running as .ps1
$PSCommandPath
} else { # Running as .exe"
Convert-Path ([Environment]::GetCommandLineArgs()[0])
}
# Look for the JSON config file in the same directory as this script / executable, load it and parse it into an object.
$config =
Get-Content -Raw ([IO.Path]::ChangeExtension($scriptOrExePath, '.json')) |
ConvertFrom-Json
# $config is now an object with .LnkPath and .LnkFile properties.
$config # Output for diagnostic purposes.
Note the need to use [Environment]::GetCommandLineArgs() to determine the executable path when running as an .exe file, because the usual automatic variables indicating the script path ($PSCommandPath) and script directory ($PSScriptRoot) aren't available then.

Perl Net::FTP not lists particular file

I am trying to list the file of remote host using Net::FTP Perl module. Later I want to download it using put command.
Will it possible to list the particular file using ls command
Syntax I am using is:
$remote_dir = "/home/user/test_dir/";
$ftp->cwd($remote_dir);
$file_name = "test.txt";
print $ftp->ls($file_name);
This file is exists in remote server when I tried checking it manually. But listing is not happening.
ftp->ls(); is used for only listing the directory contents or can it be used to list the particular file from a directory as I mentioned above?
ftp->ls(); is used for only listing the directory contents or can it be used to list the particular file from a directory
From the documentation:
ls ( [ DIR ] )
Get a directory listing of DIR, or the current directory.
Thus, it is clearly documented to list a directory, not a specific regular file.

How to add user created .mat files to the search path after compilation

How can I allow user to add additional dependenies after the source code was compiled with mcc.
I was thinking about an empty folder next to the executable, where the users can add the needed .mat-files, but I can't add the folder path to my executable (since addpath is not allowed in deployed applications).
Any ideas?
This answer assumes that your code can be customised by data contained in one or more .mat files at runtime.
You can point your code to look at a folder where the optional .mat file(s) would be located.
For example in the users home folder with a sub folder being the name of your application (or in the local app data) or whereever...
If you want it in a sub folder where the exe is, you can do this as well, you find the exe path using (on windows):
[status, result] = system('path');
installpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
fprintf ( 'The exe install path is "%s"\n', installpath );
Then your code looks to load for example:
file2load = fullfile ( installpath, 'subFolder', 'runtimeCustomisation.mat' )
if exist ( file2load, 'file' ) == 2
"doSomething with the file"
end
Or something like that.
Recall that this is for dependencies which are .mat files only.

Rename the dir once update is finished - perl

Platform - Windows7
I am looking for a script that will change the name of the latest modified directory :
Here is how I am getting the latest modified directory :
my $epoch_timestamp = (stat("logdir"))[9];
#date=localtime($epoch_timestamp);
$date[5] +=1900;
$date[4] +=1;
print "$date[5]-$date[4]-$date[3] $date[2]:$date[1]:$date[0]\n";
exp : c:/dir_name/renamedir
I have a folder c:/dir_name; inside that I am copying one folder after another. I need to change the name of the folder which has been copied completely.
c:/dir_name/renamedir1
c:/dir_name/renamedir2
The issue I am facing :
I need to change the name once update is finished, i.e. after all the files have been copied in the dir.
So can anybody let me know how come we can be sure that all the files got copied in the folder?
Could you please try this way:
use File::Copy;
my $origDir = 'C:\dir_name\renamedir1\file1.txt';
my $desDir = 'C:\dir_name\renamedir2\file1.txt';
#Check whether the files copied or not
copy($origDir, $desDir) || die "Couldn't able to copy the files: $!";
On the Other way try to compare the folders/subfolder/files. And get reference from the below link:
How to compare two directories and their files in perl

Perl script to Hyperlink to open a file based on the name of the file in each cell

I had developed a perl script to look for matching strings from text files and copied it into a spread sheet, I had also included the text file name into the spreadsheet for corresponding matching string. Now I want to hyperlink to open the file by clicking the file name in each cell.
Any help would be much appreciated! I'm not much of a programmer... Thanks!
Here is some examples by using the Spreadsheet::WriteExcel
# External link to a local file
$worksheet->write('B2', 'external:Italy.xls');
# External link to a local file with worksheet
$worksheet->write('B3', 'external:Italy.xls#Sales!B3');
# External link to a local file with worksheet and alternative string
$worksheet->write('B4', 'external:Italy.xls#Sales!B4', 'Link');
# External link to a local file with worksheet and format
$worksheet->write('B5', 'external:Italy.xls#Sales!B5', $format);
# External link to a remote file, absolute path
$worksheet->write('B6', 'external:c:/Temp/Asia/China.xls');
# External link to a remote file, relative path
$worksheet->write('B7', 'external:../Asia/China.xls');
# External link to a remote file with worksheet
$worksheet->write('B8', 'external:c:/Temp/Asia/China.xls#Sales!B8');
# External link to a remote file with worksheet (with spaces in the name)
$worksheet->write('B9', q{external:c:/Temp/Asia/China.xls#'Product Data'!B9});
You can see the write API for more information.