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

问题描述

我是4GL的新手,我写了以下代码文件从输入目录移到输出目录。但是担心的是,我想在移动之前/之后重命名文件

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