Avatar of G_M
G_M
Flag 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
VB ScriptWindows 7

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Lucian Constantin

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
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
Lucian Constantin

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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
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
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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"
Bill Prew

Welcome, glad that helped.

~bp