查找文件夹中具有最高编号的文件名 [英] Find the filename with highest number in a folder

查看:210
本文介绍了查找文件夹中具有最高编号的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多文件的文件夹,它们基于一个带有迭代数字的模式命名。

I have a folder with a lot of files, they're named based on a pattern with an iterating number in it.

我正在尝试保存新的文件通过 vb.net 。目的是将其命名为最高的文件夹数量+1

I'm trying to save new files through vb.net. The aim is to name it with the highest number of the folder +1

我在互联网上看了很多关于正则表达式和Linq的内容,这帮助我使用以下代码:

There for I looked on internet and found a lot of things about regex and Linq which helped me to make the following code :

If tmpFileName.Contains("%num%") Then
        Dim lastFileNo As Integer = 1
        Dim tmpFName = Dir(frmMain.saveLocalTFPath & "*.docx")
        Dim numbers() As Integer = Regex.Split(tmpFName, "(?<alpha>[\w-[0-9]]+)(?<num>[\d]+)").Skip(1).Select(Function(s) Integer.Parse(s)).ToArray
        For Each element In numbers
            If element > 0 And element < 999 And element > lastFileNo Then lastFileNo = element
        Next
        Do Until tmpFName = ""
            numbers = Regex.Split(tmpFName, "(?<alpha>[\w-[0-9]]+)(?<num>[\d]+)").Skip(1).Select(Function(s) Integer.Parse(s)).ToArray
            For Each element In numbers
                If element > 0 And element < 1000 And element > lastFileNo Then lastFileNo = element
            Next
            tmpFName = Dir()
        Loop
        tmpFileName = tmpFileName.Replace("%num%", lastFileNo)
    End If

但它没有按预期的方式工作。这是我在Linq和Regex中的第一个代码,我没有用来检测我的代码中有什么问题。
有人可以提供一个提示吗?

But it doesn't work as expected. Has it is my first code in Linq and in Regex and i'm not used to detect what is wrong in my code. Can someone give a hint please?

谢谢

推荐答案

p>是的__lotus你是对的,但在我的情况下匹配,而不是 match
我30分钟前发现了,但我想在发布解决方案之前使其工作。

Yes the_lotus you're right but with matches in my cases instead of match. I discovered it 30 minutes ago but I wanted to make it work before posting the solution.

感谢你,我设法组装了这个peaces,这是正确的代码:

Thanks to you I managed to assemble the peaces and this is the right code:

Dim lastFileNo As Integer = 1
Dim files() As String = Directory.GetFiles(frmMain.saveLocalTFPath, "*.docx")

For Each file As String In files
    file = Path.GetFileNameWithoutExtension(file)
    Dim numbers As MatchCollection = Regex.Matches(file, "(?<num>[\d]+)")

    For Each number In numbers
        number = CInt(number.ToString())
        If number > 0 And number < 1000 And number > lastFileNo Then lastFileNo = number
    Next
Next

这篇关于查找文件夹中具有最高编号的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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