Uploading .pdf and .doc files from list of URLs directly to Google Drive - pdf

I have a Google Sheet with a list of URLs for files - roughly 900 entries, maybe 95% PDFs with a few .docs and .docxs in there as well.
I would like to upload every file to a Google Drive folder - ideally a shared folder within my employer's workspace - retaining the filename, which I also have in the sheet.
I have found some near-answers on here, but they use deprecated Google Scripts methods.
For example:
var urlOfThePdf = 'http://www.fostexinternational.com/docs/tech_support/pdfs/280_owners_manual.pdf';// an example of online pdf file
var folderName = 'GAS';// an example of folder name
function saveInDriveFolder(){
var folder = DocsList.getFolder(folderName);// get the folde
var file = UrlFetchApp.fetch(urlOfThePdf); // get the file content as blob
folder.createFile(file);//create the file directly in the folder
}
fails at getFileFromURL.
Any suggestions gratefully received.

This piece of code will save the the pdf into the desired folder:
var urlOfThePdf = '';// an example of online pdf file
var folderName = '';// an example of folder name
function saveInDriveFolder(){
var folders = DriveApp.getFoldersByName(folderName);
var file = UrlFetchApp.fetch(urlOfThePdf);
while (folders.hasNext()) {
var folder = folders.next();
folder.createFile(file);
}
}
It uses the DriveApp.getFoldersByName() method to fetch all folders with that name, UrlFetchApp.fetch() to fetch the pdf and folder.createFile() to save the pdf in the folder.
Note that DriveApp.getFoldersByName() fetches a folder iterator (all the folders in your Drive with that name). In this code, I am iterating through all the folders with that name and saving the pdf in all of them.
I would recommend using Drive.getFolderById() and supplying the id of your desired folder (can be extracted from the url). Using this, you will only save the pdf in one folder and you will not have to iterate though the folders in the code (the whole while loop can be replace by: folder.createFile(file);).

Related

Why is my zip folder not locked with password, but the files inside are locked?

