# Saving image file, location by extracting script path

**URL:** https://scilab.discourse.group/t/saving-image-file-location-by-extracting-script-path/450
**Category:** General usage
**Created:** [March 29, 2024, 4:35pm UTC](https://scilab.discourse.group/t/saving-image-file-location-by-extracting-script-path/450 "2024-03-29T16:35:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cfuttrup](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cfuttrup](https://scilab.discourse.group/u/cfuttrup)
#### Post date: [March 29, 2024, 4:35pm UTC](https://scilab.discourse.group/t/saving-image-file-location-by-extracting-script-path/450/1 "2024-03-29T16:35:04Z")

</div>

Hi,

I’m using Scilab 2024.0.0 on a Windows 10 machine.

If I execute the following code:

// Extract the filename and path of this script:  
[units,typs,nams]=file(); // nams(1) = script file name incl. path  
fpathname=strsplit(nams(1), [filesep()]);  
scriptpath = get\_absolute\_file\_path(fpathname($));

It cannot get the proper information. This means when I try to save a plot:

saveplot = %t;  
if saveplot then  
xs2png(gcf(),xs2png(gcf(),scriptpath + "" + “output.png”));  
end

Then I get the following error in the Scilab console:

xs2png: Unable to create export file, permission denied.

In the past this used to work, but I admit now it doesn’t work in older Scilab releases, like 2023.0.0 or even Scilab 6.1.0, so maybe it’s a Windows issue.  
The script is located in a user directory with write permissions. Maybe Scilab doesn’t have the user permissions?  
Maybe today there is another preferred way?

With kind regards,  
Claus

---

<div class="post-metadata">

### Author: ![cfuttrup](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cfuttrup](https://scilab.discourse.group/u/cfuttrup)
#### Post date: [March 30, 2024, 7:50am UTC](https://scilab.discourse.group/t/saving-image-file-location-by-extracting-script-path/450/2 "2024-03-30T07:50:24Z")

</div>

Hi

With a bit of help from the side, I was able to fix the problem. At some point the file() output to nams contains more variables, so it should be nams(2) instead of nams(1) … and, oh, some copy error made the xs2png(gcf() appear twice - sorry. Here is the (minimal) code that works:

```auto
saveplot = %t;
// Extract the filename and path of this script:
[units,typs,nams]=file(); // nams(2) = script file name incl. path
fpathname=strsplit(nams(2), [filesep()]);
scriptpath = get_absolute_file_path(fpathname($));
// Plot some data:
plt = scf();
plot([2 3],[4 5]);
if saveplot then 
    fnams = scriptpath + "\" + "myplot.png";
    xs2png(gcf(),fnams);
end

```

/Claus
