Link to home
Start Free TrialLog in
Avatar of calebbaity
calebbaity

asked on

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
SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of calebbaity
calebbaity

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
>> The problem is happening between 2-3.
Not likely, the copy command finishes before you try to delete it.
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

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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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"
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
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!