如何使用Vala将文件移动到另一个目录?

时间:2018-07-04 11:31:59

标签: file directory move glib vala

我正在尝试根据Vala documentation将文件移动到另一个目录,但是以下方法无效。

请告诉我是否误解了move方法的工作原理。

moveMyFile V1:

grouped.sum(['quantity'])

端子输出(无错误,dest中没有test.txt):

public void moveMyFile(){

    GLib.File dest = GLib.File.new_for_path("~/Desktop/dest/");
    GLib.File src = GLib.File.new_for_path("~/Desktop/src/test.txt");

    print("\ndest path : %s \n", dest.get_path());
    print("src path : %s \n", src.get_path());

    if(dest.query_exists() && src.query_exists()){
        try {
            src.move(dest, FileCopyFlags.NONE, null);
        } catch (Error e) {
            print ("moveMyFile Error: %s\n", e.message);
        }
    }

}



------------------更新1 ----------------------



moveMyFile V2:

dest path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/dest 
src path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/src/test.txt

端子输出(无错误,dest中没有test.txt):

public void moveMyFile(){

    GLib.File dest = GLib.File.new_for_path("~/Desktop/dest/test.txt");
    GLib.File src = GLib.File.new_for_path("~/Desktop/src/test.txt");

    print("\ndest path : %s \n", dest.get_path());
    print("src path : %s \n", src.get_path());

    if(dest.query_exists() && src.query_exists()){
        try {
            src.move(dest, FileCopyFlags.NONE, null);
        } catch (Error e) {
            print ("moveMyFile Error: %s\n", e.message);
        }
    }

}



------------------更新2 ----------------------



moveMyFile V3:

public void moveMyFile(){

dest path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/dest/test.txt 
src path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/src/test.txt

}

端子输出(无错误,dest中没有test.txt):

string homePath = Environment.get_home_dir();
string destPath = homePath + "/Desktop/dest/test.txt";
string srcPath = homePath + "/Desktop/src/test.txt";

GLib.File dest = GLib.File.new_for_path(destPath);
GLib.File src = GLib.File.new_for_path(srcPath);

print("\ndest path : %s \n", dest.get_path());
print("src path : %s \n", src.get_path());

if(dest.query_exists() && src.query_exists()){
    try {
        src.move(dest, FileCopyFlags.NONE, null);
    } catch (Error e) {
        print ("moveMyFile Error: %s\n", e.message);
    }
}

1 个答案:

答案 0 :(得分:2)

我正在检查dest文件是否存在。我从if语句中删除了dest.query_exists()。现在可以了。

volumes:
       - db-data:/var/lib/postgresql