Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have files with following naming convention.

RE12356_GJ123456789.DAT

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
683 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

output:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...