# Why doesn't my file get renamed while copying?

**URL:** <https://discuss.gradle.org/t/why-doesnt-my-file-get-renamed-while-copying/4684>\
**Category:** Old Forum Archive\
**Created:** [April 12, 2013, 12:21pm UTC](https://discuss.gradle.org/t/why-doesnt-my-file-get-renamed-while-copying/4684 "2013-04-12T12:21:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Singray](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Joe\_Singray](https://discuss.gradle.org/u/Joe_Singray)\
**Post date:** [April 12, 2013, 12:21pm UTC](https://discuss.gradle.org/t/why-doesnt-my-file-get-renamed-while-copying/4684/1 "2013-04-12T12:21:00Z")

</div>

Hello all,

Currently I have the following task:

```gradle
task testBackup(type: Copy) {
    from '.../testfile.xml'
    into '.../Backups/'
    rename { String fileName ->
        fileName -= ".xml"
        fileName += "-" + new Date().format('dd-MM-yyyy-hh:mm:ss') + ".xml"
        println "fileName: " + fileName
    }
}

```

The problem is, the println displays the correct name in the terminal, but when I go check out the file, it’s still the same name as the original. Am I forgetting something here? I’m sure it’s small and I’m just not seeing it.

Thanks!

~ Joe

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [April 12, 2013, 12:28pm UTC](https://discuss.gradle.org/t/why-doesnt-my-file-get-renamed-while-copying/4684/2 "2013-04-12T12:28:00Z")

</div>

Strings are immutable; operations like ‘+=’ return new Strings. Hence you have to return the new filename from the closure, either by adding an explicit ‘return fileName’, or by removing the ‘println’ (in which case the previous line becomes an implicit return).

---

<div class="post-metadata">

**Author:** ![Joe\_Singray](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Joe\_Singray](https://discuss.gradle.org/u/Joe_Singray)\
**Post date:** [April 12, 2013, 12:32pm UTC](https://discuss.gradle.org/t/why-doesnt-my-file-get-renamed-while-copying/4684/3 "2013-04-12T12:32:00Z")

</div>

I didn’t know these kind of operations returned new Strings. Thanks for that! For the interested, this is the task I ended up with (24hr notation):

```gradle
task testBackup(type: Copy) {
    from '.../testfile.xml'
    into '.../Backups/'
    rename { String fileName ->
        fileName -= ".xml"
        fileName += "-" + new Date().format('dd-MM-yyyy-HH:mm:ss') + ".xml"
        println "fileName: " + fileName
        return fileName
    }
}

```
