How to open/operate file in batch classes? - batch-processing

I have to import in a Table from file (csv for example).
I used this code:
public void run ()
{
TextIO textIO;
str filename, fileOpen, folder;
int handle;
Io thisMYFile;
FileIoPermission perm;
#File
#avifiles
#OCCRetryCount
[handle, filename] = WINAPI::findFirstFile(folder + "\\*.csv");
fileOpen = strFmt (folder + "\\" + filename);
perm = new FileIoPermission(fileOpen, 'w');
perm.assert();
thisMYFile = new CommaTextIo(fileOpen , 'w');
}
But in Debug the class IO thisMYFile is null and so I can't to read and get information.
In my class declaration I extends RunBaseBarch.
If I used a "normal" classes (not batch) I can read,write,move file etc, but now I can't to open.
I know the WinAPI classes not work in Batch, now I think there is also another way to read/open file in batch? With WinAPIServerclass I can findFirstFile ?
A clarification, I have also a same problem if I don't schedule the process batch : flag Batch processing is false, in the batch dialog, and clicked only Ok. (example image)
If you've a tutorial, any advice or help,
Thanks all,
enjoy!

Beware that the batch runs on an other machine, the AOS will not have access to your local files.
Therefore always use a full UNC file path to the file, ex. \\MyHost\Temp\x.csv
If new CommaTextIO fails to open the file, it returns null, it does not throw an exception. If you do not test for null your code will fail later.

Related

HMS Drive kit Query and query string

I'm a beginner developer just starting with HMS. I'm trying to write a code that finds the "profile.json" file, which was previously uploaded to the cloud into the folder "DebtSorter". My code (Kotlin) for it currently looks like:
var fileList: MutableList<File?> = mutableListOf()
thread {
try {
val drive = buildDrive()
val request = drive.files().list()
var cursor: String?
fileList = ArrayList()
do {
var result = request.setQueryParam("fileName contains 'profile'")
.setOrderBy("fileName")
.setPageSize(10)
.setFields("*")
.execute()
for (file in result.files) {
fileList.add(file)
}
cursor = result.nextCursor
request.cursor = cursor
} while (!StringUtils.isNullOrEmpty(cursor))
} catch (e: java.lang.Exception) {
Toast.makeText(applicationContext, "executeFilesList exception: $e", Toast.LENGTH_LONG)
.show()
}
}
return fileList
However, the returned fileList is always empty, even when I upload the file to the root of the cloud. I think I'm messing something up with the query string, because I can upload files just fine. Can someone tell me the syntax and keywords used in the query string, and possibly help me with the code and how to search in a given folder in HMS Drive kit?
Update:
You can also refer to the demo: https://github.com/HMS-Core/hms-drive-serverdemo In the demo, the thread executes the method of obtaining the file list directly.
Your query parameters is incorrect. You can't define the format of your query string. Please refer to this sample query statement.
(assuming that the ID of folder A is f192358798744098816):
To query all files in folder A, run the following statement: 'f192358798744098816' in parentFolder and mimeType != 'application/vnd.huawei-apps.folder'
To query all folders in the recycle bin, run the following statement: mimeType='application/vnd.huawei-apps.folder' and recycled=true
The query criteria 'fileId' in parentFolder and recycled=true cannot be used together, that is, a user cannot query files or folders in a specified parent folder in the recycle bin.
For more details, see the official guide.
In the end, the problem was with creating the thread. Since the thread is asynchronous and I created the fileList variable before the thread, modified it in the thread and returned it after the thread, the actual modification happened after the method already returned with the empty fileList. I took out the lines with thread and put the method call into a thread. This solved it.

Process.Start PDF in a folder

