Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

When moving files, and a file already exists, add a number to it's name

$
0
0
G'day everyone

I'm writing a script that will move files on my hard drive.  In 99% of cases, there will not be a file in the target directory with the same name as the file that I'm moving towards that directory, but in 1% of cases, there will be an existing file in that directory with the same name.  Instead of either skipping the file or replacing it, I would like to rename it with a number.  Is that easily done?

I can easily rename the file using a date/time stamp (_NowCalc), or with a random number, but it will look more elegant if I can simply rename it with a single digit, that increments by 1 every time the same file name is being copied again.

It doesn't matter to me which file is renamed -- the existing file or the file that I'm moving -- as long as it becomes possible for both files to be in the same directory with highly similar names.

For example, if I move "file.txt" to a folder and "file.txt" already exists, I'd like to rename one of them to "file1.txt", and the next time I try to move another "file.txt" to that folder, I'd like to rename one of them "file2.txt", etc.

Tis a pity FileExists doesn't take regex :-)

Added:  I'm trying to see if I can use _FileListToArray for this, but _FileListToArray only supports wildcards within the actual file name, and in my script the file name is a variable.  How does one combine variables with wildcards?  For example, if $filename1stpart is "foo" and $filename2ndpart is "bar", how can I specify "foo*bar" in _FileListToArray?

Thanks
Samuel

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>