WMI VBScript to rename files

I needed to watch a directory for incoming files, then rename the files sequentially and move them. Here's how I did it.

 int i

for i = 1 to 10000
    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colFiles = objWMIService.ExecQuery("Select * From CIM_DataFile Where Path = '\\scripts\\' and Extension = 'pdf'")

    For Each objFile in colFiles
     strNewName = objFile.Drive & objfile.path & "\\new\\" & i & ".pdf"
     errResult = objFile.copy(strNewName)  
     objfile.delete
    
    Next

    wscript.sleep 3000

    'Wscript.echo "done " & i
next

Add new comment

Anonymous limited

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.