I want to remove all files in the directory d:\\tmpdata in os win10.
import shutil
shutil.rmtree('d:\\tmpdata')
It encounter an issue such as:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'd:\\tmpdata'
Try to get which process is using the resources--directroy d:\\tmpdata:
tasklist | findstr 'tmpdata'
or
tasklist #run it only ,search by eye.
No process can be found ,how to get which process is using the directory ?
Try built-in software Resource Monitor. Search resmon.exe. Start program, select CPU then Associated Handles - type your folder path and see, what is using it.
Related
I'm using Windows 7 with a VPN (Cisco AnyConnect) connection to an academic file share server. I copied a PowerPoint file from a local path to the server, and tried to delete the file on the server after no longer needing it.
It now shows up as a 0KB file, and I can no longer perform any actions on it except opening it as read-only. When I do open it as read-only, I receive the prompt "Unknown is working on \server\share_pathtofile. Do you want to open a read-only copy in the meantime?", and the file is empty (no slides, settings, etc). Here 'server_pathtofile' is the entire path of the problem file. I am positive no other user is accessing the file from another machine.
I have tried to overwrite it by Save As, using the same file name. Once I click Save, the save window closes and reopens without performing any action. This happens indefinitely if I continue to click Save. I can save the file with a different name on the server, and delete the file with a different name without issue.
I tried removing problem file using python through the Anaconda Spyder distro i use on my laptop.
import os
path = r"\\server\share"
file = r"\file.pptx"
#file = r"\test2.txt"
f = path + file
os.remove(f)
When i run it for the trouble file, it produces the error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '\\\\server\share_pathtofile'
I looked into the shutil function, but don't think that'll be any more useful. I suspect the process handle for the PowerPoint file has been corrupted in some way. I thought to try and use Windows to perform the action directly through Powershell:
Remove-Item 'file.pptx'
But received a similar error:
Remove-Item : Cannot remove item \\server\share_pathtofile: The process cannot access the file '\\server\share_pathtofile' because it is being used by another
process.
At line:1 char:12
+ Remove-Item <<<< 'file.pptx'
+ CategoryInfo : WriteError: (\\server...file.pptx:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
UPDATE 1: I tired postanote's suggestion by deleting through cmd.exe
pushd \\server\share
del /f "file.pptx"
The process cannot access the file because it is being used by another process.
I would've thought my inclusion of the powershell attempt to remove the file would've made this approach an obvious dead end. Shoudln't i be trying to find the so-called process that is telling Windows the file is still open?
Also, I was able to reproduce the same issue with a second ppt file and an excel file. They are not 0KB, but upon opening i get the same sort of prompt: "someone else is working on them and you can read-only".
I was making copies of the file within the same folder directory of the file share. My connection to the server got interrupted somehow, as Windows Explorer prompted me saying the connection to server share was able to be found. Upon closing the prompt, Windows Explorer did an unexpected refresh, the mapped drive was fine (i could navigate it without having to remap it), then suddenly I had two more problem files.
It is certainly not having the Windows Explorer preview pane open, i have never used that Explorer feature. Any ideas? We shouldn't let this file win...
PE will only look at process where it was started. Just like Task Manager, and if you are not accessing the file via some process (PowerPoint.exe), or know that in a shared file scenario, no one else has it locked, then you can take action on it.
This is not a PS specific issue (so not really a PS question) or anything related to process explorer and nothing to do with admin right on a server/workstation. If you have read/write/modify/delete permissions on a system / file share, you can act on that file of course.
It's a Windows proper issue. I've seen (and had this happen) this zero length files (depending on how they were created / copied) more than once, and they appear as impossible to delete.
The deal here, often Windows Explorer for such files won't even let you shorten the name via a rename effort. So, name length, odd characters, etc., are often the culprit(s)
So, you need to use cmd.exe and delete the file using the short DOS name. Just do a dir on the location where file is to get the short name and delete it.
dir /X
Preferably a command line tool to do this...
I understand that there is a program called tasklist.exe in Windows systems, and it has many powerful features to view processes currently running on the system.
Unfortunately it does not have the functionality to view the path of the .exe file that created or spawned the process in the first place.
I finally decided to take a tour of my system and I've noticed some shady looking processes and I want to check if they live in equally shady looking places on my system.
Any ideas?
You can use PowerShell.
Click Start -> Run, and type powershell to invoke Power Shell.
View all processes currently:
tasklist
Show full path of .exe file (Example command for Notepad++):
Get-Process notepad++ | Select-Object Path
You will see output:
Path
C:\Program Files (x86)\Notepad++\notepad++.exe
I am trying to map a drive with an elevated batch file but any attempt to use the Net Use command fails. The error is always: "The system cannot find the drive specified."
I am using the following code: Net Use w: "\Server\Folder"
It works fine with an elevated command prompt or with a non-elevated batch file.
I also tried simply: Net Use
Yet with an elevated batch file even that fails with the same error message.
net use DrvLetter: \\server\sharename[\foldername]
so
net use m: \\serenity\C$
Note UNC paths start \\
or for any computer this will work to the admin share called C$. $ means it's a hidden share. Every drive has one (ie D$, E$, ..., Z$) + there is Admin$ (C:\windows), IPC$, and Printer$ (your printer driver folder).
net use m: \\%computername%\C$
I'm trying to make a scheduled task that runs a python script in a particular directory located on a network drive. I figured I need a batch file in order to achieve this.
I'm running the following in command prompt successfully:
pushd \\mydrive\somedirectory
Z:\>
But I'm trying to do the same thing in a batch file and get:
The network name cannot be found,
The system cannot find the path specified.
What might I be doing incorrectly?
Why I ask is that my program uses 3rd party software that sometimes leaves behind orphan processes that have no link back to my program or the 3rd party process. These orphan processes start to add up and consume tons of resources over time. I would like to kill them off periodically, but in order for me to do that, I need to know for sure they were created by my program and not some other program. I have viewed the orphan processes in Process Explorer and when looking at the properties of the process, I see a field called "Current Directory". The current directory for the orphaned process is the install directory of my program. This would give me reassurance I am killing a process created by my program.
Since these processes are created by a 3rd party, I need to just kill them after they are created by running taskkill on them or something. Is there a way to figure out the current working directory of a process using out of the box windows commands in a batch file? If this can be done through wmic queries that would be preferable, but I cannot seem to find the current working directory when using wmic. I assume if Process Explorer is able to obtain this info, I should be able to get it too through some batch commands.
tlist from WDK to the rescue! The 2nd line of its output ("CWD: ...") shows the working directory of a process:
> tlist 944
944 postgres.exe
CWD: D:\Lab\Database\pgsql\test\
CmdLine: "D:/Tools/pgsql/bin/postgres.exe" -D "."
VirtualSize: 221116 KB PeakVirtualSize: 242620 KB
WorkingSetSize: 17076 KB PeakWorkingSetSize: 19336 KB
NumberOfThreads: 4
9084 Win32StartAddr:0x00000000 LastErr:0x00000000 State:Waiting
8504 Win32StartAddr:0x00000000 LastErr:0x000000b7 State:Waiting
8616 Win32StartAddr:0x00000000 LastErr:0x00000000 State:Waiting
7468 Win32StartAddr:0x00000000 LastErr:0x00000000 State:Waiting
9.3.5.14202 shp 0x0000000000400000 D:\Tools\pgsql\bin\postgres.exe
6.1.7601.18247 shp 0x00000000770D0000 C:\Windows\SYSTEM32\ntdll.dll
...
See the doc for more info.
Handle is an utility that displays information about open handles for any process in the system. You can use it to see the programs that have a file open, or to see the object types and names of all the handles of a program.
Its GUI-based version is Process Explorer .
handle -p yourProcess.exe > log.txt
It'll list all handles for yourProcess.exe in log file and now using batch command you can easily extract 'current working directory' of yourProcess from log.txt.
added by barlop
here is the output.. for process c:\tinyweb\tiny.exe run from c:\tinyweb\rrr
C:\Users\user>handle -p tiny.exe
Nthandle v4.1 - Handle viewer
Copyright (C) 1997-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
------------------------------------------------------------------------------
tiny.exe pid: 20668 compA\user
10: File C:\Windows
1C: File C:\tinyweb\rrr
9C: File C:\tinyweb\rrr\access_log
A0: File C:\tinyweb\rrr\agent_log
A4: File C:\tinyweb\rrr\error_log
A8: File C:\tinyweb\rrr\referer_log
E4: Section \Sessions\1\BaseNamedObjects\__wmhr_msgs_buffer_name$1e74
EC: File C:\Windows\winsxs\x86_microsoft.windows.common-controls_659
C:\Users\user>
If you want to parse it specifically then you could do it in pure cmd.exe with e.g. for /f, or with a third party scripting language like ruby, or with windows ports of various *nix style command line tools. This line uses such tools and gets it (obviously the following line requires grep and sed, preferably decent versions of them e.g. from cygwin)
C:\Users\harvey>handle -p tiny.exe | grep "pid:" -A 3 | sed -n "3p" | grep -o ".:[\]\S*"
C:\tinyweb\rrr
The following will work, though you only need "CommandLine" or "ExecutablePath" - not both:
wmic process where "ProcessID=1111" get CommandLine, ExecutablePath
It will return something like the following, showing where the program for PID 1111 is running:
"C:\Program Files (x86)\Common Files\MyProgram\Agent\agent.exe"