How to find and delete a file with CMD command? - windows

I know the filename, say myfile.pdf. But I do not know its location because my web application created it temporarily. When the user disconnect from the application then I want this file to be deleted. So how to search and delete the file?

del path\filename /s
e.g.
del c:\test\h.bat /s
Will delete h.bat in any sub folders from the test directory.
To search and delete on an entire drive: -
del c:\test.bat /s
The S switch deletes all specified files from all subdirectories.

You can delete files by using this command.
rm [filename]

Related

Why can't I delete the contents of the 'INetCache' folder from the command prompt?

I'm using Windows 10 and need to update a batch file to clear the contents of folder at "C:\Users\myUserName\AppData\Local\Microsoft\Windows\INetCache". This is the folder that holds cache for Internet Explorer.
I would have thought the command was simply as follows:
DEL /S /F /Q C:\Users\myUserName\AppData\Local\Microsoft\Windows\INetCache\*
This command executes without issue, but when I look at the contents of the folder via Windows Explorer, it still has stuff in it. I say "stuff" because while it looks like a bunch of files, they don't behave like typical files. For instance, I cannot see these files when when I use "dir" on the command prompt no matter which flags I utilize. And when I examine the properties of these files, there is barely any information associated with them.
Why isn't my DEL command clearing out these files?
Screenshot of "files" still present in my INetCache folder
Screenshot of the properties of one of these files
I found that using the "rmdir" command instead of the "del" command resolved my issue.
rmdir "C:\Users\myUserName\AppData\Local\Microsoft\Windows\INetCache" /s /q
open txt and save .bat file
DEL /S /F /Q C:\Users\%username%\AppData\Local\Microsoft\Windows\INetCache\*
pause

How to delete a directory from within that same directory with a batch file?

call :deleteSelf&exit /b
:deleteSelf
start "" /D "C:\Windows" /MIN cmd /c RD /S /Q "C:\Windows\test"&&exit /b
This is the code I use. Batch file running it sits within C:\Windows\test
The file is successfully deleted, along with any other files in the directory, but not the directory itself. Does anyone know some way to solve this issue? I'm rather stumped.
You will need, at least, to
leave the current batch file so it is not open
ensure your current active directory is not the one you want to remove
so, if you follow the already pointed dbenham's approach for leaving the current batch file you could use something like
((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0")
That is,
the (goto) will generate an error that will leave current batch file execution
we change the current active directory to the parent of the folder where the batch file is stored
it the active directory has been changed, we try to remove the folder that holds the batch file
Of course, if there is another process/file locking the folder you will not be able to remove it.
surely it's not as easy as adding the following line to your batch file:
cd c:\
rd c:\windows\test

Creating a batch file to delete a file for all users on a workstation

I am trying to create a batch file to delete a file for all users on a workstation.
Example: I want to delete the following: C:\Users\*\AppData\Local\file.XX.
I can create this to delete for any specific user, but am unsure how to create for the file to search through all user-IDs in C:\Users\*\.
Please note that we cannot use PowerShell, as this is disabled by our AD.
Try this:
FOR /D %%f IN (C:\Users\*) DO (
ECHO DEL %%f\AppData\Local\file.XX
)
Once you are sure this will act on the files you want, remove the ECHO and run it again to actually delete the files.
del /s "C:\users\file.xx"
should delete the file file.xx in all subdirectories of c:\users, also in c:\users itself.

Deleting log files windows

I need to make a batch file that can search the hard-drive for log files and remove them. Does any one know a code that I can use. I am using windows 7.
If you are certain that all *.log files on a drive can be deleted with no after effects, then this will delete the log files that are not in use:
#echo off
del \*.log /s /f /q

Delete Files with Windows Command Prompt

There is a shared folder in my D drive as works (D:\works). I need to delete all the files in that folder and sub folders except word and excel files in there. how can i do this ?
You could do something similar to what this guy's done: http://www.codesingh.com/2009/08/using-robocopy-to-delete-old-files-from.html
Something like this should work:
mkdir D:\_tempDelete
robocopy D:\works D:\_tempDelete /e /MOVE /XF *.xls* *.doc*
rmdir D:\_tempDelete /s /q
Provided you have permissions to create and delete folders on D:. Otherwise you could just move the files somewhere on your local drive and delete them from there.

Resources