/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:
> 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 closedOutput
/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.logprints 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.
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]::OpenorStreamWriterin PowerShell stays open untilClose()orDispose(). - Retry: sync clients and antivirus hold files only briefly.
robocopyhas/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
Related pages
errors · cmdcmd: Access is denied.Access is denied in cmd is Windows error 5: the file is read-only, NTFS permissions do not allow it, or the command needs an elevated Command Prompt to run.codes · cmdrobocopy exit codes: 0, 1, 2, 3, 8 and 16 explainedrobocopy exit codes are bit flags: 1 copied, 2 extra files, 4 mismatches, 8 failures, 16 fatal error. Any code below 8 is success; treat 8 or more as failure.errors · powershellPowerShell: Cannot find path 'C:\...' because it does not exist.The file or folder is not there: a typo, a path relative to another folder, or a name with [ ] read as a wildcard. Check with Test-Path, use -LiteralPath.