Applescript help: Move files to folders according to a CSV file - csv

I am looking for an AppleScript that moves files in a single directory into new folders based on info provided in a CSV file, where the columns are A) Filename with extension B) New folder title. Thank you.

Related

Move files to folders based on value in .csv file using .bat file

I need to move .jpg images from one folder to another using listing from CSV which changes periodically.
I need to move .jpg images from one folder into sub folders based on a value in a CSV file which periodically changes.
For example I have a folder called D:\photos which folder contains numbered sub folders i.e.
D:\photos\1000
D:\photos\1001
D:\photos\1002
Now in my folder D:\photos There are three files named a.jpg, b.jpb, c.jpb where the output.csv looks like following
Output
1001
So as per CSV I have to move mention above files into d:\photos1001 which will be removed form actually folder.
Next in my folder D:\photos there are 3 files. a.jpg, b.jpg and c.jpg
and output.csv contains following information.
Output
1002
In this case I need to move the files a.jpg, b.jpg and c.jpg into D:\photos1002 then delete the files from D:\photos
Or another way I can change files in 1002_a.jpg format too in the D:\photosfolder.
Can anyone help me out with creating a .bat file that does this?
Move all .csv file from one folder to another folder
#echo off
move E:\folder_from*csv*.* E:\folder_to

How to delete a file (deep) in a ZIP folder in Python?

I'm writing a script that is extracting 2 JSON files from deep (3 folders down) in a ZIP Folder, modifying its contents, and writing those files back into that same folder. The only issue is that when I write the JSON Files back into the folder where they were initially pulled from, they do not overwrite so now I have 4 files named (product, product, package, package). I wish to delete or overwrite the non-modified files but don't know how.
I have looked up how to do it on other threads and it says it's not supported but I was hoping by now someone had addressed this without having to do the "rewrite the entire directory minus the old files into a new ZIP" routine. I've also tried writing to the zip file with 'w' mode but that just deletes the whole other contents of the zip file.
This is how I'm writing each file back into the ZIP file
zf = zipfile.ZipFile(ZipName, 'a')
zf.write('package.json',addressBook[0],compress_type = zipfile.ZIP_DEFLATED
zf.write('product.json',addressBook[1],compress_type = zipfile.ZIP_DEFLATED

Can copy file, can't add it Google script

In a Google script, I have a folder object (testFolder) and a file object (testFile). I have these two lines of code:
testFolder.addFile(testFile);
testFile.makeCopy('this is a copy', testFolder);
The second line correctly copies the file into the folder.
The first line seems to do nothing. I'm expecting it to add a reference to the file and place it in the folder.
I obviously have the correct objects and I am the owner of the file and the folder, so any other ideas?
Thanks
You can make a copy create new file in Google Drive. But one cannot simply move a file into a folder. You have to make a copy into the desired folder and then remove the old file. Or you have to directly create the file in the desired folder location.
What is your code for getting the 'testFile' and 'testFolder' objects? Are you referencing them by ID or by name? If you have multiple folders of the same name in your Google Drive, this could be causing the issue. Here's the script that worked for me
var file = DriveApp.getFileById('YOUR_FILE_ID');
var folder1 = DriveApp.getFolderById("YOUR_FOLDER1_ID");
var folder2 = DriveApp.getFolderById("YOUR_FOLDER2_ID");
file.makeCopy('another copy', folder1);
folder1.addFile(file);

When adding a file to a folder, how to make it not default save in Google Drive?

In my script I have template docs in the main drive, but when the copy is created and populated with my excel data, I want it to be saved in a folder in drive. I have the script do that, but it has one copy of the file in the Main Google Drive, and the other in the folder I want it in. If I delete one of the copies, it deletes both.
Is there any way I can have it save automatically in the specified folder without also being in the main drive folder?
Folders in Google drive are not exactly like forders in a computer : having the file in your 'root' folder and in another folder doesn't mean there are 2 files, but rather 1 and only file with 2 labels... that's why you can't delete one without deleting the other !
The solution is simply to play with these labels in the script, here is how it works : (I commented each step to make it clear.)
function othertest(){
folder=DocsList.createFolder("MyFolder"); // or getFolderById or whatever other way to get your target folder
var file=DocsList.createFile('File2', 'Empty');// just an empty file for test but this would be your file copy that you want to "move"
file.addToFolder(folder);// put it in the folder
file.removeFromFolder(DocsList.getRootFolder());// and remove from the root
}
The other possible solution is to create the file directly in the target folder since the folder object supports the createFile method. (Not sure though that you can do it in your specific use case)
here is an example, you can see that the file is not in the root folder.
function createFileinFoldertest() {
var folder = DocsList.getFolder('test')
folder.createFile('Empty test fileName','nothing in there')
}

Issue in creating Zip file using glob.glob

I am creating a Zip file from a folder (and subfolders). it works fine and creates a new .zip file also but I am having an issue while using glob.glob. It is reading all files from the desired folder (source folder) and writing to the new zip file but the problem is that it is, however, adding subdirectories, but not adding files form the subdirectories.
I am giving user an option to select the filename and path as well as filetype also (Zip or Tar). I don;t get any problem while creating .tar.gz file, but when use creates .zip file, this problem comes across.
Here is my code:
for name in (Source_Dir):
for name in glob.glob("/path/to/source/dir/*" ):
myZip.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
myZip.close()
Also, if I use code below:
for dirpath, dirnames, filenames in os.walk(Source_Dir):
myZip.write(os.path.join(dirpath, filename) os.path.basename(filename))
myZip.close()
Now the 2nd code taks all files even if it inside the folder/ subfolders, creates a new .zip file and write to it without any directory strucure. It even does not take dir structure for main folder and simply write all files from main dir or subdir to that .zip file.
Can anyone please help me or suggest me. I would prefer glob.glob rather than the 2nd option to use.
Thanks in advance.
Regards,
Akash
Glob by design does not expand into subdirectories. It follows UNIX style path rules and expansions see the documentation for fnmatch for more information. If you want to get at the subdirectories you need to add it to the path. This example will get everything at one level down.
for name in (Source_Dir):
for name in glob.glob("/path/to/source/dir/*/*" ):
myZip.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
myZip.close()
Doug Hellman has an excellent discussion here. If you are not using the pattern features of glob (like *.txt for all text files or *[0-9].txt for all text files that have a number before the extension) then I think your os.walk solution is better