Jump to content

FileMove And iF File Exists Rename


Recommended Posts

  • Moderators

Was that a request, a demand, or a question?

Anyway, I just happened to have wrote something like this last week... lucky you.

_myFileMove(@DesktopDir & "\test.au3", @DesktopDir & "\test.au3.bak", 16)

Func _myFileMove($sSource, $sDestination, $iFlag = 0)
    ; $iFlag; $FC_NOOVERWRITEENUM = 16 ... This doesn't exist, so magic numbers are used

    Local $iRet
    If Not (BitAND($iFlag, 16) = 16) Then
        $iRet = FileMove($sSource, $sDestination, $iFlag)
        Return SetError(@error, @extended, $iRet)
    EndIf

    $iFlag = BitXOR($iFlag, 16)
    If Not FileExists($sDestination) Then
        $iRet = FileMove($sSource, $sDestination, $iFlag)
        Return SetError(@error, @extended, $iRet)
    EndIf

    Local $aParts = StringRegExp($sDestination, "^(.+?)(\.?\w+)\z", 3)
    Local $sFile, $iCC = 1
    Do
        $sFile = $aParts[0] & "(" & $iCc & ")" & $aParts[1]
        $iCc += 1
    Until Not FileExists($sFile)

    $iRet = FileMove($sSource, $sFile, $iFlag)
    Return SetError(@error, @extended, $iRet)
EndFunc 

Edit:

Forgot to enum my counter in the loop

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

?

#include <File.au3>

$source = @DesktopDir & "\test.au3"
$myfolder = @DesktopDir & "\testdir\"
$dest = $myfolder & "\test.au3"

_myFileMove($source, $dest)

Func _myFileMove($sSource, $sDestination)
 Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
 Local $aPathSplit = _PathSplit($sDestination, $sDrive, $sDir, $sFilename, $sExtension)
 Local $iNum, $newfile = $sDestination
 While FileExists($newfile) 
    $iNum += 1
    $newfile = $sDrive & "\" & $sDir & "\" & $sFilename & " (" & $iNum & ")" & $sExtension
 Wend
 If FileMove($sSource, $newfile, 8) Then Return 1
EndFunc
Link to comment
Share on other sites

Was that a request, a demand, or a question?

Anyway, I just happened to have wrote something like this last week... lucky you.

_myFileMove(@DesktopDir & "\test.au3", @DesktopDir & "\test.au3.bak", 16)

Func _myFileMove($sSource, $sDestination, $iFlag = 0)
    ; $iFlag; $FC_NOOVERWRITEENUM = 16 ... This doesn't exist, so magic numbers are used

    Local $iRet
    If Not (BitAND($iFlag, 16) = 16) Then
        $iRet = FileMove($sSource, $sDestination, $iFlag)
        Return SetError(@error, @extended, $iRet)
    EndIf

    $iFlag = BitXOR($iFlag, 16)
    If Not FileExists($sDestination) Then
        $iRet = FileMove($sSource, $sDestination, $iFlag)
        Return SetError(@error, @extended, $iRet)
    EndIf

    Local $aParts = StringRegExp($sDestination, "^(.+?)(\.?\w+)\z", 3)
    Local $sFile, $iCC = 1
    Do
        $sFile = $aParts[0] & "(" & $iCc & ")" & $aParts[1]
        $iCc += 1
    Until Not FileExists($sFile)

    $iRet = FileMove($sSource, $sFile, $iFlag)
    Return SetError(@error, @extended, $iRet)
EndFunc 

Edit:

Forgot to enum my counter in the loop

 

 

?

#include <File.au3>

$source = @DesktopDir & "\test.au3"
$myfolder = @DesktopDir & "\testdir\"
$dest = $myfolder & "\test.au3"

_myFileMove($source, $dest)

Func _myFileMove($sSource, $sDestination)
 Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
 Local $aPathSplit = _PathSplit($sDestination, $sDrive, $sDir, $sFilename, $sExtension)
 Local $iNum, $newfile = $sDestination
 While FileExists($newfile) 
    $iNum += 1
    $newfile = $sDrive & "\" & $sDir & "\" & $sFilename & " (" & $iNum & ")" & $sExtension
 Wend
 If FileMove($sSource, $newfile, 8) Then Return 1
EndFunc

 

 

iT's Working ... Tnx
Link to comment
Share on other sites

You are indeed lucky, as I wouldn't have even been as illuminating as markyrocks, not without you having provided some basic code first.

I wonder if you and other requestors realize just how lucky you are sometimes, at the brilliant helpers that provide at this forum?

Akin to getting breakfast in bed.

I do often wonder about the existence of the Help file and Wiki though, and what they are for. o:)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

TheSaint,

You are totally right

I had this code on hand though and a copy/paste with some minor modifs was less time consuming than providing explanations  :>
But it would probably have been different in case of an entire to-do script

Getting breakfast in bed is like Xmas, it's very sweet while it occurs once a year  :)

Edited by mikell
Link to comment
Share on other sites

The thing is very complicated because of the file extension (that sometimes does not exist) etc etc, the safe way is _FileExistsEx '?do=embed' frameborder='0' data-embedContent>>

; #FUNCTION# ====================================================================================================================
; Name...........: _FileExistsEx
; Description ...: Get New Files Name
; Syntax.........: _FileExistsEx(ByRef $sFilePath[, $iFileExists])
; Parameters ....: $sFilePath   - The Fullpath file
;                 $iFileExists - Optional
; Author ........: DXRW4E
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _FileExistsEx(ByRef $sFilePath, $iFileExists = 0)
    While FileExists($sFilePath)
        $iFileExists += 1
        $sFilePath = StringRegExpReplace($sFilePath & " ", "( - \(\d+\))?(\.[^\.\\]*)?(\h)$", " - (" & $iFileExists & ")$2")
    WEnd
EndFunc
Local $sFilePath = @DesktopDir & "\FileName1.xxx"
_FileExistsEx($sFilePath)
;if the file exists - $sFilePath = @DesktopDir & "\FileName1 (1).xxx"

_FileExistsEx($sFilePath, 7)
;if the file exists - $sFilePath = @DesktopDir & "\FileName1 (8).xxx"
Ciao. Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...