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!
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!
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.
You can just use the "move" command which will not actually rewrite the file but will just move the entry in the file table... which is essentially a "rename"
If you don't want to use filesystemobject.move you could issue a command line rename command via: Wsript.shell
like...
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c rename somefile.txt newname.txt"
Set objShell = Nothing
or as Ekkehard.Horner pointed out:
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
f.name = "newFileName.txt"