Google Drive API search for folder in Team Drive folder with many folder levels; if exists return ID if not create folder at the correct level - google-api

I am trying to locate a folder inside a Google Team Drive, which has a folder structure with multiple levels.
Here is an example:
Team Drive
- Folder A
- Folder B
- Folder C
I would like to search for Folder C by using the folder ID of the Team Drive. If the folder exists, return the folder ID for Folder C and if not, create that folder under Folder B.
So, I have some code I am working with, creating the Google Drive API query.
Here is the code snippet I am using - (Team Drive ID) would be the ID:
// Set the drive query...
String driveQuery = "mimeType='application/vnd.google-apps.folder' and '(Team Drive ID)' in parents and name='Folder C' and trashed=false";
try {
result = service.files().list().setQ(driveQuery).setIncludeTeamDriveItems(true).setSupportsTeamDrives(true)
.execute();
} catch (IOException e) {
e.printStackTrace();
}
for (File folder : result.getFiles()) {
System.out.printf("Found folder: %s (%s)\n", folder.getName(), folder.getId());
foundFolder = true;
folderID = folder.getId();
}
if (foundFolder != true) {
// Need to create the folder...
File fileMetadata = new File();
fileMetadata.setName(folderKeyToGet);
fileMetadata.setTeamDriveId(parentFolderID);
fileMetadata.set("supportsTeamDrives", true);
fileMetadata.setMimeType("application/vnd.google-apps.folder");
fileMetadata.setParents(Collections.singletonList(parentFolderID));
try {
newFolder = service.files().create(fileMetadata).setSupportsTeamDrives(true).setFields("id, parents")
.execute();
} catch (IOException e) {
e.printStackTrace();
}
// Send back the folder ID...
folderID = newFolder.getId();
System.out.println("Folder ID: " + newFolder.getId());
}
return folderID;
I have the query set to:
"mimeType='application/vnd.google-apps.folder' and '(Team Drive ID)' in parents and name='Folder C' and trashed=false"
but is seems not to traverse the folder structure to find the Folder C folder.
Do I need to have the Folder B folder ID to find the folder or is there a way to have the query search the whole Team Drive for the folder?

Related

What is the recommended path to save auto - generated files in?

I am currently building a WinForms app and I need to create a bin file
in which I will serialize data. I let the user choose in which folder he wants the file to be saved in, but if he doesn't choose anything I want to save the file in a default path.
The thing is, I am not so familiar with windows' file system, and I am unable to find a good folder to save the file in.
My requirements from such folder are:
All windows computers should have it
The path to this folder all windows computer is the same
Is used for programs' auto-generated files as an "international default"
Not used frequently by the user (a "just don't touch" folder)
what is the common solution for such things?
You cas use a dedicated common ProgramData folder:
string CommonAppDataFolderPath
= Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
+ Path.DirectorySeparatorChar
+ AssemblyCompany
+ Path.DirectorySeparatorChar
+ AssemblyTitle
+ Path.DirectorySeparatorChar;
public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if ( attributes.Length == 0 )
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
public string AssemblyTitle
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if ( attributes.Length > 0 )
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
if ( titleAttribute.Title != "" )
{
return titleAttribute.Title;
}
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}
}
It is AppData and you can get it with Environment.GetFolderPath:
(Environment is in System.IO namespace)
string binPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SomeAppName");
// this will return something like this:
// C:\Users\SomeUserName\AppData\Roaming\SomeAppName
You can use System.Reflection to identify a path in the debug or release folder of your assembly:
public static string Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "someapp");

Google API REST Get the FilesInFolder Name

