cmd: Find updated folder and delete it using cmd - cmd

Using CMD line, in a given directory, I want to detect the most recently created/written folder and delete all the contents of that folder.
Any help/suggestions would be helpful.

This command prints all subdirectories in order of their last write/created time in reverse order (latest directories first):
DIR /A:D /O:-D /TW /B
To delete a directories' contents, a simple
DEL /S /Q "directory"
should be sufficient
If you want to process only the first result of the DIR command, you can use a FOR loop in a batch file, that leaves after the first iteration.
It should look something like this:
#ECHO OFF
REM delete all contents from the sub directory most recently created or written to
FOR /F "delims=" %%A IN ('DIR /A:D /O:-D /TW /B') DO (
RD /S /Q %%A
EXIT /B
)
Only works for the subdirectories of the current working directory, so use with care!
I guess for empty directories there will be some weird output, but I didn't test it.
EDIT:
Updated the batch file to remove the whole directory and its content using:
RD /S /Q "directory"

Related

Delete file with same name contained in different sub-folders

For some reason, I have a copy of dismhost.exe in a lot of folders, inside temp folder; what I want to do is delete every instance of it, which are inside folders inside temp.
So the structure is as follows:
/temp
/folder1
dismhost.exe
/folder2
dismhost.exe
/folder3
dismhost.exe
...
I first tried
rm ./*/dismhost.exe
but then I remembered there is no rm in windows, so I tried with rd with same arguments. That raised an error, saying that the * modifier is not valid.
How can I achieve this?
This can be done using a FOR loop iterating over a list of files returned by a recursive DIR search. When you are satisfied with the output, remove ECHO in order to actually delete the files.
FOR /F "usebackq tokens=*" %f IN (`DIR /S /B /A:-D \temp\dismhost.exe`) DO (ECHO DEL "%~f")
If this is placed into a .bat script, be sure to double the % characters on the variable.
FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D \temp\dismhost.exe`) DO (
ECHO DEL "%%~f"
)
Or use recursive delete.:
del /s \temp\dismhost.exe

How do I find a Specific File Name in Batch and change to that directory?

I am coding a batch file that will get the directory of a specific file(curl_for_64bit.exe). I tried using the find command but it does not work. It basically gets the directory of the file, changes to that directory so that it can be copied.
you can traverse the directory tree with a FOR command checking for the existence of the required file until it's found
for /r /d %%a in (*) do (
if exist %%a\curl_for_64bit.exe (
pushd %%a
goto :eof
)
)
you can use command like #Ken White say, then use other code to get the file's directory.
here is my code
rem you should go to the specific root directory(like c d e etc.)
cd /d c:\
dir /s /a /b curl_for_64bit.exe >tmp.txt
set /P file_path=<tmp.txt
del tmp.txt
cd /d %file_path%\..
And if you just want to copy those files, why you want to go to the directory of file?
The following script searches the file curl_for_64bit.exe in the directory tree rooted at C:\ROOT\ and changes to the parent directory where the found file is actually located:
for /F "delims=" %%F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do (
cd /D "%%~dpF"
)
Or this command line to be typed directly into cmd:
for /F "delims=" %F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do #cd /D "%~dpF"
To learn what the ~dp modifier of the for variable %%F means and how it works, open a new command prompt window, type for /? and read the help text (see the last section in particular).

Removing all directories keeping files untouched (Windows cmd)

I need to write a .bat file that deletes all directories in a specified directory, but not files. How could I do that? Thanks in advance.
You could try something like
for /f %%d in ('dir /b /ad') do rmdir %%d
to delete all empty directories in the current working directory.
The /b switch gives just the summary, so just one entry per line.
The /ad switch gives only directories.
rd (or rmdir) deletes only empty directories by default.
Edit:
As deadlyDev pointed out, you could add /S /Q to RD to remove non-empty directories, resulting in
for /f %%d in ('dir /b /ad') do rmdir /s /q %%d

Recursively Delete Folders matching "*folder1\folder2"

I'm trying to delete all .svn folders ONLY if they are in a CVS folder. The pattern should be something like this "*CVS\.svn".
However, my attempts at writing a batch script at this is not working at the moment. Here is what I have so far although it doesn't work.
FOR /D /R %%X IN (*CVS\.svn) DO RD /S /Q "%%X"
or
FOR /R CVS %%X IN (.svn) DO (RD /S /Q "%%X")
This works if you start one level higher than the "CVS" directory (it's a little more complex otherwise):
for /f %d in ('dir /a:d /b /s CVS') do (
if exist "%d\.svn\." rd /s /q "%d\.svn"
)
The first line finds all the directories named "CVS" recursively, then the second deletes the sub-directory ".svn" if it exists. If you're running it from a batch/shell script, use %%d instead of %d.

How to delete files recursively

I have the directory structure /foo/bar/fooBar/.. . I want to write a Windows command where I can mention the path till foo directory and it deletes all the files and directory recursively in /foo, but it should NOT delete the foo directory.
I have been using rmdir /q /s [path to foo] but this command deletes the foo directory as well. Let me know if there is any command(s) to accomplish this.
rd /s /q /path/to/foo
md /path/to/foo
del /f /s /q DirectoryWhichContainsFilesToDelete/\*
This will delete all files in the folder DirectoryWhichContainsFilesToDelete without deleting the folder itself.
Have fun :)
I had been scratching my head on this one as well. It is easy enough to create a for loop that uses rmdir however it leaves behind folders that have spaces in the long names. It is possible to manipulate a dir list and get the 8.3 filenames however here is a much simpler solution.
Create an empty folder then;
robocopy \empty_folder \folder_with_sub_folders /PURGE
All subfolders & files will be deleted.
del X /f /s /q
rd X /s /q
this WILL remove the ROOt directory though. make it again with
md X
or make a copy of it first.
otherwise you'll have to do batch funkiness
dir X /ad /b
will give you a list of the immediate subdirectories of X. you can work out the rest
I was looking for a simple command to delete all files in a directory recursively but leaving the directory structure unchanged. So, maybe this could be interesting ;)
for /f "delims=" %i in ('dir /B /S /A:-DH') do #del /F /Q /A:H "%i"
The command 'dir /B /S /A:-D' lists only files (/A:-D) in current directory recursively (/S) without 'dir' summary report (/B). The 'for' loops through each full line (/delims=) and executes the delete command, forced and quiet. I additionally used the hidden flag (/H) both for listing and deletion for some mysterious (e.g. thumbs.db) files.
deltree /foo/* should work fine.
I have used this in a batch file in the past. It uses a for loop to navigate the directory structure.
Here I remove the cvs sub directories off of a tree, needed when copying from one branch to another.
#echo off
if /I exist CVS. rd CVS /s /q >nul
for /F %%z in ('dir cvs /ad /s /b') do echo %%z && rd /s /q %%z
echo Batchfile %0 is complete
Try to use Powershell:
powershell -Command "Remove-Item '\foo\*' -Recurse -Force"
To prevent deleting of the foo directory try change directory to foo prior to the delete such as:
cd c:\foo
rd /s /q c:\foo
This will delete all the files and folders under foo but NOT foo. An error message will be displayed as follow "The process cannot access the file because it is being used by another process."

Resources