My "folder 3" contains "Folder 4A", but when I made a new folder within it, "Folder 4B", my "Folder 4A", and all it's contents got deleted. What is wrong with my code, or is this normal for FileUtils.mkdir_p
FileUtils.mkdir_p "/Users/Me/Folder 1/Folder 2/Folder 3/Folder 4B"
Related
I'm not a newbie to (or an expert on) scripting and programming, but after a few hours, I am stumped at how to copy a file (after having googled and tried various things for a few hours) using AppleScript in this situation.
I want to move a file from the most recently modified folder one level up. I'm trying to move the file:
/Users/tsg/my downloads folder/newest folder/pdfs/target1.pdf
to
/Users/tsg/my downloads folder/newest folder/target1.pdf
Where "newest folder" is not a constant name, but will change as I download newer folders.
When I run the code below,
tell application "Finder"
set downloadFolder to POSIX file "/Users/tsg/my downloads folder" as alias
set latestModifiedFolder to (downloadFolder & (name of last item of (sort every folder of downloadFolder by modification date)))
set targetFolder to latestModifiedFolder (* Is this necessary? *)
set pdfFolder to (targetFolder & ":pdfs:")
set pdfFile to (pdfFolder & ("target1.pdf"))
move pdfFile to targetFolder
end tell
I get the error
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type folder." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to folder
If I add the modifier folder: "move pdfFile to folder targetFolder" for the last line before "end tell", I get
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to integer"
If I add the modifier file "move file pdfFile to folder targetFolder", I get
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} to integer
If I remove the "move" line altogether, I get the result:
{alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"}
I would actually like to find the file based on a search term, declared as a property (Property searchWord : {"target"}), rather than hard coded, but I couldn't get that to work either.
Code:
set pdfFile to first file of pdfFolder whose name contains searchWord
Error:
"Can’t get file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}." number -1728 from file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}
There may be more than one pdf containing the word "target", and I'd actually like to move all files containing the search word up one level, and then rename them pdfs.one.pdf and pdfs.two.pdf.
Thanks in advance for any help.
The errors occur because you try to combine multiple incompatible classes.
When dealing with the Finder the Finder specifier syntax
file "foo" of folder "bar" of folder "baz" of startup disk
is a reasonable way to avoid those cannot-do-this errors
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFile to file "target1.pdf" of pdfFolder
move pdfFile to targetFolder
end tell
or to move all files containing "target" in the file name
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFiles to files of pdfFolder whose name contains "target"
move pdfFiles to targetFolder
end tell
:)
Let's say in the file path, C:\Users\CurrentUser\Documents\Records, there are several folders.
I want to rename all the files in all the folders by adding their respective folder names in front of their current file names.
For example, folder 1 and folder 2 exists in C:\Users\CurrentUser\Documents\Records. All the files in folder 1 should be renamed with "folder 1 " added in front. And all the files in folder 2 should be renamed with "folder 2 " added in front. So something like the file "Invoice" in folder 1 being renamed to "folder 1 Invoice". Same thing with folder 2.
Is there a way to do this in Powershell? Any help is much appreciated! :)
I think this should do it:
(Get-ChildItem -Path 'C:\Users\CurrentUser\Documents\Records' -File -Recurse) |
Rename-Item -NewName {'{0} {1}' -f $_.Directory.Name, $_.Name} -WhatIf
The -WhatIf switch is a precaution. With that added, the code wil only show in the console what would happen. If you are satisfied with that, remove the -Whatif switch to actually start renaming the files.
I have this file structure:
files
-- folder1
---- text.json
-- folder2
---- text.json
-- folder3
---- text.json
I'd like to prepend the folder name before each text.json, so I would get this in the end:
files
-- folder1
---- folder1_text.json
-- folder2
---- folder2_text.json
-- folder3
---- folder3_text.json
But I have no idea how to do this..
Thanks for your help
I don't know anything in bash but needed to do this task, so I wrote this mini script.
To run it, copy/paste it into the Terminal or create a file.sh with the code inside.
notes:
- Don't forget to replace path variable with your parent folder path.
- I advise you to try first with a test folder.
#!/bin/bash
path=~/Documents/yourparentfolder/
for folder in "$path"/*; do
foldername=${folder##*/}
for file in "$path"/"$foldername"/*; do
filename=${file##*/}
mv "$path"/"$foldername"/"$filename" "$path"/"$foldername"/"$foldername"_"$filename"
done
echo $foldername files renamed!
done
echo finished!
Commented version:
#!/bin/bash
# path of parent folder
path=~/Documents/yourparentfolder/
# for each folder in parent folder
for folder in "$path"/*; do
# get folder name
foldername=${folder##*/}
# for each file in folder
for file in "$path"/"$foldername"/*; do
# get file name
filename=${file##*/}
# rename path/folder/filename to path/folder/folder_filename
mv "$path"/"$foldername"/"$filename" "$path"/"$foldername"/"$foldername"_"$filename"
done
echo $foldername files renamed!
done
echo finished!
Hope to have helped you.
I'm in a bit of a bind and an Applescript noob. I've been stumbling through a script that would allow me to copy the contents of (TemplateFolder) into all of the client folders in a directory, with the added crux that all of the existing clients' folder contents be moved into a folder within the client labeled "Old Files" or something of the sort. I'll include images with my script thus far to illustrate what I'm trying to achieve. Thank you everyone in advance for all your help.
P.S. Since I don't have the rep required, I'll have to post links to my images.
http://imgur.com/a/lL9q7
The first image is the template folder (the folder I'd like all contents copied into each client folder).
The second image is an example of an existing client folder with all the bad structure (or lack thereof).
The final image is the expected results where the template folder's contents are moved into the client folder and the original content of the client's folder are moved into a separate folder titled "Old Structure Files".
Below is the applescript I've written, with help from others, to copy the contents. However there are missing components and some elements that need to change; currently the applescript simply copies the entire folder rather than just the contents and it makes a simple copy rather than inserting, the script is not recursive for an entire directory, and there's no function to move the existing client files into a "Old Structure Files" folder. Again, any and all help is greatly appreciated :)
on run
set source_folder to choose folder with prompt "Select folder to be duplicated:" as string
my do_main_script(source_folder)
end run
on open of source_folder_list
repeat with i from 1 to number of items in the source_folder_list
set this_folder_path to item i of the source_folder_list as string
if last character of this_folder_path is ":" then
my do_main_script(this_folder_path)
end if
end repeat
end open
on do_main_script(source_folder)
tell application "Finder" to set source_folder to folder (source_folder)
tell application "Finder" to set the target_folder to (parent of source_folder)
if source_folder is not "" and target_folder is not "" then
set new_folder_name to (name of source_folder as string) & " duplicate"
set source_folder to source_folder as string
set target_folder to target_folder as string
my create_new_folder(target_folder, new_folder_name)
my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")
end if
end do_main_script
In the script bellow, the template folder is at fixed place on desktop, all the content of the customer selected folder is moved to a new folder "old structure", and after that, all content of the template folder is duplicated into the customer folder. If I well understand, this is what you're looking for :
set Templatefolder to "HD:Users:my_user:Desktop:Template"
set CustomerFolder to (choose folder with prompt "Select customer folder to be re-organised")
tell application "Finder"
set CustomerData to every item of CustomerFolder
set OldF to make new folder in CustomerFolder with properties {name:"Old Strutucture Files"}
move CustomerData to OldF
set Newstructure to every item of folder Templatefolder
duplicate Newstructure to CustomerFolder
end tell
Suppose I have the following files in folder "A":
"AAAAA 1x1", "AAAAA 1x2", "BBBBB 1x1", "BBBBB 1x2", "CCCCC 1x1", "CCCCC 1x2".
And in folder "B", I have the following folders:
"AAAAA", "BBBBB", "CCCCC".
What I'd like to do is move all the "AAAAA" files in folder "A", to folder "AAAAA" in folder "B", "BBBBB" files to folder "BBBBB", and so on.
How would I do this using Apple Script?
Try running a command like this in Terminal:
for f in A/*; do echo mv "$f" B/${f:2:5}; done
Remove echo to actually move the files.
Try:
set folderA to "/Users/Joao/Desktop/A"
set folderB to "/Users/Joao/Desktop/B"
tell application "System Events"
set subFolders to folders of (folder folderB)
repeat with subfolderB in subFolders
move (files of folder folderA whose name starts with (name of subfolderB)) to (path of subfolderB)
end repeat
end tell
Here's the code (thanks to some fine folks at MacScripter):
set sourceFolder to alias "SSD:Users:JPCanaverde:Documents:A"
set destinationFolder to alias "SSD:Users:JPCanaverde:Documents:B"
tell application "Finder"
repeat with aFolder in (get folders of destinationFolder)
set folderName to name of aFolder
set filesToMove to (files of sourceFolder whose name begins with folderName)
move filesToMove to (contents of aFolder)
end repeat
end tell