I can't get Process.Start to simply launch a PDF with default PDF viewer.
I tried so many combinations of shell execute, working folder etc etc. Keeps giving me either 'The system cannot find the file specified' or 'the directory name is invalid'
private void button1_Click(object sender, EventArgs e)
{
string filename = #"Milking and cooling software set 2018-39.pdf";
MessageBox.Show(currentpath + #"\Astronaut A5 v1.5(b7)\documentation\" + filename);
fullpath = currentpath + #"\Astronaut A5 v1.5(b7)\documentation";
fullfile = fullpath + filename;
ProcessStartInfo process = new ProcessStartInfo();
process.WorkingDirectory = fullpath;
process.UseShellExecute = false;
process.FileName = fullfile;
process.RedirectStandardOutput = true;
process.Verb = "run as";
Process.Start(process);
}
Why is this so hard, I have tried for hours to simply lauch Acrobat Reader to open a PDF file. I can double click it no problem in it's location but C# can't open it, either I get .NET errors or Adobe opens and says it can't find the file. Tried so many combinations of "\"", full path, hard coded path etc etc...unbelievable that this is so hard to code in this day and age.
You’ve told the system to not use ShellExecute. This means the path you’re giving should be an actual executable program. PDFs are not so if you want to open it with the default reader use ShellExecute.
process.UseShellExecute = true;
Also using “run as” as the verb doesn’t make any sense here, unless there is such a verb defined for PDFs which I’m pretty sure there isn’t. That should be removed.

Google Drive: Move file to folder

I want to understand the moveFileToFolder script that is listed as an example when you select google drive scripts http://www.google.com/script/start/ (click "Start Scripting" then select "Drive" option in "Create script for" menu)
The code is below. My question is how would I alter this code so that it could be used to move the file "Austin" into the folder "Texas"? This is presuming that the file "Austin" is a google doc which is currently sitting in my main google drive file list and the folder "Texas" is also currently sitting in my main google drive list.
There are a few other posts regarding moving files in drive and I understand the main concepts but I can't seems to successfully procure the file or folder ID's. Any help greatly appreciated?
/**
* This script moves a specific file from one parent folder to another.
* For more information on interacting with files, see
* https://developers.google.com/apps-script/class_file
*/
function moveFileToFolder(fileId, targetFolderId) {
var targetFolder = DocsList.getFolderById(targetFolderId);
var file = DocsList.getFileById(fileId);
file.addToFolder(targetFolder);
};
now I had the same task and used this code:
var file = DriveApp.getFileById(fileId)
var folder = DriveApp.getFolderById(folderId);
folder.addFile(file);
Check out function getFolderByName in https://gist.github.com/suntong001/7955694 to see how to get folder ID by its name. For files the principle is the same.
function addDocToPublic() {
// Create random filename
var fn = "doc" + Math.floor(10000 * Math.random()) + ".doc";
// Create file with very MIME TYPE and return file ID
var fh = DriveApp.createFile(fn, "Das ist das " + fn + " Testfile", MimeType.MICROSOFT_WORD);
// Get first/next Iterator ID ( = folder ID) for Folder (with name PUBLIC)
var fid = DriveApp.getFoldersByName("PUBLIC").next();
// Copy File to Folder (using file and folder IDs
fh.makeCopy(fn,fid);
// Remove Source File
fh.setTrashed(true);
}
... maybe that way ...

phantomjs: how to make fs.open behave the same as require, for paths

require() works based on the script path. fs.open() works on the invocation (current shell) path.
Let's say i have the tree:
dirA/
dirA/dirB/
dirA/dirB/dirC/
dirA/dirB/dirC/test.js
dirA/dirB/dirC2/include_me.js
dirA/dirB/dirC2/open_me.js
and the contents for test.js:
var myMod = require('../dirC2/include_me.js');
var fs = require('fs');
fs.open('../dirC2/open_me.js')
Regardless of the current path I am when i execute phantomjs $PATH/test.js the require will succeed, and the fs.open will fail (unless $PATH is dirC)
Is there any way to make fs.open() behave as require()?
I'm thinking something like fs.open( phantomjs.getScriptPath() + '../dirC2/open_me.js' );
but my searchfoo failed to find anything to fill in for the made-up getScriptPath method there.
Edit:
posting here the workaround i'm using in case the answer is "No".
/**
* Opens a file relative to the script path.
* this solves an inconsistency between require() and fs.open()
*/
exports.getScriptPath = function( newPath ){
var script, scriptPath;
script = exports.system.args[0];
scriptPath = script.replace(/\/[^\/]+$/, '') // removes everything from
// the last "/" until the end of
// the line, non-greedy
return scriptPath + '/';
}
this is used in a utility module i have. Also it assumes you are calling a script (won't work well if you use said module in an interactive phantomjs session) and unix style paths. if you're using windows, just add \ to the regexp. then it's used like:
filehandle = exports.fs.open( exports.getScriptPath() + '../dirC2/open_me.js', 'r' );

How to get the name of a temporary file created by File.tmpfile in D2?

I need to generate a temporary file, fill it with some data and feed it to an external program. Based on description of D available here I'm using File.tmpfile() method:
auto f = File.tmpfile();
writeln(f.name());
which doesn't provide a way to get the generated file name. It's documented that name might be empty. In Python I would do that like this:
(o_fd, o_filename) = tempfile.mkstemp('.my.own.suffix')
Is there a simple, safe and cross-platform way to do that in D2?
Due to how tmpfile() works, if you need the name of the file you can't use it. However, I have already created a module to work with temporary files. It uses conditional compilation to decide on the method of finding the temporary directory. On windows, it uses the %TMP% environment variable. On Posix, it uses /tmp/.
This code is licensed under the WTFPL, so you can do whatever you want with it.
module TemporaryFiles;
import std.conv,
std.random,
std.stdio;
version(Windows) {
import std.process;
}
private static Random rand;
/// Returns a file with the specified filename and permissions
public File getTempFile(string filename, string permissions) {
string path;
version(Windows) {
path = getenv("TMP") ~ '\\';
} else version(Posix) {
path = "/tmp/";
// path = "/var/tmp/"; // Uncomment to survive reboots
}
return File(path~filename, permissions);
}
/// Returns a file opened for writing, which the specified filename
public File getTempFile(string filename) {
return getTempFile(filename, "w");
}
/// Returns a file opened for writing, with a randomly generated filename
public File getTempFile() {
string filename = to!string(uniform(1L, 1000000000L, rand)) ~ ".tmp";
return getTempFile(filename, "w");
}
To use this, simply call getTempFile() with whatever arguments you want. Defaults to write permission.
As a note, the "randomly generated filenames" aren't truely random, as the seed is set at compile time.