Change directory and executing external command in cmd - ERLANG - cmd

I am working on Erlang script that need to execute some command in cmd. I was trying to solve problem on this way, but I get nothing.
start() ->
comparing(0000).
comparing(Num) ->
file:set_cwd("F:\Documents\UPI\dump_rabbitmq\rabbitmq-dump-queue-v0.3-windows-amd64"),
DirOut = os:cmd("dir"),
io:format("DirOut=~p~n", [DirOut]),
os:cmd('rabbitmq-dump-queue -uri="amqp://username:password#localhost:5672/" -queue=try -full -max-messages=5 -output-dir=E:\Programs\Erlang\erl8.3\usr'),
NumMes = Num,
io:format("NumMes=~p~n", [NumMes]),
NameF = "msg-000",
io:format("NameF=~p~n", [NameF]),
FileName = concat(NameF, lists:flatten(io_lib:format("~p", [NumMes]))),
io:format("FileName=~p~n", [FileName]),
File = filelib:is_regular(FileName),
io:format("File=~p~n", [File]),
...
This is just a part of code where I have a problem. In line file:set_cwd("F:\Documents\UPI\dump_rabbitmq\rabbitmq-dump-queue-v0.3-windows-amd64") I want to change directory.
In this two lines I check if directory is changed:
DirOut = os:cmd("dir"),
io:format("DirOut=~p~n", [DirOut])
And in line os:cmd('rabbitmq-dump-queue -uri="amqp://username:password#localhost:5672/" -queue=try -full -max-messages=5 -output-dir=E:\Programs\Erlang\erl8.3\usr') I want to execute command in cmd for dump messages from RabbitMQ server. I was trying to change directory with os:cmd("cd F:\Documents\UPI\dump_rabbitmq\rabbitmq-dump-queue-v0.3-windows-amd64"), but nothing happends. Any suggestion how to solve this problem?
EDIT
Problem is solved. It's neccesary to have two slashes in this line: file:set_cwd("F:\\Documents\\UPI\\dump_rabbitmq\\rabbitmq-dump-queue-v0.3-windows-amd64") Dogbert solved the problem in comments.

There are two problems in the code:
You cannot change the working directory with os:cmd("cd ..."), you need to call file:set_cwd/1. os:cmd("cd ...") will only change the directory for the execution of that command, the change will not persist for future calls to os:cmd/1.
You need to escape the slashes, otherwise they're interpreted as escape sequence when it's a valid escape sequence, otherwise they're ignored.
1> io:format("~s~n~s~n", ["C:\abc\nop", "C:\\abc\\nop"]).
C:abc
op
C:\abc\nop
So, changing this:
os:cmd("cd F:\Documents\UPI\dump_rabbitmq\rabbitmq-dump-queue-v0.3-windows-amd64"),
to:
file:set_cwd("F:\\Documents\\UPI\\dump_rabbitmq\\rabbitmq-dump-queue-v0.3-windows-amd64"),
should fix your problem.

Related

Perl code doesn't run in a bash script with scheduling of crontab

I want to schedule my Perl code to be run every day at a specific time. so I put the below code in bash file:
Automate.sh
#!/bin/sh
perl /tmp/Taps/perl.pl
The schedule has been specified in below path:
10 17 * * * sh /tmp/Taps/Automate.sh > /tmp/Taps/result.log
When the time arrived to 17:10 the .sh file hasn't been running. however, when I run ./Automate.sh (manually) it is running and I see the result. I don't know what is the problem.
Perl Code
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use XML::Dumper;
use TAP3::Tap3edit;
$Data::Dumper::Indent=1;
$Data::Dumper::Useqq=1;
my $dump = new XML::Dumper;
use File::Basename;
my $perl='';
my $xml='';
my $tap3 = TAP3::Tap3edit->new();
foreach my $file(glob '/tmp/Taps/X*')
{
$files= basename($file);
$tap3->decode($files) || die $tap3->error;
}
my $filename=$files.".xml\n";
$perl = $tap3->structure;
$dump->pl2xml($perl, $filename);
print "Done \n";
error:
No such file or directory for file X94 at /tmp/Taps/perl.pl line 22.
X94.xml
foreach my $file(glob 'Taps/X*') -- when you're running from cron, your current directory is /. You'll want to provide the full path to that Taps directory. Also specify the output directory for Out.xml
Cron uses a minimal environment and a short $PATH, which may not necessarily include the expected path to perl. Try specifying this path fully. Or source your shell settings before running the script.
There are a lot of things that can go wrong here. The most obvious and certain one is that if you use a glob to find the file in directory "Taps", then remove the directory from the file name by using basename, then Perl cannot find the file. Not quite sure what you are trying to achieve there. The file names from the glob will be for example Taps/Xfoo, a relative path to the working directory. If you try to access Xfoo from the working directory, that file will not be found (or the wrong file will be found).
This should also (probably) lead to a fatal error, which should be reported in your error log. (Assuming that the decode function returns a false value upon error, which is not certain.) If no errors are reported in your error log, that is a sign the program does not run at all. Or it could be that decode does not return false on missing file, and the file is considered to be empty.
I assume that when you test the program, you cd to /tmp and run it, or your "Taps" directory is in your home directory. So you are making assumptions about where your program looks for the files. You should be certain where it looks for files, probably by using only absolute paths.
Another simple error might be that crontab does not have permission to execute the file, or no read access to "Taps".
Edit:
Other complications in your code:
You include Data::Dumper, but never actually use that module.
$xml variable is not used.
$files variable not declared (this code would never run with use strict)
Your $files variable is outside your foreach loop, which means it will only run once. Since you use glob I assumed you were reading more than one file, in which case this solution will probably not do what you want. It is also possible that you are using a glob because the file name can change, e.g. X93, X94, etc. In that case you will read the last file name returned by the glob. But this looks like a weak link in your logic.
You add a newline \n to a file name, which is strange.

