# Add string to file name

**URL:** <https://discourse.processing.org/t/add-string-to-file-name/33774>\
**Category:** Coding Questions\
**Created:** [November 26, 2021, 6:16pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774 "2021-11-26T18:16:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 26, 2021, 6:16pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/1 "2021-11-26T18:16:13Z")

</div>

I have a loop which create graphics and save them into jpg files,  
and save them as  
saveFrame(“C:\process\test\colors-###.jpg”);

I create the graphics from randomize list of colors, and if the color red was chosen,  
I want the file to be saved as colors-###-red.jpg

Is that possible?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 26, 2021, 7:47pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/2 "2021-11-26T19:47:02Z")

</div>

Hello,

> [@yalaniv](#):
>
> colors-###-red.jpg

what about colors-red-###.jpg ? Might be easier. (the other thing is also possible)

Than you only need an if-clause to distinguish I guess:

```auto
if(chosenColor==color(255,0,0)) {
   saveFrame(“C:\process\test\colors-red-###.jpg”);
}
else {
   saveFrame(“C:\process\test\colors-###.jpg”);
}

```

_not tested_

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 28, 2021, 4:53pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/3 "2021-11-28T16:53:27Z")

</div>

Thanks for your reply, but it won’t do the job I need, because it will change the order of the files,  
but I got an idea for different problem I had, in different project, so… thanks 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 29, 2021, 11:54am UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/4 "2021-11-29T11:54:50Z")

</div>

You can sort by date

But you can of course make another way to enumerate the files

Instead of using the inbuilt automatically numeration

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 29, 2021, 12:28pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/5 "2021-11-29T12:28:26Z")

</div>

sort by date won’t help cause I need to upload them to a platform sort only by name

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [November 29, 2021, 12:29pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/6 "2021-11-29T12:29:14Z")

</div>

However, if I am using my own variable,  
Is there an option to use string as a file name?

---

<div class="post-metadata">

**Author:** ![philipplehmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philipplehmann/32/4111_2.png) [@philipplehmann](https://discourse.processing.org/u/philipplehmann)\
**Post date:** [November 29, 2021, 1:24pm UTC](https://discourse.processing.org/t/add-string-to-file-name/33774/7 "2021-11-29T13:24:46Z")

</div>

Tested on macos

```auto
//global
int num = 0;
color chosenColor;

void setup() {
  
  size(200,299);
  
}

void draw() {
  
}

void mouseClicked() {
  
  //local
  num++;
  String dir = "path/to/dir/";
  
  if(chosenColor==color(255,0,0)) {
     saveFrame(dir + "colors-" + nf(num, 3) + "-red.jpg");
  }
  else {
     saveFrame(dir + "colors-" + nf(num, 3) + ".jpg");
  }

}

```
