如何重命名文件并移动到输出目录?

How to rename a file and move to output directory?

我是 progress 4GL 的新手,我编写了以下代码将文件从输入目录移动到输出目录。但问题是我想重命名文件 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).

请记住,如果源和目标位于不同的文件系统上,“mv”将变成复制操作,然后是删除操作。对于大文件,这可能会很慢。

下面应该执行你想要的两步过程:

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.

没有理由在两个不同的命令中执行“移动”和“重命名”,除非您要测试两者之间的“移动”是否成功。如果您这样做,那么我会假设您必须已经拥有该代码。

如果文件名和路径是独立输入的,您可能还想使用 SUBSTITUTE() 函数来构建完整的路径字符串。像这样:

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 ).