D
D
DIASWORD2022-03-07 13:34:43
linux
DIASWORD, 2022-03-07 13:34:43

How to automatically move files from lists to a specified folder?

How to implement this idea It is necessary that the script looks into the folder (for example, root/server) and if a file from the specified list appears in the folder (for example, test.cs), it automatically moves the file to the specified directory (for example, root/server/test)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
voproser45654, 2022-03-07
@voproser45654

I don’t really remember the python, but if I’m not mistaken, then so

import os, shutil
#path_1 и path_2 - путь
if str(os.listdir(path_1)).find("test.cs"):
    shutil.move(path_1 + "text.cs", path_2)

R
Radiks Alijevs, 2022-03-07
@dedalik

I won’t tell you on python, on node.js you can do this:

const fs = require('fs'); 
fs.readdir(folderName, function(err, files) {
    if (err) {
       // обработка ошибки
    } else {
       if (!files.length) {
           // каталог пустой
       } else {
          // каталог содержит файл
          const oldPath = 'old/path/file.cs'
          const newPath = 'new/path/file.cs'

          fs.rename(oldPath, newPath, function (err) {
              if (err) throw err
                 console.log('Файл перемещён')
              })
        }
    }
});

Documentation here:
https://nodejs.org/docs/latest/api/fs.html#fspromi...
https://nodejs.org/docs/latest/api/fs.html#fs_fs_r...
Also, if there is a need to check the directory in auto mode, you can use cron
https://www.npmjs.com/package/node-cron

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question