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

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

vbscript - Rename files without copying in same folder

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...