I have been struggling for a few hours now, trying to set a password on a zip folder.
However, however many times I tried with different code, the password is applied to each file inside the zip folder, and that's not what I want. I only want to apply the password to the folder itself, and no password should be set to the individual files inside.
Here is my code: (For your information, I am using DotNetZip)
//Assume that there is a folder with multiple files in it at C:\\ExampleFolder
using (Ionic.Zip.ZipFile z = Ionic.Zip.ZipFile())
{
z.Password = "MyPassword"; //Setting the password
z.AddDirectory(#"C:\\ExampleFolder"); //I thought the directory added here should be pw-protected
z.Save(#"C:\\FinalResult.zip"); //Create the pw-protected zip folder
}
However, when I run this program, it does create a zip folder named FinalResult.zip, but that zip folder is not password-protected. All the files inside that zip folder are password-protected. If this is the case, the user will have to enter the password every time they try to open a file inside, which is kind of inconvenient. I just want the user to have to enter the password only once when they try to open the zip folder.
Can anyone tell me why this code doesn't do what I want to achieve?
The password is actually working correctly:
When writing a zip archive, this password is applied to the entries, not to the zip archive itself. It applies to any ZipEntry subsequently added to the ZipFile, using one of the AddFile, AddDirectory, AddEntry, or AddItem methods, etc. When reading a zip archive, this property applies to any entry subsequently extracted from the ZipFile using one of the Extract methods on the ZipFile class.
See the remarks section in the documentation for Password in DotNetZip
https://documentation.help/DotNetZip/4444d7a5-3324-8af9-3ed3-5bf6551d3cd1.htm

How i make my script create the doc in a folder not the root directory in google drive

I have a script that merger some google docs, but it create the file in the root dir, i want the script to add it into a existing folder.
Code
Sorry im new to this..How do i do this?
Thanks
I believe you want to achieve this using Apps Script.
You could use Apps script's DriveApp.getFolderById() method to get the required folder. Also DocumentApp.create() method to create a document.
Please find the working example to create a file in specific Drive folder:
function createDoc()
{
var targetFolder = DriveApp.getFolderById(folderID);
var newDoc = DocumentApp.create('My file1');
var file = DriveApp.getFileById(newDoc.getId());
targetFolder.addFile(file);
}
Tried and tested the code.
If you want to create a Spreadsheet file, you just use SpreadsheetApp instead. Hope that helps!

Relative file links in pdf files

I'm creating a single pdf file that I'd like to link to other files in the same directory as the pdf.
ie.
MyFolder
|
|-main.pdf
|-myotherpdf.pdf
|-myotherotherpdf.pdf
I'd like the main.pdf to have links that would cause the default program on the pdf to open the other pdfs.
As I am generating these file on a server and then providing them in a download to the client I cannot use absolute links as these would not exist on the client pc.
So firstly do pdf files actually support relative file links like this, I haven't found much that says they do either way.
Additionally to generate my pdf I'm using abcpdf and providing it html to convert to pdf.
To try and generate the correct out the correct urls in html I have tried the following
<a href='test.pdf'>test pdf link to local file</a>
<a href='#test.pdf'>test pdf link to local file</a>
<a href='/test.pdf'>test pdf link to local file</a>
<a href='file:///test.pdf'>test pdf link to local file</a>
<a href='file://test.pdf'>test pdf link to local file</a>
Most of them either direct to me a point where the pdf document was generated from (temporary file path) or they link hovering shows "file:///test.pdf" in acrobat but clicking it causes a warning dialog to popup asking to allow/deny, upon clicking allow it opens up in firefox with the url "file:///test.pdf" which wouldn't resolve to anything.
Any ideas on how to get this working or if this kind of linking is even possible in pdfs?
I can only answer your question: does PDF files actually support relative file links like this?
Yes, it does. I created a little test with a main.pdf that has two links to two other PDF documents in the same folder. I created the links manually with Acrobat and associated a launch action with the link annotation. See the internal structure here:
Here is the zip with the main plus two secondary PDFs. Note that you can copy them anywhere and the relative links remain valid.
https://www.dropbox.com/s/021tvynkuvr63lv/main.zip
I am not sure how you would accomplish this with abcpdf, especially since you are converting from HTML which probably limits the PDF features available.
So I got it working in the end thanks to #Frank Rem and some help from the abcpdf guys
Code is as follows
foreach (var page in Enumerable.Range(0, doc.PageCount))
{
doc.PageNumber = page;
var annotEnd = doc.GetInfoInt(doc.Page, "Annot Count");
for (var i = 0; i <= annotEnd; ++i)
{
var annotId = doc.GetInfoInt(doc.Page, "Annot " + (i + 1));
if (annotId > 0)
{
var linkText = doc.GetInfo(annotId, "/A*/URI*:Text");
if (!string.IsNullOrWhiteSpace(linkText))
{
var annotationUri = new Uri(linkText);
if (annotationUri.IsFile)
{
// Note abcpdf temp path can be changed in registry so if this changes
// will need to rewrite this to look at the registry
// http://www.websupergoo.com/helppdfnet/source/3-concepts/d-registrykeys.htm
var abcPdfTempPath = Path.GetTempPath() + #"AbcPdf\";
var relativePath = annotationUri.LocalPath.ToLower().Replace(abcPdfTempPath.ToLower(), string.Empty);
// Only consider files that are not directly in the temp path to be valid files
// This is because abcpdf will render the document as html to the temp path
// with a temporary file called something like {GUID}.html
// so it would be difficult to tell which files are the document
// and which are actual file links when trying to do the processing afterwards
// if this becomes and issue this could be swapped out and do a regex on {GUID}.html
// then the only restriction would be that referenced documents cannot be {GUID}.html
if (relativePath.Contains("\\"))
{
doc.SetInfo(annotId, "/A*/S:Name", "Launch");
doc.SetInfo(annotId, "/A*/URI:Del", "");
doc.SetInfo(annotId, "/A*/F:Text", relativePath);
doc.SetInfo(annotId, "/A*/NewWindow:Bool", "true");
}
}
}
}
}
}
This will allow each link to be opened in the viewer that is associated with it on the pc.

How to load files dynamically in an AIR app? Do I have to use the File class?

I have an XML file that specifies the image files that I need to load. The XML file and the image files live in a subfolder relative to where the AIR app lives.
I need to load the XML file and also the images (load them and add them as children to a movieclip)
In my AIR app, when I tried to load it via the URLRequest, it didn't work.
myLoader = new URLLoader();
myLoader.load(new URLRequest(xmlFilename));
myLoader.addEventListener(Event.COMPLETE, processXML);
(I know this works from a .swf b/c I've tested it)
I've found some sample which uses the File class and here's my code and this does work:
var aFile:File = File.applicationDirectory.resolvePath( xmlFilename );
var aStream:FileStream = new FileStream();
aStream.open( aFile, FileMode.READ );
configXML = new XML( aStream.readUTFBytes( aStream.bytesAvailable ) );
aStream.close();
processXML();
...
I'm now trying to load the images specified in the XML file and I'm finding that I have to use the File class to reference the image in the file system.
var engImageFile:File = File.applicationDirectory.resolvePath( "./english/"+ engFilename );
ldr = new Loader();
var urlReq:URLRequest = new URLRequest( engImageFile.url );
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, engImgLoaded );
ldr.load(urlReq);
Is this the way that AIR accesses files (using the File class) when it wants to read/load/etc. them?
if I understand your question correctly, I belive you just need to load it as a data source/array, which you can then bind to a component of your choosing.
In the documentation it states that if you use the File class to access files then this is safe across all platforms. Also the .url property of the File object will use the appropriate URL scheme...
"app:" - relative to the application directory
"app-storage:" - relative to the special application storage directory
"file:" - all else...I think if you specify an absolute path

I can't get netbeans to find a txt file I have in the same directory... java.io.FileNotFoundException

I can't make it path specific because once I get this program to work (this is the last thing I have to do) I'm uploading to my university's ilearn website and it has to run on my professors computer with no modifications. I've tried a few different amalgamations of code similar to the following...
File file = new File("DataFile.txt");
Scanner document = new Scanner(new File("DataFile.txt"));
Or...
java.io.File file = new java.io.File("DataFile.txt");
Scanner document = new Scanner(file);
But nothing seems to work. I've got the necessary stuff imported. I've tried moving DataFile around in a few different folders (the src folder, and other random folders in the project's NetBeansProjects folder) I tried creating a folder in the project and putting the file in that folder and trying to use some kind of
documents/DataFile.txt
bit I found online (I named the folder documents).
I've tried renaming the file, saving it in different ways. I'm all out of ideas.
The file is just a list of numbers that are used in generating random data for this program we got assigned for building a gas station simulator. The program runs great when I just use user input from the console. But I can not get netbeans to find that file for the life of me! Help!?!?!?
Try adding the file to build path ..
public void readTextFile (){
try{
Scanner scFile =new Scanner(new File("filename.txt");
while(scFile.hasNext()){
String line =scFile.nextLine();
Scanner details=new Scanner(line).useDelimiter("symbol");
than you can work from there to store integer values use e.g in an array
litterArr(size)=details.nextInt();
Note: size is a variable counting the size/number of info the array has.
}
scFile.close();
{
catch
(FILENOTFOUNDEXCEPION e){
..... *code*
}
Keep file in the same folder as the program,but if it is saved in another folder you need to supply the path indicating the location of the file as part of the file name e.g memAthletics.Lines.LoadFromFile('C:\MyFiles\Athletics.txt');
hope this helps clear the problem up :)