Reputation: 51
With a method similar to the one I put below I update the "name" attribute of the files in a Google Drive folder using its "Id" attribute. The task is carried out without problems.
In this case the files are located because they are contained in the indicated folder and it is not necessary to provide any attribute for them.
Now this is doing a process through which it is necessary to provide the name of the file ( with the method I put below). The problem is that the name of the local file in Windows Explorer is not obtained with all the real characters of Google Drive. There are restrictions for naming files in Windows that do not exist in Google Drive. In this environment there can even be repeated file names since the reference and immutable attribute is the "Id".
I take the file names directly from the files selected in Windows Explorer, but since the value does not always correspond to the real value in Google Drive I have to rule out using this attribute to locate the files.
And that is the question. I would need to use a file attribute in Windows Explorer that also exists in Google Drive for that same file. That is, something like a UUID. This way, the value that would be passed to the method that performs the operation would be the "UUID" of each file and the identification would be unique.
Could someone help me find this supposed "UUID"?
Notes:
public async Task CambioNombreFicheroSeleccionados(DriveService ds, ArrayList al)
{
try
{
string r = "";
foreach (var item in al)
{
r = r + "name = '" + item + "'";
}
if (r.Length == 0) { return; };
string cadQ = r + " and trashed = false";
var drivefiles = GDrive_baj.ListFiles(ds, new GDrive_baj.FilesListOptionalParms { Q = cadQ });
foreach (var driveFile in drivefiles.Files)
{
string hhh = driveFile.Id;
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = Cg.ReemplazarCaracteresEspeciales(driveFile.Name)
};
var request2 = ds.Files.Update(fileMetadata, hhh);
var results = await request2.ExecuteAsync();
}
return;
}
catch (Exception ex)
{
string ss = ex.Message;
}
return;
}
Upvotes: 0
Views: 76
Reputation: 51
With this code I get what I wanted, that is, to update the file names in Drive and avoid the characters that Windows Explorer does not show preventing me from comparing the local names shown with the names in Drive. The solution has been to make the comparison by deleting the characters that are not shown in the Drive name (in the variable). Once it has been verified that they match, the file name update operation is performed.
At the moment the operation is performed on the files in the folder whose "id" is supplied and with a limit of 1000 files.
public async Task CambioNombreFicheroSeleccionados(DriveService ds, ArrayList al)
{
try
{
foreach (var item in al)
{
string cadQ = "parents in '_____________________________________________' and trashed = false";
Google.Apis.Drive.v3.FilesResource.ListRequest lr = new FilesResource.ListRequest(ds);
lr.PageSize = 1000;
lr.Q = cadQ;
var results = await lr.ExecuteAsync();
foreach (var driveFile in results.Files)
{
//replace known characters that are not displayed in the local Drive folder
//to compare the string displayed in Windows Explorer with the actual name in the modified Drive
string nan = driveFile.Name.ToString().Replace(":", " ");
nan = nan.Replace("*", " ");
nan = nan.Replace("/", " ");
nan = nan.Replace(((char)92).ToString(), " "); // caracter \
nan = nan.Replace(((char)34).ToString(), " "); // caracter "
nan = nan.Replace(((char)124).ToString(), " "); // caracter |
nan = nan.Replace("?", " ");
nan = nan.Replace("<", " ");
nan = nan.Replace(">", " ");
if (nan == item.ToString())
{
string Ident = driveFile.Id;
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = Cg.ReemplazarCaracteresEspeciales(driveFile.Name)
};
var request2 = ds.Files.Update(fileMetadata, Ident);
var results3 = await request2.ExecuteAsync();
}
}
}
}
catch (Exception ex)
{
string ss = ex.Message;
}
}
Upvotes: 0