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
Related
I have a data set consisting of files organised according to the following hierarchical folder/subfolder structure:
I would like to remove all nuisance subfolders (move its contents outside of it at the same hierarchical level + delete the nuisance folder), thus ending up with the files organised like this:
How can I achieve this, using a batch file, run from a command prompt inside Windows 7? I've tried a number of for statements with %F and %%F, but none worked. Grateful for any tips.
I believe the following will accomplish your goal if and only if all child folder and file names are unique. If there are duplicates, then all hell will break loose.
I have not tested, so backup and/or try the code on disposable data first.
You will have to modify the first PUSHD command to point to the root where all your "person n" folders reside.
#echo off
pushd "yourRootWherePersonFoldersReside"
for /d %%U in (*) do (
pushd "%%U"
for /f "eol=: delims=" %%A in ('dir /b /ad 2^>nul') do (
for /d %%B in ("%%A\*") do (
for /d %%C in ("%%B\*") do (
md "%%~nxC"
for /d %%D in ("%%C\*") do move "%%D\*" "%%~nxC" >nul 2>nul
)
)
rd /s /q "%%A"
)
popd
)
popd
The second FOR loop must be FOR /F instead of FOR /D because FOR /D has the potential to iterate folders that have been added after the loop has begun. FOR /F will cache the entire result of the DIR command before iteration begins, so the newly created folders are guaranteed not to interfere.
How do you all delete the newest file in a folder using batch script?
All results I have found ONLY show how to delete the oldest files or delete them after N days.
Links are : Batch file to delete files older than N days and Batch Script to delete oldest folder in a given folder
Thank you
for /F %%a in ('dir /B /O-D /A-D %the_directory%') do echo("%the_directory%\%%a"&goto deldone
:deldone
would be my approach. It rudely escapes from the for loop having deleted the first filename encountered in a sorted-in-reverse-date-order (/o-d) list.
This saves having to read the entire list. Not such a problem with a few files, but can be a pain with thousands.
The required DEL command is merely ECHOed for testing purposes. After you've verified that the command is correct, change ECHO(DEL to DEL to actually delete the files.
I would do this:
#echo off
set latest=
set the_directory=your_directory
for /F %%a in ('dir /B /OD /A-D %the_directory%') do set latest=%%a
del %the_directory%\%latest%
run dir on files only, with sorting on modification date. Then loop and keep last echoed file. Delete it (replace by echo to test!)
The following code snippet does what you want:
set "FILE="
pushd "\path\to\folder" || exit /B 1
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /O:D /T:C "*.*"
') do (
set "FILE=%%F"
)
if defined FILE del "%FILE%"
popd
The dir command returns files only (/A:-D) sorted by date in ascending order (/O:D) using the creation date (/T:C), which are located in \path\to\folder and match the pattern *.*.
The for /F loop walks through all the files returned by dir and assigns its name to variable FILE, overwriting it in each iteration, hence the final value is the name of the newest file.
The del command finally deletes the found file. The if query covers the case when no files are found in the given location.
Here is a slightly modified variant as recommended by Magoo's comment, which might be a bit more performant than the aforementioned one:
set "FILE="
pushd "\path\to\folder" || exit /B 1
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /O:-D /T:C "*.*"
') do (
if not defined FILE set "FILE=%%F"
)
if defined FILE del "%FILE%"
popd
This avoids multiplicately overwriting of variable FILE by querying whether it has already been defined. Note the reversed sort order of dir here.
However, the approach shown in Magoo's answer is still better in performance.
Bat to delete specific lis of subfolders
In windows xp I have a folder name gaming that contain 100 folders with names games1, games2, games3 ... games100.
And inside every of those folders there is a huge list of subfolders from 1 to 100000.
And I have a name list of the 50000 folders that i want to delete
example of the name list that I want to remove without knowing the name of their parent directory
6383
6385
4850
6395
6396
6397
9865
6401
6408
1200
...
..
.
try this:
#echo OFF &SETLOCAL
SET "namelist=list.txt"
for /f "usebackq delims=" %%a in ("%namelist%") do set "$%%a=1"
for /d /r "gaming" %%a in (*) DO IF DEFINED $%%~na ECHO rd /s /q "%%~a"
Look at the output and remove the word echo before rd if it looks good.
This expects a list.txt in the d:\folder\gaming folder and it will create "removefolders.bat.txt" in the same folder.
Open that file in Notepad and verify that the correct folders are listed and then you can rename it to .bat and run it to actually delete the folders.
#echo OFF
pushd "d:\folder\gaming"
del "removefolders.bat.txt" 2>nul
for /f "delims=" %%a in ('type "list.txt" ') do (
for /d /r %%b in (*) do if "%%~nxb"=="%%a" >>"removefolders.bat.txt" echo rd /q /s "%%b"
)
popd
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"
I need to write a command in a .bat file that recursively deletes all the folders starting with a certain string. How may I achieve this ?
This is the complete answer you are looking for:
FOR /D /R %%X IN (certain_string*) DO RD /S /Q "%%X"
where obviously you need to replace certain_string with the string your folders start with.
This deletes RECURSIVELY as you asked (I mean it goes throught all folders and subfolders).
How about:
for /d %a in (certain_string*) do rd /s %a
This will work from the command prompt. Inside a batch file, you would have to double the %s, as usual:
#echo off
for /d %%a in (certain_string*) do rd /s %%a
Unfinished, I think. If you meant "Recursively go down a directory hierarchy to delete all folders starting with a certain string", then the following might suffice:
for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"
This will recurse into the directory tree, finding all folders starting with "abc", iterate over that list and removing each folder.
Maybe you need to wrap an if exist around the rd depending on the order in which directories are found and returned. In general, iterating over something and changing it at the same time is rarely a good idea but sometimes it works :-)
rm -rf -- "Directory name"
Ex : rm -rf -- "-2096378"
Above command will deletes the folders/directories starting with - or wildcard characters
FOR /F "tokens=*" %i IN ('DIR **[[SearchTerm]]** /A:D /s /b') do rd /S / Q %i