通过添加增量数字创建唯一的文件名
Create unique filename by adding incremental number
如果存在以前编号的文件,我将尝试递增文件名。
例如,它应该检查"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
为此,您必须循环文件名。
$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
I'm trying to increment file names if a previously numbered one exists.
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'
如果存在以前编号的文件,我将尝试递增文件名。
例如,它应该检查"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
为此,您必须循环文件名。
$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
I'm trying to increment file names if a previously numbered one exists.
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'