How to assign a label to freshly copied folder

  • Hello,
    I'm facing one issue with my vbscript. In summary this script is copying folders from one location to another and then it's removing source object (I had permission issuest when tried to move folders).
    The distribution of source folders is based on first piece of folder name, where every piece is separated by #.
    It seems that everything is workin fine now, besides one more important thing.
    After finishing copying process I need to add label fo the folder in new location. I tried some movements without changing attributes but as previously said - no luck.Here's my code. Maybe someone will be able to help me (there may be some trashes here, so skip them if you can :slight_smile: ):
Option Explicit
Dim fso, objFSO, shell, log, sourcePath, destPath, fileSystemObject, folder, subFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
Set log = fso.CreateTextFile("F:\log.txt", True)
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")

sourcePath = "F:\"
destPath = "Y:\"

'Iterate through all folders in the source directory
For Each folder In fso.GetFolder(sourcePath).SubFolders
    Dim folderName, folderParts
    folderName = folder.Name
    folderParts = Split(folderName, "#")
    
    'Check if folder name matches pattern
    If UBound(folderParts) = 4 Then
        Dim x1, destFolder
        x1 = folderParts(0)
        log.WriteLine("Processing folder: " & folderName)
        
        'Check if matching folder exists on destination drive
        Set destFolder = Nothing
        For Each subFolder In fso.GetFolder(destPath).SubFolders
            If StrComp(subFolder.Name, x1, vbTextCompare) = 0 Then
                Set destFolder = subFolder
                Exit For
            End If
        Next
        
        'If matching folder found, copy folder and delete from source
        If Not destFolder Is Nothing Then
            log.WriteLine("    Found matching folder on destination: " & destFolder.Path)
            Dim destFolderPath
            destFolderPath = destFolder.Path & "\" & folderName
            objFSO.CopyFolder folder.Path, destFolderPath, True
			
			If Err.Number = 0 Then
                log.WriteLine("    Successfully copied folder to destination.")
                fso.DeleteFolder folder.Path
                If Err.Number = 0 Then
                    log.WriteLine("    Successfully deleted folder from source.")
					'Assign label to destination folder after it has been successfully copied
                    shell.Run "cmd /c Properties """ & destFolderPath & """ SETLABEL PostedonFBcc ADDLABEL", 0, True
                Else
                    log.WriteLine("    Error deleting folder from source: " & Err.Description)
                End If
            Else
                log.WriteLine("    Error copying folder to destination: " & Err.Description)
            End If
        Else
            log.WriteLine("    No matching folder found on destination.")
        End If
    Else
        log.WriteLine("Skipping folder: " & folderName & ". Does not match pattern.")
    End If
Next

'Clean up and display summary
log.Close()
Set objFSO = Nothing
Set fso = Nothing
Set shell = Nothing
Set log = Nothing
Set fileSystemObject = Nothing
MsgBox "Script complete. See log.txt for details."

It looks like you're trying to run the Opus Properties command via a Windows command prompt?

shell.Run "cmd /c Properties """ & destFolderPath & """ SETLABEL PostedonFBcc ADDLABEL", 0, True

You need to run Opus commands via the Opus command object. See the default script you get when creating a new script button for examples. (Or most other scripts on the forum.)

Thank you. I'm not an expert in Opus command objects.
Can you give a link to similar solution, that can be adopted to my script?

Did you look at the default script?

This assumes you're running the script inside Opus.

If it has to run outside of Opus, you can use DOpusRT.exe /acmd ... to send commands to Opus from another process. More detail in the DOpusRT.exe docs.

Ah, I understand what you mean now. Will try that.
One more question. Is that command correct?

Properties destFolderPath SETLABEL PostedonFBcc ADDLABEL

It seems it is not as easy as it seems.
First of all I'm using fixed pathes, so I'm not using

Function OnClick(ByRef clickData)

ergo I can't use:

Set cmd = clickData.func.command
cmd.RunCommand "Properties """ & destFolderPath & """ADDLABEL SETLABEL=""PostedonFBcc"""

Is it possible to call RunCommand in other way?

Edit: I also tried this (with no luck):

cmd = "dopusrt.exe /cmd Properties """ & destFolderPath & """ADDLABEL SETLABEL=""PostedonFBcc"""
shell.Run cmd, 0, True

You can get a command object with DOpus.Create.Command.

One doesn’t exclude the other.

How are you running your script?

I'm using button for that script

If you’re using a button then your script must have an OnClick function and be receiving a clickData object.

OK,
I have changed the script and it looks like the follow:

Option Explicit

Function OnClick(ByRef ClickData)
	Dim fso, objFSO, shell, log, sourcePath, destPath, fileSystemObject, folder, subFolder, cmd
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set shell = CreateObject("WScript.Shell")
	Set log = fso.CreateTextFile("F:\log.txt", True)
	Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	sourcePath = "F:\"
	destPath = "Y:"
	
	'Iterate through all folders in the source directory
	For Each folder In fso.GetFolder(sourcePath).SubFolders
		Dim folderName, folderParts
		folderName = folder.Name
		folderParts = Split(folderName, "#")

		'Check if folder name matches pattern
		If UBound(folderParts) = 4 Then
			Dim x1, destFolder
			x1 = folderParts(0)
			log.WriteLine("Processing folder: " & folderName)

			'Check if matching folder exists on destination drive
			Set destFolder = Nothing
			For Each subFolder In fso.GetFolder(destPath).SubFolders
				If StrComp(subFolder.Name, x1, vbTextCompare) = 0 Then
					Set destFolder = subFolder
					Exit For
				End If
			Next

			'If matching folder found, copy folder and delete from source
			If Not destFolder Is Nothing Then
				log.WriteLine("    Found matching folder on destination: " & destFolder.Path)
				Dim destFolderPath
				destFolderPath = destFolder.Path & "\" & folderName
				objFSO.CopyFolder folder.Path, destFolderPath, True
				
				If Err.Number = 0 Then
					log.WriteLine("    Successfully copied folder to destination.")
					fso.DeleteFolder folder.Path
					If Err.Number = 0 Then
						log.WriteLine("    Successfully deleted folder from source.")
						
						'Add label to the folder
						Set cmd = clickData.func.command
						cmd.RunCommand "Properties SETLABEL=PostedonFBcc" & destFolderPath
						log.WriteLine("    Successfully added label to folder: " & destFolderPath)
						
					Else
						log.WriteLine("    Error deleting folder from source: " & Err.Description)
					End If
				Else
					log.WriteLine("    Error copying folder to destination: " & Err.Description)
				End If
			Else
				log.WriteLine("    No matching folder found on destination.")
			End If
		Else
			log.WriteLine("Skipping folder: " & folderName & ". Does not match pattern.")
		End If

	Next

	'Clean up and display summary
	log.Close()
	Set objFSO = Nothing
	Set fso = Nothing
	Set shell = Nothing
	Set log = Nothing
	Set fileSystemObject = Nothing
	DOpus.OutputString "Script complete. See log.txt for details."
End Function

Never the less it's still refusing to set label for destFolderPath.

Instead of that it is opening Properties window of file which is currently slected in source folder.

I'm more and more confused

cmd.RunCommand "Properties SETLABEL=PostedonFBcc" & destFolderPath

That is going to try an dset a label named something like this:
PostedonFBccC:\Your\Destination\Folder ?

Probably not what you want.

If you want the Properties command to apply to a specific path, it has an argument to do that.

Or you could remove the destFolderPath part entirely and do this before running the command:

cmd.ClearFiles();
cmd.AddFile(destFolderPath);

Perfect. the second solution works fine.
Thank you