通过添加增量编号创建唯一文件名

时间:2016-03-23 00:06:07

标签: filenames increment autoit

如果存在先前编号的文件名,我正在尝试增加文件名。

例如,它应检查“Example.csv”是否存在。如果是这样,新文件应该被称为“Example2.csv”,然后是“Example3.csv”,“Example4.csv”等等。到目前为止,这是我的代码:

$fileNum = 2
; The $month variable is defined earler in the script but I'll define another var for this example
$month = "January"
If FileExists("Emissions Log - " & $month & ".csv") Then
    If FileExists("Emissions Log - " & $month & $fileNum & ".csv") Then
        $fileNum += 1
        $file = FileOpen("Emissions Log - " & $month & $fileNum & ".csv", 1)
    Else
        $file = FileOpen("Emissions Log - " & $month & $fileNum & ".csv", 1)
    EndIf
Else
    $file = FileOpen("Emissions Log - " & $month & ".csv", 1)
EndIf

2 个答案:

答案 0 :(得分:2)

为此,您必须循环文件名。

$month = "January"

For $i = 0 To 1000 ;max file versions is set to 1000

    If $i = 0 Then
        $num = ''
    Else
        $num = $i
    EndIf

    If Not FileExists("Emissions Log - " & $month & $num & ".csv") then
        $file = FileOpen("Emissions Log - " & $month & $num & ".csv", 1)
        ExitLoop
    EndIf
Next

答案 1 :(得分:1)

  

如果存在先前编号的文件名,我正在尝试增加文件名。

While FileExists() StringReplace()。例如:

Func _FilenameUnique(Const $sFilenameRequested, Const $sDelimiter = '-', Const $iCountStart = 2)
    Local $iError          = 0, _
          $iCount          = $iCountStart - 1
    Local $sFilenameUnique = $sFilenameRequested

    While FileExists($sFilenameUnique) And Not $iError

        $iCount         += 1
        $sFilenameUnique = StringReplace($sFilenameRequested, '.', $sDelimiter & $iCount & '.', -1)
        $iError          = @error

    WEnd

    Return SetError($iError, $iCount, $sFilenameUnique)
EndFunc

$sFilename = _FilenameUnique('C:\path\file.csv', '')按要求运行。另外包括current time

$sFilename = 'C:\path\file_' & @YEAR & @MON & @YDAY & @HOUR & @MIN & @SEC & '.csv'