Sidebar

How to connect to a remote share in a mapping node with credentials?

0 votes
840 views
asked Jan 28, 2016 by rich-c-2789 (16,180 points)
retagged Mar 12, 2021 by rich-c-2789
I need to read and write files on a remote share in a mapping node.  I don't know how to specify the user and password.

1 Answer

0 votes

As of version 43 of QIE a new class called NetFile was added.  This is compareable to java.io.File in its use.  With this addition the codewizard methods writeFile, readFile, moveFile, and deleteFile have been enhanced to work with this class as a parameter while still working with Strings and java.io.file.  Below is an example of how to use NetFile to connect to remote shares combined with passing NetFile as a parameter to the methods mentioned.

NOTE: As of version .47 the code wizard option "New NetFile" generates this code "qie.newNetFile('filepath', 'domainUsername', 'password', 'parameters', 'autoClose');"  Due to a memory leak the autoClose optional parameter has been added.  Also a close() method has been added to the NetFile object.   If you would like to manage closing the NetFile your self set this new parameter to false and call close on the netfile manually.  Example: myNetFile.close().

 

NOTE: the constructor for NetFile requires the user name and password.

// Create NetFile references to remote files and directories.
//NOTE: as of .47 qie.newNetFile() has a new 'autoClose' parameter that defaults to true and closes the NetFile object after 5 Minutes.  Below demostrates closing it manually.
var sourceDir = qie.newNetFile('//servername/share/richtemp/', 'myusername', 'password', false);  
var sourceFile = qie.newNetFile('//servername/share/richtemp/important.txt', 'myusername', 'password', false);
var backupDir = qie.newNetFile('//servername/share/richbackup/', 'myusername', 'password', false);
var backupFile = qie.newNetFile('//servername/share/richbackup/important.txt', 'myusername', 'password', false);

try { // NOTE: as of version .47 the netFile should be closed when it is no longer needed.
   qie.writeFile(sourceFile, "In 900 years of time and space, I've never met anyone that wasn't important", true);

   qie.moveFile(sourceFile, backupDir, true);

   var quote = qie.readFile(backupFile);
   qie.info("The Doctor said: " + quote);

   // Clean up the files and directories used in this example
   if (sourceFile.exists()) {
      qie.deleteFile(sourceFile);
   }

   if (sourceDir.exists()) {
      qie.deleteFile(sourceDir);
   }

   if (backupFile.exists()) {
      qie.deleteFile(backupFile);
   }

   if (backupDir.exists()) {
      qie.deleteFile(backupDir);
   }   
} finally {
   sourceDir.close();
   sourceFile.close();
   backupDir.close();
   backupFile.close();
}

To specifiy a domain enter the domain as part of the user name using this pattern [domain;]username.  For example: qie.newNetFile('//servername/share/richtemp/', 'somedomain;myusername', 'password', true);

Also see these related questions:

How can I connect to a shared folder?

How can I archive to a shared folder on another domain?

How to add credentials to a network share in windows?

How can I remove the credentials used by windows for a remote shared folder?

 

answered Jan 28, 2016 by rich-c-2789 (16,180 points)
edited Sep 6, 2022 by rich-c-2789
...