You are viewing this site as a guest. You can still ask questions and help others! Join our Dev2Dev IT Community to receive your own blog, share your knowledge, and much more.

Shell script to rename a file in Unix server

1 reply [Last post]
Guest's picture

Hi There,

Can somebody give a script to rename a file in a particular directory of Unix server?


Lepa's picture
User offline. Last seen 2 years 20 weeks ago. Offline
Joined: 06/23/2008
Posts: 591
Re: Shell script to rename a file in Unix server

Why call a unix script to do the rename while you can do the rename right from your PC? Below is a function that I wrote that uses unix commad to move "rename" the file on a unix server where PC is running. To rename the file, just move it to the same direcotry with a different file name.

Function uhc_move_file(&sFilePathFrom, &sSourceFileName, &sFilePathTo, &TargetFileName) Returns boolean;
   rem -- function to move files on the same app server from one directory to another;
   rem -- All params are required;
   rem -- The function returns true if executed successfully;
   If All(&sFilePathFrom, &sSourceFileName, &sFilePathTo, &TargetFileName) Then
      rem -- make sure path has "/" at the begining and the end. example: "/prod/ap/files/";
      &sFilePathFrom = "/" | RTrim(LTrim(&sFilePathFrom, "/"), "/") | "/";
      &sFilePathTo = "/" | RTrim(LTrim(&sFilePathTo, "/"), "/") | "/";
      &ExitCode = Exec("mv " | &sFilePathFrom | &sSourceFileName | " " | &sFilePathTo | &TargetFileName, %Exec_Synchronous + %FilePath_Absolute);
      If &ExitCode <> 0 Then
         MessageBox(0, "", 0, 0, ("Problem moving file from " | &sFilePathFrom | &sSourceFileName | " to " | &sFilePathTo | &TargetFileName));
         Return False;
         MessageBox(0, "", 0, 0, ("File has been moved from " | &sFilePathFrom | &sSourceFileName | " to " | &sFilePathTo | &TargetFileName));
         Return True;
      MessageBox(0, "", 0, 0, ("Missing param - all 4 params must be passed to the function."));
      Return False;

Give back to the community and help it grow!
* Help with unanswered forum questions and issues
* Register or login to share your knowledge at your own blog