Copying only completed files in shell script - shell

I am trying to complete a task to copy all the files from a directory to another location.
This will be an ongoing script so I will be copying files every 5s and then deleting the files once they are copied.
The only catch is, I need to make sure that the file is complete before I copy it (i.e not being modified any longer)
Is there anyway to find out if the file is being used/is open?
Thank you for the help

Take a look at the fuser command. It will tell you the process(es) which have the file open.

Related

Move file already open by the script

I will create a python script to download some pdf files from a web site and next save these files in some directories according to data extracted by each pdf. The problem is: I must open a pdf file in my script in ordert to extract data (I use pdfquery) but when I try to move the file to destination directory I get an error: the file is open by another process. I supposse the proces is the script. My qyestion is, it's possible to release the file from the python script and finally move it to the destination without errors?
A solution is copy (instead move) file and delete it manually or create another script which move the files when the "download and extract" script finish is job. But I search a clean approach.

Xcopy sharing violation, how to still copy without skipping

I’m copying files from one directory to another with Windows xcopy command in cmake. Sometimes I run into situations where one file is already being used by another program, which results in a sharing violation error. Therefore, the file does not get copied and my compilation fails.
How do I get around this without skipping over files that are already being used. I don’t have the option to close any open files.
Any guidance would be greatly appreciated.
The only way is to kill the process or program which uses the file that you are copying via task manager and try again.
/c will be used to skip the file and continue copying but in your case, you dont want to skip the files that are already being used.

Why does this batch file fail for some users?

I have a batch file that copies the most recent version of an access front-end file to the user's C: drive and then opens it from there.
For some users, the copy command causes the batch file to close, and I can't work out what could cause that. The file seems to copy, but the batch file just closes itself without any visible error messages.
I've used Pause to confirm that the failure happens at the Copy step, not the Run or the If.
This is Windows 7, I've tried it with Copy and Xcopy. The users with the issue say it's worked in the past, they all have access to the location being copied from (and to). Mapping the location doesn't seem to make any difference, and UNC paths work for most users so it's not that.
Deleting the existing files in C:\databases doesn't help.
if not exist "C:\Databases\" mkdir "C:\Databases"
copy "\\SERVER02\FINOPS\COMMAQR\DIGIHUB\1. Live Version\DIGIHUB v2.5.accdb" "C:\Databases\"
start [the file]
For 95%+ of users, the batch file copies the most recent version down and opens the file. For a handful, the batch file reaches the copy step and closes itself.
Does anyone know why this could happen, or alternatives to both Copy and XCopy that might not fail?

Moving files in shell script and copying files to the same folder at the same time

I am writing a shell script to move all files from one directory to other. Say I want to move all files from directory dirA to directory dirB. When the move is in progress, some user is copying some files to dirA. How do I ensure that, whatever file the shell script is trying to move, has finished copying? What would happen if we try to move a file which is still being copied?
The list of filenames is generated as soon as the move command is issued. Any changes after that will not be taken into account. So if new files are created during the move process, they won't get impacted.
You may have to run your script in a loop.
If one of the files that is part of the move command is still being modified (after being opened in its old place), the changes would
propagate to the new place when the change operation is complete and the file is closed.

Copy new files to another folder on windows automatically

I have a folder where some xml files keep updated, I want those files copied to another folder. From which a software collects the xml. I want to watch the folder and copy only new files as it comes to another folder automatically.
Is there any ready made solution or can it be done using Autoit, shell script or vb.net(last choice).
Main concerns:
Know when a file is added.
Copy them to the folder.
Make sure its notcopied second time.
If I schedule this code, it might also work
cd /home/webapps/project1/folder1 for f in *.csv do cp -v $f
/home/webapps/project1/folder2/${f%.csv}$(date +%m%d%y).csv done
But it again don't have a way to not copy those which are already copied.
rsync. Put it into cron to run every few minutes.

Resources