Macro command to rename a file

Macro command to rename a file

Anonymous
Not applicable
905 Views
3 Replies
Message 1 of 4

Macro command to rename a file

Anonymous
Not applicable

Hi, is there a macro command to rename a file?
Example: C: \ Users \ Pc_User \ Downloads \ favicon.ico     to         C: \ Users \ Pc_User \ Downloads \ icona.ico
Thank you.

0 Likes
Accepted solutions (1)
906 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Accepted solution
FUNCTION MAIN() {
   BOOL Success = false
// RENAME file in same directory
   CALL RenameFile('c:\temp\a.txt','b.txt',$Success) 
   IF $Success {
// Move/rename file
      CALL RenameFile('c:\temp\b.txt','d:\data\alfa\beta\a.txt',$Success)
      IF $Success {
// Move file to different location, same name - last char in $target is '\'
         CALL RenameFile('d:\data\alfa\beta\a.txt','c:\temp\',$Success)
      }
   }
}

FUNCTION RenameFile(STRING Source, STRING Target,OUTPUT BOOL Success) {
// This macro rename/move source file to target file
// Method: Copy source to target, then delete source file 
   $Success = false
   $Source=trim($Source)
   $Target=trim($Target)
   IF $Source == '' OR $Target == '' {
      MESSAGE INFO 'RenameFile() - Empty string(s)'
      RETURN
   }
   IF NOT file_exists($Source) {
      MESSAGE INFO 'RenameFile() - '+$Source +' file is not exists'
      RETURN
   }
   IF substring($Target,length($Target)-1) == '\' {
      $Target=$Target+basename($Source)
   }
   IF dirname($Target) =='' {
      $Target=dirname($Source)+'\'+$Target
   }
   IF lcase($Target) == lcase($Source) {
      MESSAGE INFO 'RenameFile() - Source == Target'
      // ?? $Success = true
      RETURN
   }
   IF file_exists($Target) {
      STRING prompt = 'RenameFile() - '+ $Target +' file is exists. Overwrite with ' + $Source + '?'
      BOOL overwrite = false
      $overwrite = query $prompt
      IF NOT $overwrite {
         RETURN
      }
      DELETE FILE $Target
   }
   IF NOT dir_exists(dirname($Target)) {
      CALL MakeDir(dirname($Target),$Success)
      IF NOT $Success {
         RETURN
      }
   }
   COPY FILE $Source $Target
   IF NOT file_exists($Target) {
      MESSAGE INFO 'RenameFile() - copy error'
      $Success = false
      RETURN
   }
   DELETE FILE $Source
   IF file_exists($Source) {
      MESSAGE INFO 'RenameFile() - cannot delete '+ $Source
      $Success = false
      RETURN
   }
   $Success = true
} 

FUNCTION MakeDir(STRING path, OUTPUT BOOL Success) {
   IF dir_exists($path) {
      $Success = true
      RETURN
   }
   IF NOT dir_exists(dirname($path)) {
      CALL MakeDir(dirname($path),$Success)
      IF NOT $Success {
         RETURN
      }
   }
   MKDIR $path
   IF NOT dir_exists($path) {
      $Success = false
      MESSAGE INFO 'MakeDir() - cannot create '+$path 
   } ELSE {
      $Success = true
   }
}
0 Likes
Message 3 of 4

Anonymous
Not applicable

You are a great,
Perfect.
Thank you.

0 Likes
Message 4 of 4

Anonymous
Not applicable

New version of MakeDir() function. It now handles missing/invalid drives.

 

FUNCTION MakeDir(STRING path, OUTPUT BOOL Success) {
   IF $path == '' {
      $Success = false
      RETURN
   }
   IF dir_exists($path) {
      $Success = true
      RETURN
   }
   IF NOT dir_exists(dirname($path)) {
      CALL MakeDir(dirname($path),$Success)
      IF NOT $Success {
         RETURN
      }
   }
   MKDIR $path
   IF NOT dir_exists($path) {
      $Success = false
      MESSAGE INFO 'MakeDir() - cannot create '+$path
   } ELSE {
      $Success = true
   }
}

0 Likes