1. Tracy Lamb
  2. PowerBuilder
  3. Saturday, 19 August 2023 13:18 PM UTC

Hi all,

I want to rename a directory if the directory exists.  How can I do that?  I know how to use DirectoryExists() and RemoveDirectory(), but not seeing a function to rename a directory.

Say the initial directory is 'd:\mydocs\manuals\tektronix' , and I want to rename it to 'd:\mydocs\manuals\tektronix-scopes'.

~~~Tracy

Accepted Answer
Mark Goldsmith Accepted Answer Pending Moderation
  1. Wednesday, 23 August 2023 16:31 PM UTC
  2. PowerBuilder
  3. # Permalink

Hi Tracy,

Try prepending cmd /c in front of your rename string with it's own quotes around the entire string. So it would look something like:

ls_RunCommand = "cmd /c 'rename "' + ls_full_path + '" "' + ls_description + '"'"

Lots of single and double quotes already in your string so don't forget to add the closing double-quote.

HTH...regards,

Mark

Comment
  1. Tracy Lamb
  2. Wednesday, 23 August 2023 17:52 PM UTC
Thank you Mark, that worked!

ls_RunCommand = 'cmd /c rename "' + ls_full_path + '" "' + ls_description + '"'



~~~Tracy
  1. Helpful
  1. Tracy Lamb
  2. Wednesday, 23 August 2023 17:55 PM UTC
Crazy mix of quotes and double-quotes! Lol!
  1. Helpful
  1. Mark Goldsmith
  2. Wednesday, 23 August 2023 18:17 PM UTC
LOL, yes lots of quotes. Great to hear that worked for you Tracy and thanks for the update!
  1. Helpful
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Saturday, 19 August 2023 14:52 PM UTC
  2. PowerBuilder
  3. # 1

Hi, Tracy - 

A "rename" operation is, in essence, the same as a "move" operation. You can utilize the MoveFileW Windows API function:

// External Function Declaration to move a file or directory
FUNCTION Boolean MoveFile ( &
   String lpExistingFileName, &
   String lpNewFileName &
   ) LIBRARY "kernel32.dll" ALIAS FOR "MoveFileW"

Online documentation here:

    https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefilew

Depending on your needs, there is also a MoveFileExW API function:

    https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexw

It utilizes a third argument value that specifies bit flags. In C/C++, it is declared as a DWORD. In PB, this is equivalent to an UnsignedLong.

Best regards, John

Comment
  1. Mark Goldsmith
  2. Wednesday, 23 August 2023 22:37 PM UTC
Lol...true, unless you use the Minimized! windowstate, then you only have a brief icon flash on the taskbar ;)
  1. Helpful 1
  1. Chris Pollach @Appeon
  2. Thursday, 24 August 2023 00:05 AM UTC
If you use Roland's "run & wait" - it can suppress the external dialogue.
  1. Helpful 1
  1. Miguel Leeuwe
  2. Thursday, 24 August 2023 12:21 PM UTC
Hi Chris,

Have to try that out! Many times running a batch / cmd file would do wonders, but I've always been reserved about the black cmd shell showing.

Thanks!
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Saturday, 19 August 2023 14:34 PM UTC
  2. PowerBuilder
  3. # 2

Hi Tracy;

  I use the PB Run() command calling the DOS "Rename" command to perform that action. HTH 

Regards ... Chris 

Comment
  1. Tracy Lamb
  2. Wednesday, 23 August 2023 12:10 PM UTC
HI Chris,

I tried this. From the cmd prompt, any directory, I can enter:

rename "E:\BT_Docs\Manuals\Fluke" "Fluke-Calibrators"

But when I run this exact command in my app, I keep getting a -1 error code. The command I'm sending from PB is exactly the same. Here's the code:

if DirectoryExists( ls_server_path) then

ls_full_path = ls_server_path + '\Manuals\' + is_original_description

if DirectoryExists ( ls_full_path ) then

ls_RunCommand = 'rename "' + ls_full_path + '" "' + ls_description + '"'

li_rc = RUN(ls_RunCommand)

if li_rc = -1 then

MessageBox("Change directory name", "Run command failed. ~r~n"+ ls_RunCommand)

end if

end if

end if

Any thoughts?

~~~Tracy

  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 23 August 2023 12:38 PM UTC
Hi Tracy;

This could because of your App's execution privileges.

Also, you might need to use the Run & Wait approach to get the proper return code from the Rename command.

FYI: https://www.topwizprogramming.com/freecode_runandwait.html

Regards ... Chris
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.