Something so simple seems so hard. I am trying to create a script that will rename a folder to "foldername#" where # is the next backup number. This is to make way for a download of the original folder from a server share.
This is what I have, it isn't working for me.
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objSysInfo = CreateObject("ADSystemInfo")Set objUser = GetObject("LDAP://" & objSysInfo.UserName)Set objNet = CreateObject("WScript.Network")Set objShell = CreateObject("Wscript.Shell")strQueryPath = "C:\Users\" & strUserName & "\AppData\Local\Lotus\Notes\Data\"strQueryFile = "Names.nsf"'======================================================================================================================================== '==== Execution Code ==================================================================================================================== '======================================================================================================================================== If objFSO.FileExists(strQueryPath & strQueryFile) Then WScript.Echo "File Exists" intBackup = 1 Do Until objFSO.FolderExists(Mid(strQueryPath, 1, Len(strQueryPath) - 1) & intBackup & "\") = False intBackup = intBackup + 1 Loop RenameFolder Mid(strQueryPath, 1, Len(strQueryPath) - 1), Mid(strQueryPath, 1, Len(strQueryPath) - 1) & intBackupEnd If'======================================================================================================================================== '===== Functions ======================================================================================================================== '======================================================================================================================================== Sub RenameFolder(strRenameFrom, strRenameTo) strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colFolders = objWMIService.ExecQuery ("Select * from Win32_Directory where name = '" & strRenameFrom & "'") For Each objFolder in colFolders errResults = objFolder.Rename(strRenameFrom & strRenameTo) Wscript.Echo errResults NextEnd Sub
It should work - I have tested with this script and it works on Win7 & XP without problem:
RenameFolder "C:\\Folder1", "C:\\Folder2"Sub RenameFolder(strRenameFrom, strRenameTo) strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name = '" & strRenameFrom & "'") For Each objFolder In colFolders errResults = objFolder.Rename(strRenameTo) Wscript.echo errResults NextEnd Sub
Ok I have also tested with the parameters you had above and it works... IF the folder is in the root directory. As soon as I put the folder I want to rename into another directory I get the same error.
So back to the original problem with a bit more information.
Hope someone can help :op
Cheers
G_M
G_M
ASKER
Thanks bp... it is defined but I forgot to add it to the code above.
errResults = objFolder.Rename(strRename
with
errResults = objFolder.Rename(strRename
and it should work.
HTH