Perl not writing to a file in Windows 10 - windows

I have a Perl script written on Server 2008. It works fine here.
I have copied it to my laptop, which is running Windows 10 Home Edition, Version 1993, OS Build 18362.959.
I also download Active Perl, for the very first time.
The script basically takes an input file and applies regular expressions to the content and then outputs the results to a file.
On Windows 10 it is not writing to the file, the file is not even created.
I have done a search on this issue but have not found a solution.
I tried the following code, found on one on the reply to the same issue. But it does not create or writes to the file. It works fine on server 2008. Am I missing something?
As mentioned this is the first time I downloaded ActivePerl version
Perl Version Details are as follows:
This is perl 5, version 28, subversion 1 (v5.28.1) built for MSWin32-x64-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2018, Larry Wall
Binary build 0000 [58a1981e] provided by ActiveState http://www.ActiveState.com
Built Apr 10 2020 17:28:14
Perl code is as follows:
use strict;
use IO::File;
my $FileHandle = new IO::File;
$FileName = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\ShoppingList.txt";
open ($FileHandle, "<$FileName") || print "Cannot open $FileName\n";
local $/;
my $FileContents = <$FileHandle>;
close($FileHandle);
$FileContents =~ s/(Add|Bad|Limit|Each).*\n|Add$|\nWeight\n\d{1,}\nea|\$\d{1,}\.\d\d\/100g\n//g;
Do more Regular expressions.
$FileContents =~ s/(.*)\n(.*)\n(\$\d{1,}\.\d\d)/$1,$3,$2/g;
printf $FileContents;
Above code works. Code below does not create or write to file.
$OutFile = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\test.txt";
$FileHandle = new IO::File;
open ($FileHandle, ">$OutFile") || print "Cannot open $OutFile\n";
printf $FileHandle $FileContents;
close($FileHandle);

Always use use strict; use warnings;.
my $OutFile = "C:\Users\moons\Documents\Personal Planning\Shopping\test.txt";
results in
Unrecognized escape \m passed through at a.pl line 3.
Unrecognized escape \D passed through at a.pl line 3.
Unrecognized escape \P passed through at a.pl line 3.
Unrecognized escape \S passed through at a.pl line 3.
You could use
my $OutFile = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\test.txt";
Whole thing:
use strict;
use warnings;
my $in_qfn = "C:\\Users\\moons\\Documents\\Personal Planning\\Shoppin\\ShoppingList.txt";
my $out_qfn = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\test.txt";
open(my $in_fh, '<', $in_qfn)
or die("Can't open \"$in_qfn\": $!\n");
open(my $out_fh, '>', $out_qfn)
or die("Can't create \"$out_qfn\": $!\n");
my $file;
{
local $/;
$file = <$in_fh>;
}
for ($file) {
s/(Add|Bad|Limit|Each).*\n|Add$|\nWeight\n\d{1,}\nea|\$\d{1,}\.\d\d\/100g\n//g;
s/(.*)\n(.*)\n(\$\d{1,}\.\d\d)/$1,$3,$2/g;
}
print $file;

Related

Execute Windows command with spaces in Perl

My firefox is installed in C:\Program Files\Mozilla Firefox.
I amm trying to read the firefox version like this:
open (IN, "\"C:\\Program\ Files\\Mozilla\ Firefox\\firefox.exe\" --version|") or die "Couldn't fork - $!\n";
my $aarray = (IN);
print $fh "=========Array Content========> $aarray <====\n";
close(IN);
This is working perfectly. But, when I put path or command in a variable and try to execute, it fails saying "Couldn't fork"
my $ffox = 'C:\Program Files\Mozilla Firefox\firefox.exe';
$ffox =~ s/\\/\\\\/g; #Replacing single \ with double \\
$ffox =~ s/\ /\\ /g; # Adding escape character before space
chop($firefoxVer); # remove last \n chr.
$ffox =~ s/$/\\\"/g; # With the below two commands, massaging the command to become
$ffox =~ s/^/\\\"/g; # "C:\\Program\ Files\\Mozilla\Firefox\\firefox.exe\"
$firefoxVer = $ffox.' --version|'; # Adding "version|" to the above command
$firefoxVer =~ s/$/\"/g; # and make it look like:
$firefoxVer =~ s/^/\"/g; # "\"C:\\Program\ Files\\Mozilla\\Firefox\\firefox.exe\" --version|"
open (IN, $firefoxVer) or die "Couldn't fork - $!\n"; ==> Fails
With the backticks you can get the result directly without the open. Quote the command-line things that need to preserve their spaces:
use v5.10;
my $version = `"C:\\Program Files\\Mozilla Firefox\\firefox.exe" -version`;
say "version is $version";
If that directory is in your path, you don't need to fool around with that:
my $firefox = 'C:\\Program Files\\Mozilla Firefox';
$ENV{PATH} .= ";$firefox";
my $version = `firefox -version`;

