Jump to content

Automatic renaming of copied Files if original name exists

Recommended Posts

Hi all, I apologize if this's a stupid question. I've just started messing with AutoIt recently, and have run into a minor snag.

I have a specific file I want copied to another directory on a regular basis. Seems simply at first glance; just use the FileCopy command. But I also want it to be automatically renamed if a file with that name already exists, similar to how Windows Explorer does it.

So if the file is named Example.doc, the first time it's copied it'd stay as Example.doc. Then the second time, it'd become something like Example(2).doc, and the third time Example(3).doc, and so on.

I've attempted using commands in AutoIt to automatically check if the last name in the list is taken, and if so, to use the next one. Just a bunch of IF commands. But I'd much rather have something which is more...seamless, I guess would be the word. Going back to my previous example, I'd like it to work like Windows Explorer does, where if the name currently exists, it just tacks on "Copy of", a number at the end of the name, or something. The problem comes in that I can't just set up a specific list and be done with it; I have to keep regular copies of this file, along with prior versions, for an amount of time which is unspecified, and for an unspecified amount of copies. For all I know, it could be Ten. Or Fifty. Or, god forbid, a thousand. So I'd like something which doesn't require me to constantly add new IFs to a script to make up for only thinking I'd need to keep X amount.

As I said, I'm rather new to AutoIt and most programming in general(I've dabbled in it, but nothing extensive), so if I'm missing something, I'd appreciate it being pointed out.

Thanks for your time.

Link to post
Share on other sites

Simple!!! And please refrain from making 20 topics on a few items, lol. Just post a bunch in one topic.

If FileCopy($Source, $Dest, 0) = 0 Then
Local $Track = 1
   $Track = $Track + 1
   $Dest = StringSplit($Dest, ".")
   $Dest = $Dest[1] & "(" & $Track & ")." & $Dest[2]
   $Copy = FileCopy($Source, $Dest, 0)
Until $Copy = 1

BTW whats this project your working on that u need so much support for? Just post a script support thread instead of a new support for each item that doens't work.

Only works if there are not other dots in the file name/url.

Edited by AutoIt Smith
Link to post
Share on other sites

Beat me to it...

Anyway, here's another way to do it:

Dim $filesub
While 1
    If FileExists ("Example" & $filesub & ".doc") Then
        $filesub = ($filesub + 1)
        FileCopy ("Example.txt", "Example" & $filesub & ".txt")
Link to post
Share on other sites

BTW whats this project your working on that u need so much support for? Just post a script support thread instead of a new support for each item that doens't work.

Igtenio has a total post count of one as I write this, and has not suggested in any way that they will spawn multiple threads. What on earth are you talking about? B)

For what it's worth anyway, different threads for different problems greatly aid searches.

Link to post
Share on other sites

No period problem with this.

If Not FileCopy($source, $destination) Then
    $position = StringInStr($source, '.', 0, -1)
    $filename = StringLeft($source, $position -1)
    $extension = StringMid($source, $position)
    For $i = 1 To 1000000
        If FileCopy($source, $filename & "(" & $i & ")" & $extension) Then ExitLoop
Link to post
Share on other sites

Thank you all for your help. It's been a busy week, starting a new project and all.

And to AutoIt Smith, it's alright. I do that all the time. I'll gloss over a name, and think it's another.

Anyway, the reason I need something like this is due to a new project at work. While I can't talk about it in detail(Due to NDAs, which I think are absolutely stupid with a product like this, but I don't get to give input on policies), I'm the Coordinator for the Programming portion of it, and am the one responsible for drafting the Design document.

For some reason I can't fathom, the Higher-ups want me to keep a daily update of the document, all the way to the initial draft(Which I begin working on in a week, after the current project wraps up). This's a whole new thing for me, since they haven't wanted this with past projects, and I'd rather just set a program to run every night instead of possibly screwing up, which would either A) Put the project on hold, or B) Possibly get me demoted or fired. I know, it's insane, but it's what I got told.

Thank you all for your help, once again. I'm learning as much as I can, since I'd love to have the skill to fall back on, and I'd be able to help the people I'm trying to Coordinate.

Link to post
Share on other sites

Given these requirements, you may or may not instead prefer a solution that names the file according to date:

FileCopy('C:\Path\File.doc', 'C:\Path\File Backup ' & @YEAR & '-' @MON & '-' & @MDAY & '.doc')

which would copy C:\Path\File.doc to e.g. C:\Path\File Backup 2005-11-24.doc. Also by formatting the date this way the files will always be sorted chronologically.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...