通过添加增量编号来创建唯一的文件名 [英] Create unique filename by adding incremental number

查看:96
本文介绍了通过添加增量编号来创建唯一的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to increment file names if a previously numbered one exists.

例如,它应该检查"Example.csv"是否存在.如果是这样,则新文件应命名为"Example2.csv",然后命名为"Example3.csv","Example4.csv",依此类推.到目前为止,这是我的代码:

For example, it should check if "Example.csv" exists. If so, the new file should be called "Example2.csv", then "Example3.csv", "Example4.csv" and so on. Here's my code so far:

$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

推荐答案

为此,必须循环使用文件名.

In order to do that, you must loop the filenames.

$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

这篇关于通过添加增量编号来创建唯一的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