Perl script to check file exists in Linux file system - perl

I am working on production servers.. I want to check file generated daily and send report via email like file generated or not daily.. I like to use perl script and I might run the script on different server so my script should check on other server where file generates. And also it check for file header and trailer. Please guide me how to check in different system and how to generate as mail content depends upon file exists or not conditions I.e if all file exists content something like all file generated and it contain table with filename and status of the file exists . if file not exists file name should be highlighted and content should be like missing file not generated please investigate... Give me some ideas around this.. Thanks
Summaries:
Just check file exists or not with header and trailer check . generate as a mail with table format of filename and status as 1or 0. If not generated highlight those files using perl

You can check if a file exists using
if(-e "filename"){
#file exists
}
else{
#file does not exist
}
And also it check for file header and trailer.
Not sure what you mean with this, but I guess once you verified that the file exists you can just read its content to check it:
open FILE, "filename" or die "Error opening file!\n"
while(my $line=<FILE>){
#process line
}

Related

Run for-loop only if there is at least one json file

I want to iterate over all json files in a specific subdirectory.
#!/bin/sh
source_dir="nodes"
for file_path in $source_dir/*.json;
do
file_name=$(basename $file_path .${file_path##*.})
echo $file_name
done
My code is working as expected if there is at least one json file in the directory.
If there is no json file in the directory, the loop will still be executed. The file_name is then "*".
How do I have to change the for loop so that it is only executed if there is at least one json file in the directory?
you can wrap you loop in an if clause to check if the pattern matches anything.
Check this SO question on how to do this: Check if a file exists with wildcard in shell script

Input argument is a file or an either content to Perl

I wrote a Perl script to convert from TEX format to JSON format.
Calling in the batch file:
perl -w C:\test\support.pl TestingSample.tex
This is working fine now.
Perl script having two types of input from another program (might be any platform/technology) one is file (*TEX) or else content (*TEX file) either this or that option.
How can I receive the full content as the input to the Perl script?
Now my Perl script is:
my $texfile = $ARGV[0]; my $texcnt = "";
readFileinString($texfile, \$texcnt);
I am trying to update:
perl -w C:/test/support.pl --input $texcnt" #Content is Input
I am receiving error message:
The command line is too long.
Could someone please advice?
First of all regarding the error you're getting:
Perl (or your shell) is complaining that your input argument is too long.
Parsing entire files as arguments to scripts is generally a bad idea anyway, for example quotation mark escaping etc. might not be handled and thus leave a wide open vulnarbility to your entire system!
So the solution to this is to modify your script so that it can take the file as an argument (if that isn't already the case) and if you really need to have an entire file's content parsed as an argument I'd really advise you to create a temporary file in /tmp/ (if on Linux) or in your %TEMP% directory on Windows and parse the file the content into the file and after that give your support.pl script the new temp file as an argument.

SSIS - Use data from Flat File Source in Subject Line of Send Mail task

We import files from an FTP. The file could be named anything.csv.
Every 15 mins, we use a Script Task to pull the directory listing of an FTP folder; then in a ForEach Loop Container (using Variable Enumerator) we download each FTP file, process it, delete it from the FTP and send a generic "Complete" email to confirm a file has been processed.
Is there a way we can use one of the columns ([ClientName]) in the anything.csv data file and put in the Subject line of the email we send out. So it reads "[ClientName] order has been imported"
I've tried using an Object variable (via a Recordset Destination and it's VariableName), but I get the error "the datatype of variable "User::Subject" is not supported in an expression".
Thanks
Chris
If you only want one client name per .csv file, you need to populate a string-type variable with just one clientname from the file. Then you will be able to use that string variable to build the subject line expression.

Perl write to file returns huge weird stacktrace

I have the following problem: when I try to save the file that contains a semicolon in the name it returns a huge and weird stacktrace of the characters on the page. I've tried to escape, to trim and to replace those semicolons, but the result is still the same. I use the following regex:
$value =~ s/([^a-zA-Z0-9_\-.]|;)/uc sprintf("%%%02x",ord($1))/eg;
(I've even added the |; part separately..)
So, when I open the file to write and call the print function it returns lots of weird stuff, like that:
PK!}�3y�[Content_Types].xml ���/�h9\�?�0���cz��:� �s_����o���>�T�� (it is a huge one, this is just a part of it).
Is there any way I could avoid this?
Thank you in advance!
EDIT:
Just interested - what is the PK responsible of in this string? I mean I can understand that those chars are just contents of the file, but what is PK ? And why does it show the content type?
EDIT 2.0:
I'm uploading the .docx file - when the name doesn't contain the semicolon it works all fine. This is the code for the file saving:
open (QSTR,">", "$dest_file") or die "can't open output file: $qstring_file";
print QSTR $value;
close (QSTR);
EDIT 3.0
This is a .cgi script, that is called after posting some data to the server. It has to save some info about the uploading file to a temp file (name, contents, size) in the manner of key-value pairs. So any file that contains the semicolon causes this error.
EDIT 4.0
Found the cause:
The CGI param function while uploading the params counts semicolon as the delimiter! Is there any way to escape it in the file header?
The PK in file header it means it is compressed ZIP like file, like docx.
One guess: The ; is not valid character in filename at the destination?
Your regexp is not good: (the dot alone is applicable to any character...)
$value =~ s/([^a-zA-Z0-9_\-.]|;)/uc sprintf("%%%02x",ord($1))/eg;
Try this:
#replace evey non valid char to underscore
$value =~ s/([^a-zA-Z0-9_\-\.\;])/_/g;

What is the ideal way to take backup of a configuration file using perl script, which you are going to edit

I am trying to create a script that manipulates a configuration file . So I need to take back up of the existing configuration file , in case there is any problem during manipulation the contents of the back up file should replace the contents of the configuration file . also when rollback is given as argument to the script the contents of the backup file should replace configuration file .
Typically I'd create a file with a name based on the original file's name:
my $file = 'input.txt';
my $new_file = "$file.new";
Start reading lines from the input file, and manipulate them as necessary before writing them to the new file.
When you reach the end-of-file of the input file, close them both. Rename the input file to "$file.old", then rename the new file to the old name $file.
You want to keep the original file intact as long as possible so it remains available in case something fails during processing.
If you have to roll back, reverse the rename process if processing completed. If processing didn't complete just delete the new file.