Skip to content

/errors/cmd/file-is-being-used-by-another-process

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

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Command Prompt (cmd)
exit code
32
versions
Windows 10, Windows 11, Windows Server 2016+
os
windows

The process cannot access the file because it is being used by another process. is Windows system error 32 (ERROR_SHARING_VIOLATION). Another process (or your own script) has the file open and did not allow others to delete, rename or write it. Windows does not break the lock; close the program that holds the file and run the command again.

Reproduce it

A PowerShell one-liner opens report.log with no sharing, and cmd tries to delete, rename and append to it while it is open:

cmd
> report.log echo line 1
powershell -NoProfile -Command "$f = [IO.File]::Open('report.log', 'Open', 'Read', 'None'); cmd /c 'del report.log 2>&1 & ren report.log old.log 2>&1 & (echo more>> report.log) 2>&1'; $f.Close()"
del report.log
if not exist report.log echo deleted after the handle was closed

Output

/tmp/work/report.log
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
deleted after the handle was closed

del prints the file name and the message, ren and the >> redirection print only the message.

Find the process that holds the file

  • Resource Monitor: run resmon, open the CPU tab, type the file name in Associated Handles. The process is listed with its PID.
  • Sysinternals Handle: handle.exe report.log prints the process name and PID (run it as administrator to see every process).
  • Programs you know: close the document in Word or Excel, stop the dev server that writes the log, stop the service.
cmd
tasklist /fi "pid eq 1234"

In scripts

  • Make sure your own handles are closed: a > redirection closes when the command finishes, but a [IO.File]::Open or StreamWriter in PowerShell stays open until Close() or Dispose().
  • Retry: sync clients and antivirus hold files only briefly. robocopy has /r:N /w:S (retries and wait seconds) for this case.
  • Executables and DLLs that are running cannot be overwritten. Stop the program, or replace the file at the next restart.

Sources