Batch file Error: Process cannot access the file because it is being used by another process

I need to create a batch file that backs up a file then deletes the original file so that I can download a file with the same name.

As follows:

del myfileBackup.csv
copy myfile.csv myfileBackup.csv
del myfile.csv
...
myapplication --username --password --formID  --output myfile.csv

The problem I am running into is when a file is deleted/renamed it ties up that file and prevents the next step.

I get an error message

The process cannot access the file because it is being used by another process.

This caused my import/download to fail.

I appreciate any help that can be provided.

Thanks!

Michael
calebbaityAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Copying Locked files requires using either:

 A) A 3rd party utility to copy the file (Examples Here)

 B) A backup program (like running windows NTBackup from the command line) and restoring the file to where you want it.

 C) Volume Shadow Copy commands to take a shadow-Copy of the file and then restoring that shadow copy to another location.
calebbaityAuthor Commented:
Yes, I think the problem is a file lock.  
But, isn't there any way to unlock?
How long is a lock on a file maintained.

I was thinking of doing a
timeout /t 10
but that does not seem to work either.
Gerwin Jansen, EE MVETopic Advisor Commented:
You can try:

del /f /q myfileBackup.csv

but it's better to try and find what application has the file open and close that application before you try the delete.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Yes, I think the problem is a file lock.  

Yes, it is.  Probably by whatever is writing "myfile.csv"

But, isn't there any way to unlock?
Close any programs which have myfile.csv open.

How long is a lock on a file maintained[?]
Until you close all programs/services touching the file which have the file open for writing or exclusive reading.


I was thinking of doing a
timeout /t 10
but that does not seem to work either.

This will only work if you can reasonably expect that the file is only being opened, written to, and then closed periodically.

This would only be the case if say you knew that a process runs periodically, opening the file occasionally and then closing it after a few seconds.  (In which case you could use a retry mechanism to catch the file when it unlocks.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
calebbaityAuthor Commented:
I would think it is the copy command.

Because, I am
1. deleting my backup copy
2. copying my previously downloaded file to the backup file.
3. deleting the previously downloaded file.

The problem is happening between 2-3.
calebbaityAuthor Commented:
Essentially, I am doing coping (copy) and deleting (del) commands.
The import does not happen till later.

So, how do I close out a copy or del command?

This is all in a batch file.
Gerwin Jansen, EE MVETopic Advisor Commented:
>> The problem is happening between 2-3.
Not likely, the copy command finishes before you try to delete it.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I would think it is the copy command.

Because, I am
1. deleting my backup copy
2. copying my previously downloaded file to the backup file.
3. deleting the previously downloaded file.

The problem is happening between 2-3.
Report Comment

...

previously downloaded file

so myfile.csv is downloaded by what?  Why not download it to "backupmyfile.csv"?

Is it being stuck on an FTP site?  might be possible IIS is holding on to it needlessly..

I don't think it's the delete, but if the above is not the case it could be I suppose.

  If so you can do the following (and just add it as it makes no harm to just use this as a whole)


DEL /F/Q myfileBackup.csv
COPY /Y  myfile.csv myfileBackup.csv
DEL /F /Q  myfile.csv

Open in new window

calebbaityAuthor Commented:
The error message is happening on the second del command.
I would think that the copy and del commands release quickly.
But I even tried adding the timeout /t 10 for each delete.
same result.
calebbaityAuthor Commented:
I am using a third party tool that downloads their data file.
When I used it I was able to successfully download their file.
Now that I am trying to automate the process using a batch file
I am having problems.

I just tried the code you provided and got the same result.

In fact I created a new batch file with just those commands and am getting the same error.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Essentially, I am doing coping (copy) and deleting (del) commands.
The import does not happen till later.

So, how do I close out a copy or del command?

This is all in a batch file.
Report Comment

I suspect based on everything you're saying now, the real issue is deleting "MyFile.csv"

The copy must complete before the patch can progress.

Here do the following: and see where the error occurs:

@(
	ECHO OFF
	SET "_Src=C:\Path\To\Myfile.csv"
	SET "_Dst=C:\Path\to\BackupMyFile.csv"
	SET "_eLvl=0"
)

CALL :Main

(
	ENDLOCAL
	EXIT /B %_eLvl%
)

:Main
	ECHO.
	ECHO. Deleting Last Backup Located at: "%_Dst%"
	Call :DelFile "%_Dst%"
	ECHO.
	ECHO Moving "%_Src%" to "%_Dst%"
	MOVE /Y  "%_Src%" "%_Dst%" && (
		ECHO. Move Completed.
	) || (
		ECHO.  Move Failed!
		SET /A "_eLvl+=10"
	)
	REM And Just for good measure:
	ECHO.
	ECHO. Deleting Source File Located at: "%_Src%"
	Call :DelFile "%_Src%"
	ECHO.
GOTO :EOF

:DelFile
	IF NOT EXIST "%~1" (
		ECHO. File Does Not Exist: "%~1"
		ECHO. Skippinng Delete
		GOTO :EOF
	)
	ECHO. Found: "%~1"
	ECHO. Now Deleting "%~1"
	DEL /F /Q "%~1"
	ECHO.
	ECHO. Checking Result:
	IF NOT EXIST "%~1" (
		ECHO. Success Deleting: "%~1"
	) ELSE (
		ECHO. Error Deleting: "%~1"
		SET /A "_eLvl+=1"
	)
GOTO :EOF

Open in new window


Edit:  Also reading your two followup comments that were written while I was writing the above, it sounds like you have confirmed my suspicion that the issue is actually in deleting the Source file.

I am using a third party tool that downloads their data file

So, what is this tool?

Seems likely that tool is the problem.
calebbaityAuthor Commented:
How would that be possible?  
The tool has not been called yet.
In fact in the batch file I am running now it is only copy and del commands.
I removed all reference to the tool.

I tried the code you provided but it runs so quickly I can see no output.
I added a pause just before the last line, but I am still not able to see what is
happening.
calebbaityAuthor Commented:
Okay it is working now!

The real problem was me!
I had a copy of the imported file open on another screen (minimized).

So the error was reporting true.  It could not delete the file because I
had it open.

the real issue is deleting "MyFile.csv"
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
The real problem was me!
I had a copy of the imported file open on another screen (minimized).

So the error was reporting true.  It could not delete the file because I
had it open.

the real issue is deleting "MyFile.csv"

@Calebbaity

  Yup, thats what I was driving at, glad to help!

Ben
calebbaityAuthor Commented:
Take a step back  and don't assume the problem is the computer.  What you are doing or you forgot you  are doing can be the issue.

Even Experts can't help you if you 'Know' that the problem is somewhere else!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.