I want to upload a file to a Google drive folder, if the file name is same i want to skip. With the code below i able upload file to Google Drive. But how to get the filename inside a folder?
File newFile = GoogleDriveHelper.uploadFile(service, dir, directoryId);
I'm referring to google REST document when I pass in all the param, it will return the Id in Google Drive.
Thx for Advice!
Check here pass the fileId to this function. The fileId can be get at here
public static void printFile(DriveService service,String fileId)
{
try
{
File file = service.Files.Get(fileId).Execute();
Console.WriteLine("Title: " + file.Title);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}

Polling from a network directory

I have been working on the following project, some background:
I am an intern currently developing a new search system for my organization. The current setup is microsoft sharepoint 2013 in which the users upload files etc.. and on the other hand is the system I am developing which indexes all data being uploaded to apache SOLR.
I have been succesfull in mapping the sharepoint content repository to a network drive, and I can manually start my program to start indexing the conent of this network drive to SOLR using the Solrj api.
The problem I am facing however is that I am unable to poll events from this network drive. In my test build which ran local I used a watcher service to launch code (reindex documents, delete indexes) on file create, file modify and file delete.
This does not work unfortunantly with a url pointing to a network drive :(.
So the big question: Is there any API / library available for polling events from network drives?
Any help would be extemely appreciated !
So I fnally figured this one out, tried looking at .net's variant of the watcher service (system.io.filesystemwatcher) and i was having the same problem. I finally got it working by using java.io.FileAlterationMonitor / observer.
Code:
public class UNCWatcher {
// A hardcoded path to a folder you are monitoring .
public static final String FOLDER =
"A:\\Department";
public static void main(String[] args) throws Exception {
// The monitor will perform polling on the folder every 5 seconds
final long pollingInterval = 5 * 1000;
File folder = new File(FOLDER);
if (!folder.exists()) {
// Test to see if monitored folder exists
throw new RuntimeException("Directory not found: " + FOLDER);
}
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor =
new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
// Is triggered when a file is created in the monitored folder
#Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
System.out.println("File created: "
+ file.getCanonicalPath());
if(file.getName().endsWith(".docx")){
System.out.println("Uploaded resource is of type docx, preparing solr for indexing.");
}
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
// Is triggered when a file is deleted from the monitored folder
#Override
public void onFileDelete(File file) {
try {
// "file" is the reference to the removed file
System.out.println("File removed: "
+ file.getCanonicalPath());
// "file" does not exists anymore in the location
System.out.println("File still exists in location: "
+ file.exists());
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
System.out.println("Starting monitor service");
monitor.start();
}
}

Script to move files from folder to another folder using paths in text file

On Windows 8, could someone please help me create a script to move some images from a particular folder to another folder?
The file path that lists the images i want to move (not all images) from the folder are listed in this file: C:\Users\Emmanuel\Desktop\test.txt
The folder in which contains some of the images I want removed appear in this folder:
C:\Users\Computer\Desktop\Images1
The folder in which I want the images to be moved to is this folder:
C:\Users\Computer\Desktop\Images2
Your help will be much appreciated
Try this where SourcesFile is your test.txt and DestFolder is the destination.
public int Run()
{
if (!File.Exists(SourcesFile))
{
throw new ArgumentException("Source folder does not exist");
}
if (!Directory.Exists(DestFolder))
{
Console.WriteLine("Destination folder doesn't exist");
Console.WriteLine("Creating destination folder...");
Directory.CreateDirectory(DestFolder);
}
string[] files = File.ReadAllLines(SourcesFile);
Console.WriteLine("Moving {0} files...", files.Length);
foreach (string file in files)
{
string dest = Path.Combine(DestFolder, Path.GetFileName(file));
if (File.Exists(dest))
{
string newFilename = string.Format("{0}_{1}{2}",
Path.GetFileNameWithoutExtension(file),
Guid.NewGuid().ToString("N"),
Path.GetExtension(file));
string newDest = Path.Combine(DestFolder, newFilename);
Console.WriteLine("File {0} already exists, copying file to {1}", file, newDest);
File.Move(file, newDest);
continue;
}
File.Move(file, dest);
}
return 0;
}

Delete a directory from Isolated storage windows phone 7

I created a directory named "MyFolder" and wrote some text files there. Now, I want delete that directory and I am using the following code:
public void DeleteDirectory(string directoryName)
{
try
{
using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
{
currentIsolatedStorage.DeleteDirectory(directoryName);
textBox1.Text = "deleted";
}
}
}
catch (Exception ex)
{
// do something with exception
}
}
I tried with
DeleteDirectory("MyFolder")
DeleteDirectory("IsolatedStore\\MyFolder")
however it didn't delete that directory. Any idea to solve that?
Have you deleted all of the contents of that directory?
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.deletedirectory(v=vs.80).aspx
says (although it isn't the windows phone version of the documentation):
A directory must be empty before it is deleted. The deleted directory cannot be recovered once deleted.
The Deleting Files and Directories example demonstrates the use of the DeleteDirectory method.

Resources