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

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!

Related

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

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);).

Unpack a rar file

Okay, so I have searched for dll files that will allow me to unrar files and I was able to find quite a few such as unrar.dll, chilkat, sharpcompress and some more but I wanted to use the one provided by Rar themselves.
So I referenced the DLL file in my project and imported it. I was using unrar.dll.
But I wasn't able to find any up to date code to allow me to test and try things out. All the examples I found were either not up to date or not for Vb.net.
I also tried the official example, which came in the installation but that didn't work even after I fixed it and when I tried to use the code I always got an error for
object reference not set to an instance of an object
I just want to unrar a rar file from a specific location to the root directory of my program so if my program was on the desktop I want it to unrar a file in My documents and extract the files to my desktop.
If you just want to unrar files, I Was able to do that with SharpCompress
First I created a new VB.Net App and added a reference to SharpCompress.dll before using this code to extract all the files from a Rar file.
'Imports
Imports SharpCompress.Archives
Imports SharpCompress.Common
'Unrar code
Dim archive As IArchive = ArchiveFactory.Open("C:\file.rar")
For Each entry In archive.Entries
If Not entry.IsDirectory Then
Console.WriteLine(entry.Key)
entry.WriteToDirectory("C:\unrar", New ExtractionOptions With
{.ExtractFullPath = True, .Overwrite = True})
End If
Next
More code samples
for those who will try in vb.net the extract options are renamed and used as
Dim options As New ExtractionOptions With {
.ExtractFullPath = True,
.Overwrite = True
}
entry.WriteToDirectory(Application.StartupPath, options)

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 :)

BlackBerry FileIO: Delete files with specific extension

I'm working on a blackberry application which do some file handling, I would like to know how I can check a specific folder and delete only files with a specific extension. For example, I only want to delete my .log files.
Please, spend some time studying the FileConnection API. It has everything you need.
FileConnection dir = (FileConnection) Connector.open(path);
Enumeration dirContent = dir.list("*.*", true);
FileConnection file = (FileConnection) Connector.open(filePath);
if (file.exists()) {
file.delete();
}

Resolving relative paths outside the standard directories (applicationDirectory, desktopDirectory, etc)

I need to navigate to a file relative to my applicationDirectory, but as it says in the documentation:
No ".." reference that reaches the file system root or the application-persistent storage root passes that node; it is ignored.
But the crazy thing is that if I do something like
File.applicationDirectory.resolvePath("/home/myHome/");
I can get anywhere in the filesystem.
My question is:
is there a workaround to navigate from my applicationDirectory to a relative path like "../../my.cfg" ?? (I need to read a config file generated by a different application)
if you are trying to access root privileged folders - than you can not.
in other cases try do next "home/blah/blah/blah/../../my.cfg" and research once again http://help.adobe.com/en_US/AIR/1.5/jslr/flash/filesystem/File.html to save your time about navigation.
also you have another few ways: create a link to your file or run external bash/bat script.
I was previously using the little hack mentioned in Eugene's answer to copy from an absolute path:
var file = 'C:\Users\User1\Pictures\pic.png';
var newPath = air.File.applicationStorageDirectory.resolvePath('images/pic.png');
air.File.applicationDirectory.resolvePath('/../../../../../../../../../../' +
file).copyTo(newPath, true);
However, this is a much better way of doing it:
var file = 'C:\Users\User1\Pictures\pic.png';
var newPath = air.File.applicationStorageDirectory.resolvePath('images/pic.png');
new air.File(file).copyTo(newPath, true);