How to move files from a server to the computer using perl

I want to move files from a server that my Windows computer is connected to, to the actual computer. I have tried the code on my mac and it works fine, so I suspect the problem has to do with the fact that the files I wish to move are on a server or perhaps with Windows (I am unfamiliar with this OS). It is important to me to be able to use File::Find::Rule because there are many subdirectories within subdirectories that need to be searched.
use strict;
use warnings;
use File::Find::Rule;
use File::Copy;
# directory where files live
# my $dir = "\\172.18\user\folder\folder2";
# directory where TextGrids will be moved to
my $outdir = "\users\lisa\desktop\test";
my #files;
#files = File::Find::Rule -> file()
-> name("*_clean.TextGrid")
-> maxdepth()
-> in($dir);
foreach my $file (#files) {
$file =~ /(.*\\)(.*)/;
my $name = $2;
copy("$file", "$outdir/$name") or die "Copy failed: $!";
}
Edit: Ok, I've made some changes to the script below. But the strange thing is, that when I ask it to print each file, it gives me something like \\172.18\user\folder\folder/255/file.txt. I changed the regex to be (.*\/)(.*) and now the script works perfectly, though I don't know why!
use strict;
use warnings;
use File::Find::Rule;
use File::Copy;
# directory where files live
my $dir = "\\\\172.18\\user\\folder\\folder2";
# directory where TextGrids will be moved to
my $outdir = "C:\\Users\\lisa\\desktop\\test";
my #files;
#files = File::Find::Rule -> file()
-> name("*_clean.TextGrid")
-> maxdepth()
-> in($dir);
foreach my $file (#files) {
print "$file\n";
$file =~ /(.*\\)(.*)/;
my $name = $2;
copy("$file", "$outdir\\$name") or die "Copy failed: $!";
}
After your edit, the script works because the last directory separator in the string happens to be /, which is matched by the \/ in the regular expression. Even though you had \ in the input, the library you used to find the files added /s.
I have some suggestions:
You can avoid the need to escape (most) backslashes by using single quoted strings, unless you need the interpolation of the double quoted ones.
Escaping backslashes is optional unless followed by a single quote or another backslash:
my $outdir = '\users\lisa\desktop\test';
but
my $outdir = '\users\lisa\desktop\test\\';
$outdir = '\users\lisa\desktop\test\\\'ere is a path';
my $not_a_path = 'three backslashes\\\\\in between, all but the last need escaping';
'ere is a path is the last element in that path.
If you're dealing with Windows, consider using [\\/] in place of directory separator in regular expressions. (Or [\\\/] if you absolutely must use / as regular expression delimiter.)
Even if you have control over user input to only use \ in paths, libraries you use will usually add /, so it's better to be prepared for a combination of both.
$file =~ /(.*[\\\/])(.*)/;
$file =~ m{(.*[\\/])(.*)};
$file =~ m¤(.*[\\/])(.*)¤;
I also removed the superfluous quotes from around $file in the copy() call. Final result:
use strict;
use warnings;
use File::Find::Rule;
use File::Copy;
# directory where files live
my $dir = '\\172.18\user\folder\folder2';
# directory where TextGrids will be moved to
my $outdir = 'C:\Users\lisa\desktop\test';
my #files;
#files = File::Find::Rule -> file()
-> name("*_clean.TextGrid")
-> maxdepth()
-> in($dir);
foreach my $file (#files) {
print "$file\n";
$file =~ /(.*[\\\/])(.*)/;
my $name = $2;
copy($file, "$outdir\\$name") or die "Copy failed: $!";
}
use strict;
use warnings;
my $dir = "\\172.18\user\folder\folder2";
print("$dir\n");
my $outdir = "\users\lisa\desktop\test";
print("$outdir\n");
outputs
Unrecognized escape \d passed through at a.pl line 7.
\172.18Ser?older?older2
Sersisadesktop est
You need to escape your backslashes!
use strict;
use warnings;
my $dir = "\\\\172.18\\user\\folder\\folder2";
print("$dir\n");
my $outdir = "\\users\\lisa\\desktop\\test";
print("$outdir\n");
Don't ignore warnings.

