How can I call winrar in perl on windows - windows

Is it possible to call winrar through perl on a windows system, such as
perl -e "rar a -rr10 -s c:\backups\backup.rar #backup.lst"
If so, is there a more efficient way to do this?
I've looked up "perl -e" +winrar on google, however none of the results gave me any answer that was remotely close to what i was looking for. The system Im running this on is a Windows XP system. Im open to doing this in another language like python if its easier, however I am more comfertable with perl.

You can access the RAR facilities in Windows using the CPAN module Archive::Rar:
use Archive::Rar;
my $rar = Archive::Rar->new(-archive => $archive_filename);
$rar->Extract();

One way to execute external commands from a Perl script is to use system:
my $cmd = 'rar a -rr10 -s c:\backups\backup.rar #backup.lst';
if (system $cmd) {
print "Error: $? for command $cmd"
}

To use external applications from your Perl program, use the system builtin.
If you need the output from the command, you can use the backtick (``) or qx operator as discussed in perlop. You can also use pipes as discussed in perlipc.

Related

Translating OS X Bash Script for Windows

I use Hedge to transfer Magic Lantern video files shot on my Canon 5D Mark III.
On OS X, I'm able to use Automator to set up a bash script, to execute an mlv_dump to transfer the files from MLV into cDNG sequences.
The script I use currently is:
cd "$(dirname "$1")"
for f in "$#"; do
if [[ -f $f ]]; then
filename=${f##*/};
folder=${filename%.*}
mkdir "${folder}";
~/mlv_dump --dng $f -o ${folder}/${folder}_;
fi
done
Can this easily translate into a Windows equivalent?
Thanks,Thomas
As with any translation between programming languages, there's (a) an as-literal-as-possible approach, which contrasts with (b), an not-immediately-obvious-but-in-the-spirit-of-the-target-language approach.
(b) is always preferable in the long run.
Use PowerShell, because it is the - far superior - successor to the "Command Prompt" (cmd.exe) and its batch files.
The code below is an attempt at (b), in PowerShell (v3+ syntax).
I encourage you to study the code and post an explanation of it in an answer of your own, so that others may benefit too.
To help with the analysis, consider the following resources:
The official Windows PowerShell get-started page and the equivalent for PowerShell Core.
This series of articles is a great, recipe-oriented introduction to PowerShell.
http://hyperpolyglot.org/shell juxtaposes the syntax of POSIX-like shells such as bash with that of cmd.exe and PowerShell in concise, tabular form.
PowerShell-idiomatic translation of your code:
param(
[Parameter(Mandatory, ValueFromRemainingArguments)]
[System.IO.FileInfo[]] $LiteralPath
)
$outputBaseFolder = Split-Path -Parent $LiteralPath[0].FullName
foreach ($f in $LiteralPath) {
if ($f.exists) {
$outputFolder = Join-Path $outputBaseFolder $f.BaseName
New-Item -ItemType Directory $outputFolder
& "$HOME/mlv_dump" --dng $f.FullName -o "$outputFolder/$($f.BaseName)_"
} else {
Write-Warning "Item doesn't exist or is not a file: $($f.FullName)"
}
}
Easy, is a relative answer specific to the skills of the individual, As the others have commented, there is no out of the box thing / tool you can use or buy to do this. It's all manual work, using the pointers and others who have tried to see what you can glean to accomplish you use case.
For example, this, in your block...
filename=${f##*/};
folder=${filename%.*}
mkdir "${folder}";
... is easily translated into...
$filename='PathToFileName'
$foldername='PathToFolder'
mkdir 'FolderName'
Now, that translation is really simplistic, and obviously not complete. That is something you'll have to figure out, using the resources pointed to and the PowerShell built-in help file and those examples.
There are several posts on this very forum on this conversion topic and how others have come to a consensus oh what / if anything can be done.
For example the below will highlight the efforts you'll need to traverse to accomplish X or Y.
Convert simple Bash script to PowerShell?
I am looking to port this bash code to PowerShell. Can anyone shed
some PowerShell light on this one?
Convert simple Bash script to PowerShell?
Convert bash script into Windows script
I have the following Unix shell script. I would like to convert this
into a Windows .bat file (I know I can use Cygwin instead of adapting
it to a Windows environment. But Cygwin is not an option for me).
Convert bash script into Windows script
Convert xargs Bash command to PowerShell?
I've got a simple Bash command to resize some images automatically on
a low-traffic website using ImageMagick - I'd like to convert this to
a PowerShell command so I don't have to install Cygwin on my
webserver.
Convert xargs Bash command to PowerShell?
As noted, there are paid for avenues / even online ones who can potentially be an avenue.
But, you should really read up a bit more on how to do specific things, like..
Creating files and folders
Accessing/Reading files, importing files, filtering files
Loops
Operators (comparison and assignment)
Navigating the file system/PSDrive

Perl: Why do I get error "The file name, directory name, or volume label syntax is incorrect."

I am trying to run the below perl code from Windows batch file but getting error The file name, directory name, or volume label syntax is incorrect.
The script ran fine in eclipse.My ultimate goal is to run this perl script periodically using windows task scheduler, hence running it from a batch file.
Is there any other ways with which we can achieve my goal of running perl script on windows periodically?
I want my script to be functional across platforms, coz I have plans to run it from a mac as well.
use strict;
use warnings;
use Data::Dumper;
use File::Find::Rule;
my $basedir="G:\/My_Workspaces";
my #exclude_dirs= qw(.foo);
#Fetching all the workspaces under base dir excluding the ones in #exclude_dirs
my #subdirs =
File::Find::Rule
->mindepth(1)
->maxdepth(1)
->not_name(#exclude_dirs)
->directory
->in($basedir);
#Formating list of workspaces by removing the full path
s{^\Q$basedir\E/}{} for #subdirs;
If that is exactly the contents of your file, then you're asking Windows' command interpreter to process Perl source code, which it can't do
If you really need to create a batch file that has your Perl code embedded in it, then take a look at the pl2bat utility, which will do exactly that
A command like
pl2bat myperl.pl
will create a file myperl.bat that will run on the Windows command line and has your Perl source code embedded inside it. But that file is non-portable because it uses Windows commands that aren't recognised on a Mac or Linux platform
Either something doesn't know how to execute your Perl script, or your Perl script is being interpreted by something other than perl.
This could due to a problem with your file associations (or a lack thereof). Determining the exact cause would require more information.
In any case, executing perl with your script as a parameter rather than executing the script directly should solve the problem.
In other words, execute
perl script.pl
instead of
script.pl

Run external command from within Perl

I am using Doxygen to generate HTML documentation and then run a Perl script to get function names.
To run Doxygen configuration, I need to run doxygen file_name in cmd.
But I want to run everything from Perl.
I tried this code
my $cmd = "perl -w otherscript.pl";
my $result = system("start $cmd");
But it just opens a cmd window. I need to execute cmd code directly through Perl (not a Perl command line, but through a Perl IDE). Is there a way to achieve this?
Your usage of system and start is OK.
From your description in the comment, I think it's because you're not using the correct escaping method when giving configure files to Doxygen that it throws such an error:
Error: configuration file C:SERSGHOSHBCD not found!
Try with
my $result = `doxygen C:\\Users\\aghosh\\abcd`;
In the two back-slashes, the former one is to escape the latter one, so that it's recognized by Windows as the directory separator.

Perl: Get minimum supported operating system for a binary

Is there a Perl command that lets me get the minimum supported OS for any given binary?
You can manually get that information by running "link /dump /headers [binaryFile]" and looking for the "subsystem version" link. I don't want to use that since it's got really bad perf.
Thanks
If you need this for Windows, use get_manifest from Win32::Exe. You will need to install it first.
If there's a command that gets what you want, why not just run that command?
You can use backticks or qx// in Perl to get a command's output
eg:
my $output = `command arg1 arg2 ...`;
Or, if you want an array of lines:
my #lines = `command arg1 arg2 ...`;
Then you can use Perl's normal facilities for scanning that output for patterns you're interested in.
Also, your command looks like it is for Windows - is that true? If so, you should add a Windows tag.

How can I tell if my Perl script is running under Windows?

What is the best way to programatically determine if a Perl script is executing on a Windows based system (Win9x, WinXP, Vista, Win7, etc.)?
Fill in the blanks here:
my $running_under_windows = ... ? 1 : 0;
From perldoc perlvar:
$OSNAME
$^O
The name of the operating system under which this copy of Perl was built, as determined during the configuration process. The value is identical to $Config{'osname'}. See also Config and the -V command-line switch documented in perlrun.
In Windows platforms, $^O is not very helpful: since it is always MSWin32, it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or Win32::GetOSVersion() (see Win32 and perlport) to distinguish between the variants.
$^O eq 'MSWin32'
(Source: The perlvar manpage)
Use Devel::CheckOS. It handles all of the logic and special cases for you. I usually do something like:
use Devel::CheckOS qw(die_unsupported os_is);
die "You need Windows to run this program!" unless os_is('MicrosoftWindows');
The 'MicrosoftWindows' families knows about things such as Cygwin, so if you are on Windows but not at the cmd prompt, os_is() will still give you the right answer.
This is very quick and dirty, and wouldn't bet it's 100% portable, but still useful in a pinch.
Check for presence of back slashes in the PATH Env variable, since PATH is common to both Windows and Unix.
So - in Perl:
if ( $ENV{PATH}=~m{\\} ) {
#Quick and dirty: It's windows!
print "It's Windows!";
} else {
print "It's Unix!";
}

Resources