Link to home
Start Free TrialLog in
Avatar of G_M
G_MFlag for Australia

asked on

VBScript - Rename Folder

Hey guys,

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) & intBackup

End 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
	Next

End Sub

Open in new window


Hope someone can help.
Cheers
G_M
Avatar of Lucian Constantin
Lucian Constantin
Flag of Romania image

The Rename method have only one parameter - the new name, so replace the

errResults = objFolder.Rename(strRenameFrom & strRenameTo)

with

errResults = objFolder.Rename(strRenameTo)

and it should work.

HTH
Avatar of G_M

ASKER

I'm getting a 0x80041017 error on the second character of the following line:

For Each objFolder in colFolders

Open in new window


Doesn't change after making you mod.

Cheers
G_M
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
    Next

End Sub

Open in new window


Could you make a test only with this part?... maybe the problem is in another part of your script.
Avatar of Bill Prew
Bill Prew

Looking quickly, I see this line:

strQueryPath = "C:\Users\" & strUserName & "\AppData\Local\Lotus\Notes\Data\"

But I don't see strUserName defined before this?

~bp
Avatar of G_M

ASKER

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
Avatar of G_M

ASKER

Thanks bp... it is defined but I forgot to add it to the code above.

Line 6 should read

strUserName = objUser.sAMAccountName

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G_M

ASKER

ok, so that is my issue... I guess if I define a second value

strReplacePath = "C:\\Users\\" & strUserName & "\\AppData\\Local\\Lotus\\Notes\\Data

and throw that into the Sub it should fix the problem.

Thanks bp"
Welcome, glad that helped.

~bp