# Renaming uploaded files

**URL:** https://meta.discourse.org/t/renaming-uploaded-files/172854
**Category:** Support
**Created:** [December 10, 2020, 12:59pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854 "2020-12-10T12:59:06Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [December 10, 2020, 12:59pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/1 "2020-12-10T12:59:07Z")

</div>

I have recently been uploading PDF files of academic presentations (to [this topic](https://forum.openmod-initiative.org/t/2330/2) on seasonal climate forecasting as it happens).

The file naming convention I utilize is:

- `0000-leadauthor-[etal-]title-title-title.pdf`

So far so good. Until I discover I that I have a lead author’s name wrong. But when I try to upload the same file under a corrected file name, discourse notices that the file is otherwise identical — I guess there is some hashing going on here— and supplies a reference to the original file instead, with the file name unchanged and still incorrect.

My work around is to alter the following setting to `1` (the default is `48`), remove all references in the markup, and wait until the resulting file expunging has occurred:

- `Settings > Files > clean orphan uploads grace period hours`

I also tried setting this particular parameter to `0` but that did not seem to work. In which case, the tool tip might better be modified to read: “To disable the grace period set to 0”. Or alternatively as appropriate: “To enable near immediate effect set to 0”.

I cannot tell how often this kind of maintenance is actually run but I was presuming hourly? Maybe it is daily instead? That would be useful to know too.

TIA for any assistance about how to speed up this file renaming process! I do have a work around but it is rather clumsy and slow.

 ![discourse-expunge-stale-report.screenshot-20201210t123650z](https://global.discourse-cdn.com/meta/original/3X/9/a/9a7cc604f652178a575941834b994f4b816ff4b9.png)

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [December 10, 2020, 2:12pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/2 "2020-12-10T14:12:28Z")

</div>

Hourly…

> <https://github.com/discourse/discourse/blob/main/app/jobs/scheduled/clean_up_uploads.rb>

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [December 10, 2020, 2:20pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/3 "2020-12-10T14:20:41Z")

</div>

Not being familiar with ruby, but that also looks like `0` means `1`. Maybe the parameter tip should also read: “A value of 0 is interpreted as 1.” or something similar. Particularly as zero often has the special meaning of disable in other circumstances. Thanks @neounix.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 10, 2020, 2:49pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/4 "2020-12-10T14:49:04Z")

</div>

I think what you want to do is update the upload record at the rails console, if you’re self hosted. I doubt there’s a way to do that with the API, but haven’t looked

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [December 10, 2020, 3:04pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/5 "2020-12-10T15:04:59Z")

</div>

We are self-hosted. I have been offered shell account access, but have not followed that up. Tx for the hint.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 10, 2020, 4:23pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/6 "2020-12-10T16:23:40Z")

</div>

Wait. I’m not quite sure what you’re trying to do. Is it to change the name people see in the posts in which they’re uploaded? If that’s the case, then maybe you want to do something like

```plaintext
rake posts:remap["old-name.pdf","newname.pdf"]

```

As described in [Replace a string in all posts](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729). I think that for your purposes the original filename in the Upload model doesn’t really matter.

But you can change that with

```plaintext
u=Upload.find_by(original_filename: 'mypretty-w9.pdf')
u.original_filename='better-name.pdf'
u.save

```

I tried rebaking a post with that upload and nothing happened.

EDIT:

```plaintext
def rename_upload(old_name, new_name)
  u=Upload.find_by(original_filename: old_name)
  u.original_filename=new_name
  u.save
end

```

If you paste that, then you can

```
rename_upload('name.pdf','better-name.pdf')

```

It’ll fix it so that the downloads get the better filename.

In my former life as an academic I had **my** specific way of naming my PDFs that was different from anyone else’s; I’m pretty sure that my wife has her own. My guess is that no one will like your filenames even when you get them “right”. 😉

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [December 10, 2020, 4:55pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/7 "2020-12-10T16:55:04Z")

</div>

Not what I was trying to do. The `leadauthor` string was part of the file name. Nothing more. But when I revised the file name (because my understand of the family naming conventions for Spanish academics is severely limited) and tried to upload the renamed file, discourse noticed that it had an identical file in its manifest and swapped to the original target and file name. I guess that could be considered a feature? 🙄 It was not until I had purged that file from the site database that I could upload the one with the corrected file name. I really appreciate your help and interest @pfaffman.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 10, 2020, 5:05pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/8 "2020-12-10T17:05:22Z")

</div>

Where do you see the “filename”? The actual filename in the filesystem is a random string.

---

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [December 10, 2020, 5:08pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/9 "2020-12-10T17:08:33Z")

</div>

After testing in my instance, you can change the filename on `[filename.pdf|attachment](upload://filehash.pdf)` and, while it shows the filename.pdf on the post, it still downloads as the original filename. I guess it’s the same thing he wants: to rename the filename used when downloading it.

Not really critical, but I’d also like to be able to rename uploads.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 10, 2020, 5:11pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/10 "2020-12-10T17:11:04Z")

</div>

Oh. Ok. So the three lines of rails code do what you want. It doesn’t change what you see in posts, but if you click the file it saves with the “better-name.pdf”.

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [December 10, 2020, 5:18pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/11 "2020-12-10T17:18:37Z")

</div>

Thanks to everyone who responded.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [January 9, 2021, 5:18pm UTC](https://meta.discourse.org/t/renaming-uploaded-files/172854/12 "2021-01-09T17:18:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
