Page 1 of 1

batch file code delete stubborn file ?

Posted: 09 Apr 2022 04:06
by Mysystem
hi
batch file code delete stubborn file ?

Re: batch file code delete stubborn file ?

Posted: 09 Apr 2022 07:19
by ShadowThief
Probably some combination of the TAKEOWN and ICACLS commands, but there's probably a very good reason why you aren't able to delete that file.

Re: batch file code delete stubborn file ?

Posted: 11 Apr 2022 02:03
by Jedininja
I have done extensive testing on how to delete stubborn files and i have a few solutions.

A)
Edit group policy to allow the user about to delete files admirative permissions.
- open gpedit.exe
- goto Local Computer Policy / Computer Configuration / Windows Settings / Security Settings / User Rights Assignment / Take ownership of files or other objects
note: lots of usefully settings in gpedit.exe however you can very easily break your OS install this also applies to User Account Permission's and Ownership/Inherence

B)
Change the User Account Permission's and Ownership
- Open Security and Maintenance in Control Panel/System and Security.
OR
- "Security" Tab in the left click file properties menu

- Set user permissions and ownership to :
Allow Administrators
Allow Users
Allow Everyone
Remove all the other user permission's
Disable File Inheritance
this will insure that whatever account your using can access it the file or folder.
than you can
- RD /F
- Del /F
- Shift Delete in the UI
C)
- boot any windows media in boot mode,
- get to a cmd window and
- manually delete the file with del or rd
- type notepad into the cmd window
goto "open file"
Select "all file types"
search for the file you want to delete
note: this will also work with an offline Linux live boot os/ or as alongside Linux install


Hope is Helpful

Re: batch file code delete stubborn file ?

Posted: 13 Apr 2022 07:28
by jfl
What do you mean by stubborn file?

Before going through complex and risky procedures like updating group policies, it'd be easier if we knew more about what you did, and what error messages you got.

The most common cases are
- A read-only file
- A file for which you don't have the necessary user rights
- Did you try doing this in an Administrator command shell?
- Some files are owned by the Windows system. Deleting them is usually a very bad idea. If you know for sure it's safe to delete it, MS SysInternals has tools for running a command as system.
- Maybe the file has a name incompatible with standard WIN32 APIs? (Like ending with a space, or called "nul", etc. This can happen when using the WSL.)
In which case prefixing the full pathname with \\?\ usually resolves the issue.