Programmatically renaming files...any way to do it with ShellExecuteA?

 

I'm trying to figure out how I can programmatically rename files.

I use ShellExecuteA to xcopy, that works just fine. But xcopy only allows for making a file with a duplicate name, not for copying as a new name (or so I have gathered from googling a bit).

Googling for "rename" and ShellExecute only leads me to a lot of links from other people asking the same question, but no solutions.

How can I rename a file programmatically with my EA?

 

Hi, the shell command for renaming files is REN i assume this command should be executable trough ShellExecute

add: if you want to copy and rename maybe MOVE is the command you search

 
1005phillip:

How can I rename a file programmatically with my EA?

#import "kernel32.dll"
   int MoveFileA(string ExistingFilename, string NewFilename);
#import

void start()
{
   string strCurrentFile = TerminalPath() + "\\experts\\files\\existing.txt";
   string strNewName = TerminalPath() + "\\experts\\files\\newname.txt";
   
   int res = MoveFileA(strCurrentFile, strNewName);
   if (res == 0) MessageBox("Failed!");
}     
 
1005phillip:

I use ShellExecuteA to xcopy, that works just fine. But xcopy only allows for making a file with a duplicate name, not for copying as a new name (or so I have gathered from googling a bit).
[...]
How can I rename a file programmatically with my EA?

Or, alternatively, if you really want to use ShellExecute for some reason:

#import "shell32.dll"
    int ShellExecuteA(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
#import

void start()
{
   string strCurrentFile = TerminalPath() + "\\experts\\files\\existing.txt";
   string strNewName = TerminalPath() + "\\experts\\files\\newname.txt";
   
   string strParams = "/c move \"" + strCurrentFile + "\" \"" + strNewName + "\"";

   ShellExecuteA(0, "open", "cmd.exe", strParams, "", 0);
}   

...again doing a "move" which in effect is a rename with the additional option of moving between directories. The broader point is that you can execute shell commands such as REN and MOVE by doing cmd.exe /c <command-line>

But, personally, I'd use:

#import "kernel32.dll"
   int CopyFileA(string strExistingFile, string strCopyOfFile, int OverwriteIfCopyAlreadyExists);
#import

...rather than than ShellExecute() and xcopy as a way of copying files, because it's faster, and it's easier to check whether the operation has succeeded.

 
jjc:

Or, alternatively, if you really want to use ShellExecute for some reason:

...again doing a "move" which in effect is a rename with the additional option of moving between directories. The broader point is that you can execute shell commands such as REN and MOVE by doing cmd.exe /c <command-line>

But, personally, I'd use:

...rather than than ShellExecute() and xcopy as a way of copying files, because it's faster, and it's easier to check whether the operation has succeeded.


Wow, that is awesome, it is like you read my mind!

I was only using shellexecute and xcopy because of the utility of shellexecute for programatically launching optimizations within an EA and the xcopy feature was getting a supporting job done (albeit in rather limited fashion).

But what I really needed was the ability to rename a file in its existing location (Move) and then copy that renamed file to another folder location (CopyA).

I incorporated both functions and tested it this morning, works beautifully! Thank you so much :)
Reason: