重命名文件而不复制到同一文件夹中

重命名文件而不复制到同一文件夹中

本文介绍了重命名文件而不复制到同一文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下命名约定的文件.

I have files with following naming convention.

RE12356_GJ123456789.DAT

RE12356_GJ123456789.DAT

我需要将文件重命名为 RE12356_GJ123456790.DAT 而不使用 VBS 复制文件,即每次运行 VBS 文件时我都需要增加 1.请帮我.谢谢!

I need to rename the file to RE12356_GJ123456790.DAT without copying the files using VBS i.e. I need to increment by 1, everytime when I run VBS file. Please help me. Thanks!

推荐答案

FileSystemObject 有一个方法 .GetFile(FileSpec) 返回文件 FileSpec 的对象.这些对象有一个(可写的) .Name 属性.所以获取 .Name,修改它,然后写入/分配新的.

The FileSystemObject has a method .GetFile(FileSpec) that returns an object for the file FileSpec. Those objects have a (writable) .Name property. So get the .Name, modify it, and write/assign the new one.

给你一些想法(遍历文件夹中的文件,找到要更改的文件,提取数字以递增):

To give you some ideas (looping over the files in a folder, finding the file(s) to change, extracting the number to increment):

Option Explicit

Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )

WScript.Quit demoMain()

Function demoMain()
  demoMain = 0 ' assume success

  Dim sDDir    : sDDir        = goFS.GetAbsolutePathName(".\")
  Dim reVictim : Set reVictim = New RegExp
  reVictim.IgnoreCase = True
  reVictim.Pattern    = "^(victim)(\d+)(\.txt)$"
  Dim oFile
  For Each oFile In goFS.GetFolder(sDDir).Files
      If reVictim.Test(oFile.Name) Then
         WScript.Echo "found:  ", oFile.Name
         oFile.Name = reVictim.Replace(oFile.Name, GetRef("FINC"))
         WScript.Echo "renamed:", oFile.Name
      End If
  Next
End Function ' demoMain

Function FINC(sM, sG1, sG2, sG3, nP, sS)
  FINC = sG1 & Right(100 + sG2 + 1, 2) & sG3
End Function

输出:

cscript finc.vbs
found:   victim00.txt
renamed: victim01.txt

cscript finc.vbs
found:   victim01.txt
renamed: victim02.txt

cscript finc.vbs
found:   victim02.txt
renamed: victim03.txt

如何在计数器溢出的情况下进行复制留作练习.

How to copy with overflow of the counter is left as exercise.

这篇关于重命名文件而不复制到同一文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:26