【问题标题】:How to rename a file and move to output directory?如何重命名文件并移动到输出目录?
【发布时间】:2020-10-15 06:14:27
【问题描述】:

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

【问题讨论】:

  • 您的示例代码将 UNIX 命令与 Windows 路径混合在一起。这可能不会很好地工作。
  • 未经消毒的rm 看起来很危险,我怀疑你是否希望有人通过-r *

标签: unix openedge progress-4gl


【解决方案1】:

请记住,如果源和目标位于不同的文件系统上,“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 ).

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 2022-12-05
    • 1970-01-01
    • 2014-12-19
    • 2011-03-20
    • 2023-03-26
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多