Nextcloud WebDAV: Move a file or folder

Move a file or a folder to a given path in Nextcloud. This can also be used for renaming.

Script nextcloud

by nextcloud ยท 2/25/2025

  • Submitted by nextcloud Bun
    Created 86 days ago
    1
    import axios from "axios";
    2
    
    
    3
    export async function main(
    4
        nextcloud: RT.Nextcloud,
    5
        sourcePath: string,
    6
        destinationPath: string,
    7
    ) {
    8
    
    
    9
        try {
    10
            console.log("Test");
    11
            console.log(`${nextcloud.baseUrl}/remote.php/dav/files/${nextcloud.userId}/${destinationPath}`);
    12
            const res = await axios.request(
    13
                {
    14
                    method: "MOVE",
    15
                    url: `/remote.php/dav/files/${nextcloud.userId}/${sourcePath}`,
    16
                    baseURL: nextcloud.baseUrl,
    17
                    headers: {
    18
                        Authorization: `Basic ${btoa(`${nextcloud.userId}:${nextcloud.token}`)
    19
                        }`,
    20
                        Destination: `${nextcloud.baseUrl}/remote.php/dav/files/${nextcloud.userId}/${destinationPath}`,
    21
                    },
    22
                },
    23
            );
    24
    
    
    25
            return res.data;
    26
        } catch (e) {
    27
            console.log(e.response.data);
    28
        }
    29
    }