Need to split multiple files in a directory based on string, rename properly using powershell or fix my perl script

I have a directory full of files (text exports of Dynamics NAV objects that have been exported) in Windows. Each file contains multiple objects. I need to split each file into separate files based on lines that begin with OBJECT, and name each file appropriately.
The purpose of this is to get our Dynamics NAV system into git.
I wrote a nifty perl program to do this that works great on linux. But it hangs on the while(<>) loop in Windows (Server 2012 if that matters).
So, I need to either figure out how to do this in the PowerShell script that I wrote that generates all of the files, or fix my perl script that I'm calling from PowerShell. Does Windows perl handle filehandles differently than linux?
Here's my code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Path qw(make_path remove_tree);
use POSIX qw(strftime);
my $username = getlogin || getpwuid($<);
my $datestamp = strftime("%Y%m%d-%H%M%S", localtime);
my $work_dir = "/temp/nav_export";
my $objects_dir = "$work_dir/$username/objects";
my $export_dir = "$work_dir/$username/$datestamp";
print "Objects being exported to $export_dir\n";
make_path("$export_dir/Page", "$export_dir/Codeunit", "$export_dir/MenuSuite", "$export_dir/Query", "$export_dir/Report", "$export_dir/Table", "$export_dir/XMLport");
chdir $objects_dir or die "Could not change to $objects_dir: $!";
# delete empty files
foreach(glob('*.*')) {
unlink if -f and !-s _;
}
my #files = <*>;
my $count = #files;
print "Processing $count files\n";
open (my $fh, ">-") or die "Could not open standard out: $!";
# OBJECT Codeunit 1 ApplicationManagement
while(<>)
{
if (m/^OBJECT ([A-Za-z]+) ([0-9]+) (.*)/o)
{
my $objectType = $1;
my $objectID = $2;
my $objectName = my $firstLine = $3;
$objectName =~ s/[\. \/\(\)\\]/_/g; # translate spaces, (, ), ., \ and / to underscores
$objectName =~ tr/\cM//d; # get rid of Ctrl-M
my $filename = $export_dir . "/" . $objectType . "/" . $objectType . "~" . $objectID . "~" . $objectName;
close $fh and open($fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "OBJECT $objectType $objectID $firstLine\n";
next;
}
print $fh $_;
}
I've learned quite a bit of PowerShell in the past few days. There are some things that it really does quite well. And some (such as calling an executable with variables and command line options that have spaces) that are maddeningly difficult to figure out. To call curl, this is what I resorted to:
$curl = "C:\Program Files (x86)\cURL\bin\curl"
$arg10 = '-s'
$arg1 = '-X'
$arg11 = 'post'
$arg2 = '-H'
$arg22 = '"Accept-Encoding: gzip,deflate"'
$arg3 = '-H'
$arg33 = '"Content-Type: text/xml;charset=UTF-8"'
$arg4 = '-H'
$arg44 = '"SOAPAction:urn:microsoft-dynamics-schemas/page/permissionrange:ReadMultiple"'
$arg5 = '--ntlm'
$arg6 = '-u'
$arg66 = 'username:password'
$arg7 = '-d'
$arg77 = '"#soap_envelope.txt"'
$arg8 = "http://$servicetier.corp.company.net:7047/$database/WS/DBDOC/Page/PermissionRange"
$arg9 = "-o"
$arg99 = "c:\temp\nav_export\$env:username\raw_list.xml"
&"$curl" $arg10 $arg1 $arg11 $arg2 $arg22 $arg3 $arg33 $arg4 $arg44 $arg5 $arg6 $arg66 $arg7 $arg77 $arg8 $arg9 $arg99
I realize that part is a bit of a tangent. But I've been working really hard at trying to figure this out and not have to bother you nice folk here at stackoverflow!
I'm ambivalent about making it work in PowerShell or fixing the Perl code at this point. I just need to make it work. But I'm hoping it's just some little difference in filehandle handling between linux and Windows.
It's hard to believe that the Perl code that you show does anything on Linux either. It looks like your while loop is supposed to be reading through all of the files in the #files array, but to make it do that you have to copy the names to #ARGV.
Also note that #files will contain directories as well as files.
I suggest you change the lines starting with my #files = <*> to this. There's no reason why it shouldn't work on both Windows and Linux.
our #ARGV = grep -f, glob '*';
my $count = #ARGV;
print "Processing $count files\n";
my $fh;
while (<>) {
s/\s+\z//; # Remove trailing whitespace (including CR and LF)
my #fields = split ' ', $_, 4;
if ( #fields == 4 and $fields[0] eq 'OBJECT' ) {
my ($object_type, $object_id, $object_name) = #fields[1,2,3];
$object_name =~ tr{ ().\\/}{_}; # translate spaces, (, ), ., \ and / to underscores
my $filename = "$export_dir/$object_type/$object_type~$object_id~$object_name";
open $fh, '>', $filename or die "Could not open file '$filename': $!";
}
print $fh "$_\n" if $fh;
if (eof) {
close $fh;
$fh = undef;
}
}

Why Perl in Windows can't rename file with umlaut?

There is following list of files in directory:
01 Born - Praised - Kissed.flac
02 Wunschkind.flac
03 You've got it.flac
04 Down in this Hole.flac
05 Wälsungenblut.flac
...
N. 0N Filename
#Yes, these are the songs of Oomph!
and following program on Perl:
use warnings;
use strict;
use utf8;
use open qw( :encoding(UTF-8) :std );
my #dirnames;
while ( (my $dirname = <>) =~ /\S/ ) {
chomp($dirname);
push (#dirnames, $dirname);
}
foreach my $dirname (#dirnames) {
opendir (DIR, $dirname);
while ( my $file = readdir(DIR) ) {
if(length($file)>5) {
print $file , "\n";
my $newfile;
$newfile = substr($file, 0, 2);
$newfile .= '.';
$newfile .= substr($file, 2);
rename ($dirname . '\\' . $file, $dirname . '\\' . $newfile) or die $!;
}
}
closedir DIR;
}
that gets the list of directories and renames the files in them by adding dot after number.
Program works correctly on all files, but when it try to rename file with umlaut in the filename, both of the Windows PowerShell and Command Line throw the error that Permission denied at the string with rename function.
How to solve this problem, guys?
UPD. Software:
Windows 8 x64
ActiveState ActivePerl 1601 (Perl 5.16)
Perl's readdir uses a legacy interface ("ANSI") since it can only handle file names consisting of bytes due to its unix heritage.
The "ANSI" interface uses a single-byte character encoding known as a code page. Your system's code page is 1251, and it doesn't provide a means of encoding "ä", so file names containing "ä" cannot be returned by readdir.
You need to avoid this "ANSI" interface (FindFirstFileA) and gain access to FindFirstFileW. This will provide the file name in UTF-16le, which you can pass to Win32API::File's MoveFileExW. Win32::Unicode::Dir's open+fetch does just that.
It's a dismal state of affairs. I've been meaning to address it, but it would be an extensive project.
use utf8;
use Win32 qw( );
BEGIN {
binmode(STDOUT, ':encoding(cp'.Win32::GetConsoleOutputCP().')');
binmode(STDERR, ':encoding(cp'.Win32::GetConsoleOutputCP().')');
}
use strict;
use warnings;
use feature qw( say );
use open ':encoding(UTF-8)';
use Encode qw( encode );
use Win32::Unicode::Dir qw( mvtreeW );
use Win32API::File qw( MoveFileExW );
my $dir_qfn = '.';
my $wdir = Win32::Unicode::Dir->new();
$wdir->open($dir_qfn)
or die("Can't open $dir_qfn: ".$wdir->error());
for ($wdir->fetch()) {
next if /^\.\.?\z/;
next if length() <= 5;
say;
my $o_fn = $_;
s/^..\K/./s;
my $n_fn = $_;
MoveFileExW(
encode('UTF-16le', "$dir_qfn/$o_fn\0"),
encode('UTF-16le', "$dir_qfn/$n_fn\0"),
0, # or MOVEFILE_REPLACE_EXISTING
)
or die("Can't rename $o_fn to $n_fn: $^E\n");
}
$wdir->close();
You are reading the directory as if it was in UTF-8, but you are really in Windows-1252 encoding. Lose the use open qw(...) and it should work.
Do you have access to "rename.pl"? can you do
perl rename.pl "s/^(\d\d)/$1./" *flac
?

Perl - Using Variables from an Input File in the URL when a Variable has a Space (two words)

What am I doing? The script loads a string from a .txt (locations.txt), and separates it into 6 variables. Each variable is separated by a comma. Then I go to a website, whose address depends on these 6 values.
What is the problem? If there is a space as a character in a variable as part of a string in locations.txt. When there is a space, it does not get the correct url.
The input file is:
locations.txt = Heinz,Weber,Sierra Leone,1915,M,White
Because Sierra Leone has a space, the url is:
https://familysearch.org/search/collection/results#count=20&query=%2Bgivenname%3AHeinz%20%2Bsurname%3AWeber%20%2Bbirth_place%3A%22Sierra%20Leone%22%20%2Bbirth_year%3A1914-1918~%20%2Bgender%3AM%20%2Brace%3AWhite&collection_id=2000219
But that does not get processed correctly in the code below.
I'm using the packages:
use strict;
use warnings;
use WWW::Mechanize::Firefox;
use HTML::TableExtract;
use Data::Dumper;
use LWP::UserAgent;
use JSON;
use CGI qw/escape/;
use HTML::DOM;
This is the beginning of the code :
open(my $l, 'locations26.txt') or die "Can't open locations: $!";
open(my $o, '>', 'out2.txt') or die "Can't open output file: $!";
while (my $line = <$l>) {
chomp $line;
my %args;
#args{qw/givenname surname birth_place birth_year gender race/} = split /,/, $line;
$args{birth_year} = ($args{birth_year} - 2) . '-' . ($args{birth_year} + 2);
my $mech = WWW::Mechanize::Firefox->new(create => 1, activate => 1);
$mech->get("https://familysearch.org/search/collection/results#count=20&query=%2Bgivenname%3A".$args{givenname}."%20%2Bsurname%3A".$args{surname}."%20%2Bbirth_place%3A".$args{birth_place}."%20%2Bbirth_year%3A".$args{birth_year}."~%20%2Bgender%3AM%20%2Brace%3AWhite&collection_id=2000219");
# REST OF THE SCRIPT HERE. MANY LINES.
}
As another example, the following would work:
locations.txt = Benjamin,Schuvlein,Germany,1913,M,White
I have not used Mechanize, so not sure whether you need to encode the URL. Try encoding space to %20 or + before running $mech->get
$url =~ s/ /+/g;
Or
$url =~ s/ /%20/g
whichever works :)
====
Edit:
my $url = "https://familysearch.org/search/collection/results#count=20& query=%2Bgivenname%3A".$args{givenname}."%20%2Bsurname%3A".$args{surname}."%20%2Bbirth_place%3A".$args{birth_place}."%20%2Bbirth_year%3A".$args{birth_year}."~%20%2Bgender%3AM%20%2Brace%3AWhite&collection_id=2000219";
$url =~ s/ /+/g;
$mech->get($url);
Try that.
If you have the error
Global symbol "$url" requires explicit package name.
this means that you forgot to declare $url with :
my $url;
Your use part seems freaky, I'm pretty sure that you don't need all of those modules # the same time. If you use WWW::Mechanize, no need LWP::UserAgent and CGI I guess...

Resources