Bharat
Bharat

Reputation: 177

How to rename a file and move to output directory?

I am new to progress 4GL and I wrote below codes to move a file from input to output directory. But the concern is that I want to rename the file before/after moving.

define variable m_inp     as character no-undo initial "C:\Input\Test.csv".
define variable m_outpath as character no-undo initial "C:\Output".

file-info:filename = m_inp.
if file-info:file-size > 0
then
    unix silent value("mv -f " + m_inp + ' ' + m_outpath).
else
    unix silent value("rm -f " + m_inp).

Upvotes: 0

Views: 587

Answers (1)

Tom Bascom
Tom Bascom

Reputation: 14020

Keep in mind that if the source and target are on different filesystems "mv" turns into a copy operation followed by a delete. That might be slow for large files.

The following should execute the two step process that it seems like you want:

define variable m_inp     as character no-undo initial "abc123.txt".
define variable m_outpath as character no-undo initial "/tmp/abc123.txt".
define variable newName   as character no-undo initial "/tmp/123abc.txt".

file-info:filename = m_inp.

/*** using "UNIX" command (ties your code to UNIX :( ***/

/*** 
if file-info:file-size <= 0 then
  unix silent value( "rm -f " + m_inp ).
 else
  do:
    unix silent value( "mv -f " + m_inp + ' ' + m_outpath ).
    unix silent value( "mv -f " + m_outpath + ' ' + newName ).
  end.
 ***/

/*** OS portable functions ***/

if file-info:file-size <= 0 then
  os-delete value( m_inp ).
 else
  do:
    os-rename value( m_inp ) value( m_outpath ).
    os-rename value( m_outpath ) value( newName ).
  end.

There isn't really a reason to do the "move" and "rename" in two distinct commands unless you're going to be testing for success of the "move" in between. If you are doing that then I'll assume you must have that code already.

If the file names and paths are entered independently you might also like to use the SUBSTITUTE() function to build the full path strings. Something like this:

define variable dir1  as character no-undo initial ".".
define variable dir2  as character no-undo initial "/tmp".
define variable name1 as character no-undo initial "abc123.txt".
define variable name2 as character no-undo initial "123abc.txt".

define variable path1 as character no-undo.
define variable path2 as character no-undo.

/** prompt the user or whatever you really do to get the directory and file names
 **/

path1 = substitute( "&1/&2", dir1, name1 ).
path2 = substitute( "&1/&2", dir2, name2 ).

file-info:filename = path1.

if file-info:file-size <= 0 then
  os-delete value( path1 ).
 else
  os-rename value( path1 ) value( path2 ).

Upvotes: 3

Related Questions