从目录列表中重命名一个文件 [英] Rename single file from list in Directory

查看:236
本文介绍了从目录列表中重命名一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我的无知编程。这就是为什么你天才的存在!

Excuse my programming ignorance. This is why you geniuses exist!

我想通过附表任务可以重命名一个文件每30分钟一班。

I would like to rename a single file every 30 mins via Sched task.

文件列表:

test1.txt的
的test2.txt
test3.txt
等..

test1.txt test2.txt test3.txt ect..

进入:
的test.txt
的test2.txt
text3.txt
等..

Into: test.txt test2.txt text3.txt ect..

的的test.txt将由程序被删除。因此,在30分钟的时间,我想的test2.txt重命名为test.txt的,以此类推,直到所有的文件都已经被处理。

The test.txt will be deleted by a program. Therefore in 30 mins time I would like test2.txt to be renamed to test.txt and so on until all the files have been processed.

鸭preciate你的帮助。找到<一href=\"http://stackoverflow.com/questions/3642675/rename-different-files-to-one-file-name-one-at-a-time\">Rename在同一时间不同的文件一个文件名,之一,但它仅复制文件。

Appreciate your help. Found Rename different files to one file name, one at a time but it only copies the file.

推荐答案

您可以检查与给定基名的文件是否存在,以及以其他方式重命名追加到基名的最小数字文件。尝试是这样的:

You could check if a file with the given basename exists and otherwise rename the file with the smallest number appended to the basename. Try something like this:

Const basename  = "test"
Const srcFolder = "..."
Const extension = "txt"

Set fso = CreateObject("Scripting.FileSystemObject")

dstFile = fso.BuildPath(srcFolder, basename & "." & extension)

If fso.FileExists(dstFile) Then WScript.Quit 0  'nothing to do

For Each f In fso.GetFolder(srcFolder).Files
  If LCase(fso.GetExtensionName(f.Name)) = extension Then
    If LCase(Left(f.Name, Len(basename))) = basename Then
      num = Mid(fso.GetBaseName(f.Name), Len(basename)+1)
      If Len(num) > 0 Then
        num = CInt(num)
        If IsEmpty(minnum) Or minnum > num Then minnum = num
      End If
    End If
  End If
Next

If Not IsEmpty(minnum) Then
  srcFile = fso.BuildPath(srcFolder, basename & minnum & "." & extension)
  fso.MoveFile srcFile, dstFile
End If

上的文件名称和号码的检查可以通过对一个普通的前pression测试简化了一点:

The checks on the file name and the number could be simplified a little by testing against a regular expression:

Set re = New RegExp
re.Pattern    = "^" & basename & "(\d+)\." & extension & "$"
re.IgnoreCase = True

For Each f In fso.GetFolder(srcFolder).Files
  Set m = re.Execute(f.Name)
  If m.Count > 0 Then
    num = CInt(m(0).SubMatches(0))
    If IsEmpty(minnum) Or minnum > num Then minnum = num
  End If
Next

这篇关于从目录列表中重命名一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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