0
votes

I have a directory full of folders whose subfolders each contain a .xml file. They are all supposed to be named apple.xml but unfortunately, they each have different names such as apple_1.xml or appleTEST.xml and other unique variations that don't follow a specific pattern. Luckily, there is only one .xml file per folder.

I was hoping there was a way to use Applescript to search an entire directory and all of its folders for files with the .xml extension and rename them all to just apple.xml?

Thanks

2
I'd just do it from Terminal using the following compound command, IFS=; while read -r f; do echo mv -v "$f" "${f%/*}/apple.xml"; done <<< "$(find "/path/to/parent_XML_dir" -type f -iname '*.xml')" while changing "/path/to/parent_XML_dir" to the proper pathname. Run it the first time still with the echo command in front of the mv command to see what the mv command would look like. If you like the output, remove the echo command and run it again to rename the files. NOTE: Always insure you have a backup before running commands that modify the filesystem! - user3439894
@user3439894 - That fails when part of a pathname includes a newline character(s). See how to find & safely handle file names containing newlines. Use the -print0 option with find to ensure each pathname is delimited with a NUL byte (\0) before redirecting to a while loop. To read the NUL char in a while loop add the -d '' option to read. E.g.: while IFS= read -rd '' f; do echo mv -v "$f" "${f%/*}/apple.xml"; done < <(find "/path/to/parent_XML_dir" -type f -iname '*.xml' -print0) - RobC
Demo: 1. Create test files, run: mkdir -p ~/Desktop/foo/{a..c} && touch ~/Desktop/foo/{a..b}/apple_1.xml && touch ~/Desktop/foo/c/$'apple\napple.xml' - note in folder c the multi-line filename. 2. Run your cmd: IFS=; while read -r f; do mv -v "$f" "${f%/*}/apple.xml"; done <<< "$(find ~/Desktop/foo -type f -iname '*.xml')" - note, the file in folder c` has not been renamed. 3. Clean up, run rm -rf ~/Desktop/foo and repeat point 1. 4. Run while IFS= read -rd '' f; do mv -v "$f" "${f%/*}/apple.xml"; done < <(find ~/Desktop/foo -type f -iname '*.xml' -print0) - RobC

2 Answers

1
votes

This AppleScript code is pretty much self-explanatory and should work for you.

-- Choose Parent Folder To Search For .xml Files
set searchFolder to POSIX path of (choose folder)

-- Get Paths Of All .xml Files In The Parent Folder And All Sub Folders
set xmlFiles to do shell script "find " & (quoted form of searchFolder) & " -name *.xml"

-- Create A List From The Result
set xmlFiles to paragraphs of xmlFiles as list

-- Loops Through The List And Renames Each File
repeat with i in xmlFiles
    tell application "System Events" to set name of disk item i to "apple.xml"
end repeat
1
votes

Because you've stated there is only one XML file in each sub folder, it makes it extremely easy and quick to do with AppleScript.

The following example AppleScript code will do as you've asked, after you select the Parent XML Folder when prompted, by running this code in, e.g., Script Editor.

tell application "Finder"
    set xmlFilesList to a reference to ¬
        (every file of entire contents of ¬
            (choose folder with prompt "Please Select Parent XML Folder:") ¬
                whose name extension is "xml")
    set name of xmlFilesList to "apple.xml"
end tell

This example AppleScript code is simple and fast in execution, and providing you select the proper Parent XML Folder there should not be any issues. NOTE: Always insure you have a backup before running commands that modify the filesystem!