# Can I automate just importing files, changing their type, and then saving them?

**URL:** <https://discourse.slicer.org/t/can-i-automate-just-importing-files-changing-their-type-and-then-saving-them/36501>\
**Category:** Development\
**Tags:** python\
**Created:** [May 30, 2024, 3:15pm UTC](https://discourse.slicer.org/t/can-i-automate-just-importing-files-changing-their-type-and-then-saving-them/36501 "2024-05-30T15:15:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tryingToCode](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tryingtocode/32/76766_2.png) [@tryingToCode](https://discourse.slicer.org/u/tryingToCode)\
**Post date:** [May 30, 2024, 3:15pm UTC](https://discourse.slicer.org/t/can-i-automate-just-importing-files-changing-their-type-and-then-saving-them/36501/1 "2024-05-30T15:15:14Z")

</div>

I am trying to write a Python script that imports .msg files into 3D Slicer and saves them as nii files. I’ve never automated anything and I am having a bit of a hard time understanding how to write scripts and run them. Thanks.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [May 30, 2024, 7:51pm UTC](https://discourse.slicer.org/t/can-i-automate-just-importing-files-changing-their-type-and-then-saving-them/36501/2 "2024-05-30T19:51:26Z")

</div>

If you don’t need to do anything interactive with the files, then you probably don’t need to load them into Slicer at all. You might take a look at the slicerio python package and see if you can use that to just load and save the data formats you need.

---

<div class="post-metadata">

**Author:** ![tryingToCode](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tryingtocode/32/76766_2.png) [@tryingToCode](https://discourse.slicer.org/u/tryingToCode)\
**Post date:** [May 30, 2024, 11:54pm UTC](https://discourse.slicer.org/t/can-i-automate-just-importing-files-changing-their-type-and-then-saving-them/36501/3 "2024-05-30T23:54:05Z")

</div>

Thanks so much!!! I will check it out!

---

<div class="post-metadata">

**Author:** ![tryingToCode](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tryingtocode/32/76766_2.png) [@tryingToCode](https://discourse.slicer.org/u/tryingToCode)\
**Post date:** [June 3, 2024, 7:41pm UTC](https://discourse.slicer.org/t/can-i-automate-just-importing-files-changing-their-type-and-then-saving-them/36501/4 "2024-06-03T19:41:03Z")

</div>

I ended up using freesurfer’s mri\_convert command and wrote a BASH script to loop through all the files in a directory and convert them. Here’s the code:

```auto
#!/bin/bash 

#ask user for filepath and change directory to filepath 
read -p "Enter filepath: " fp  
cd $fp 

#loop through files 
for FILE in * 
do 
        #seperate file extentsion and name 
        filepath="${fp}${FILE}" 
        filename_with_ext=$(basename $filepath) 
        filename="${filename_with_ext%.*}" 
        extension="${filename_with_ext##*.}" 

        #concatinate filename with new extension         
        ni_file="${filename}.nii" 

        #convert to nii using now seperated file name concaqtinated with .nii 
        mri_convert --in_type mgz --out_type nii ${FILE} ${ni_file} 
done 

 #switch back to home directory 
cd ~ 

```