Atom editor & Processing - 'processing-java' not recognised

I am trying to use bleikamp's Processing package to run Processing sketches from the Atom editor. The package has installed correctly, but running a sketch produces the below error:
'processing-java' is not recognized as an internal or external command, operable program or batch file.
I have added the path to my Processing directory to the PATH environment variable. Can anyone suggest why this is not working?
The problem is almost certainly to do with your PATH. As you've checked the Processing path is correct, there may be something preventing it from being recognised (there are some known issues, such as the PATH variable being too long or having invalid characters).
Solution 1:
Try using FixPath to fix some of the more common problems.
Solution 2:
Try modifying the processing package itself to point directly to processing-java.exe (it points to the PATH variable by default).
In Atom's Settings > Packages, select the processing package and click on 'View Code'.
Make a backup of \lib\processing.coffee to be safe.
In \lib\processing.coffee, search for the following code (probably near the top):
module.exports = Processing =
config:
'processing-executable':
type:"string",
default:"processing-java"
Modify the value of default to point to the exact Processing directory and processing-java.exe, for example:
module.exports = Processing =
config:
'processing-executable':
type:"string",
default:"c:\\program files\\processing\\processing-java.exe"
As Chris rightly points out in the comments below, backslash \ is an escape character in JavaScript and CoffeeScript, so itself needs to be escaped in the file path (hence the double-backslashes \\).
You have to install processing-java command line tool.
In processing, go to Tools -> Install "processing-java"

"Can't open file "C:" for reading; you may not have read permission." error in MATLAB

I have such a code;
for x = 1:100
path = sprintf('C:\Users\hasan_000\Documents\MATLAB\Project\Images\%d.jpg', x);
imgarray = imread(sprintf(path));
end
I have a folder involves 100 pictures. I want to convert them to matrix by uploading automatically in a loop.
But I get this error:
Can't open file "C:" for reading;
you may not have read permission.
How can I fix the problem?
Thanks,
The code should output the warning:
"Warning: Escape sequence '\U' is not valid. See 'help sprintf' for valid escape
sequences. "
You need to escape the \ when using sprintf. With yor code path is C:. For examples how proper escaping is done, please check the documentation for sprintf. Instead I would use this code:
P=fullfile('C:\Users\hasan_000\Documents\MATLAB\Project\Images',sprintf('%d.jpg',x))
imgarray = imread(P);
sprintf('C:\\Users\\hasan_000\\Documents\\MATLAB\\Project\\Images\\%d.jpg', x); should solve the issue.
sprintf('%s%d%s','C:\Users\hasan_000\Documents\MATLAB\Project\Images\',x,'.jpg');
is what I would suggest as it makes the code more intuitive and readable.
sprintf does not like your backslashes \ in the filename since it can be part of a specific command. If you simply run the path file you'll see:
path = sprintf('C:\Users\hasan_000\Documents\MATLAB\Project\Images\%d.jpg', 1);
path = C:
So that's where your code breaks. I'm currently not sitting on a windows machine, but I'd try reversing the slashes from backslashes \ to normal ones / and see if it can open that.
Second method works for sure:
path = ['C:\Users\hasan_000\Documents\MATLAB\Project\Images\', sprintf('%d.jpg', x)]
path = C:\Users\hasan_000\Documents\MATLAB\Project\Images\1.jpg

open file with shell() in R

I want to open a file in a windows program using R, but specifying the program rather than the default for the file extension, and for a file not necesarrily in my current R session home directory (this one getwd())
From looking at the documentation, using shell(), should be the way, but I seem to have an issue with the way R references the home directory or the way I'm writing the string.
e.g.
This works ok in the cmd "run" in windows: excel e:\test.xlsx
but using this
route <- "e:\\test.xlsx"
shell(paste("excel " , route, sep=""), flag="")
seems to get to excel (excel copyright notice is printed), but also prints the home directory and doesn't open the file in route. Thanks for any help.
Your command does the same for me. However, this works:
shell(paste("start", "excel", route))

How to add the current session file name in the status line in Vim?

I recently added the sessionman plugin to my Vim configuration, and I like it so far.
I understand that Vim sets v:this_session to the session file name when a session is being used and I’d like to add it to my status line. Unfortunately, v:this_session contains the full file path and it is often way too long for it to fit in the status line.
So my question is: How can I extract the file name without its full path from v:this_session and add it to my status line?
For this, Vim has the :help filename-modifiers like :t for the last component of the file. You can use them with the fnamemodify() function (or expand() if you want to modify a built-in Vim file identifier like %):
:echo fnamemodify(v:this_session, ':t')
Since the forward slash character is used as path separator in Vim on all
operating systems, to obtain the filename from a full path it is sufficient to
extract the last component of the path. For doing that one can can use the
matchstr() or substitute() functions:
substitute(t, '^.*/', '', '')
or
matchstr(t, '[^/]*$')

Resources