0

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

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

File list:

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

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

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.

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

Community
  • 1
  • 1

1 Answers1

2

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

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for the prompt response! there seems to be a error on this line num = Mid(fso.GetBaseName(f.Name), Len(basename)+1)) – user2151337 Mar 09 '13 at 13:45
  • Apologies. Expected end of statement. Line 14 Char 58 – user2151337 Mar 09 '13 at 14:03
  • Ok it works for text files but how about zip extensions and base name of BULK_LOAD. Is the underscore an issue in this instance? – user2151337 Mar 09 '13 at 14:51
  • Underscore not a problem. Zip is probably treated as a Folder rather than an object is this correct? – user2151337 Mar 09 '13 at 15:52
  • @user2151337 No, zip files are still files and are treated as such. Of course you need to change the constant `extension` from `"txt"` to `"zip"`. Underscores are not a problem, as you already found out. The script looks at files with the given `extension`, whose name starts with the given `basename` followed by any number of digits. Is something particular not working as expected? – Ansgar Wiechers Mar 09 '13 at 17:35
  • Nothing is happening when running the script. I changed the Constants to match. No error message either. I created the Zip files also, so it couldn't be an access issue. I also changed the script back to "txt" and and it worked?? – user2151337 Mar 10 '13 at 08:08
  • It now works with the script above. Simple issue in that the basename was declared in Caps! changed this to lower case and all is ok. Script works as you intended. Massive Apologies for my oversight. – user2151337 Mar 10 '13 at 10:15