Replace string, grab files, rename and move

Hello there! I'm having a lot of trouble writing a script.

The script is supposed to:
1) Find all files with the name "Object.mtl" within each folder in the directory: /Users/username/Desktop/convert/Objects
2) Search and replace the string ".bmp" with ".tif" (without the quotations)
3) Output the file to /Users/username/Desktop/convert/Props/ using the name of the folder the object was found in + .mtl

Example:

Found: /Users/username/Desktop/convert/Objects/Props00/Object.mtl
(Replace all ".bmp" with ".tif")
Output to: /Users/username/Desktop/convert/Props/Prop00.mtl
Found: /Users/username/Desktop/convert/Objects/Props01/Object.mtl
(Replace all ".bmp" with ".tif")
Output to: /Users/username/Desktop/convert/Props/Prop01.mtl
...And so on

Then:
4) Find all files with the name "Object.obj" within each folder in the directory: /Users/username/Desktop/convert/Objects
5) Rename the file to the folder it was found in + .obj
6) Move (or copy it doesn't really matter) to /Users/username/Desktop/convert/Props/

Example:

Found: /Users/username/Desktop/convert/Objects/Props00/Object.obj
(Rename Object.obj to Props00.obj)
Output to: /Users/username/Desktop/convert/Props/Prop00.obj
Found: /Users/username/Desktop/convert/Objects/Props01/Object.obj
(Rename Object.obj to Props01.obj)
Output to: /Users/username/Desktop/convert/Props/Prop01.obj
...And so on

Please if you could spare any help I would appreciate it! :slight_smile:
Thank you very much!

By the way:
I used to know some very basic sed/unix programming, but it has all been lost. Here is what I started to do if you would like a good laugh:

find /Users/username/Desktop/convert/Objects -name "Object.mtl" -type f -exec sh -c '\
sed 's/.bmp/.tif/g' `dirname {} `/Object.mtl > `dirname {}`/Object2.mtl; \
rm -rf {} \
' \;

You can follow below steps:-

for mtl_file in `find /Users/username/Desktop/convert/Objects -name "Object.mtl" -type f`
do
   p_dir=`echo ${mtl_file} | awk -F"/" ' { print $(NF-1) } '`
   sed 's/\.bmp/\.tif/g' ${mtl_file} > /Users/username/Desktop/convert/Props/${p_dir}.mtl
done

for obj_file in `find /Users/username/Desktop/convert/Objects -name "Object.obj" -type f`
do
   p_dir=`echo ${obj_file} | awk -F"/" ' { print $(NF-1) } '`
   mv ${obj_file} /Users/username/Desktop/convert/Props/${p_dir}.obj
done 
1 Like