rd, rmdir, del what to use - del

Situation:
x:\neededmap\files.txt
x:\neededmap\trashmap1\files.txt
x:\neededmap\trashmap1\trashmap2\files.txt
x:\neededmap\trashmap3\files.txt
x:\neededmap\trashmap4\files.txt
How to delete by batch all Files.txt and all trashmaps
Or better, just the neededmap must remain.

You may do this through del /f /s /q files.txt - this will delete all files.txt from the current working directory (CWD) and all subsequent directories. Please use caution.
Microsoft provides excellent documentation on their Del references page. The /f forces deletion of read-only files, the /s is equivalent to -recurse in Powershell. The /q makes it execute quietly and not request for confirmation.

Related

Finding and removing ._ files with batch file

I'm trying to find and remove ._ files using a batch file. The files were copied from a Mac onto a PC.
ECHO Again!
DIR /B /A-D /ON /S ._* 2>nul
DEL /S ._*
It will list them but won't delete them. What's up with that?
D:\projects\._my_colours.txt
D:\projects\._png_vs_jpeg.jsx
D:\projects\._rainbow_screen_sizes.jsx
D:\projects\._save_it_mac.jsx
D:\projects\._some_text.txt
Could Not Find D:\projects\*._
I can delete them by hand, however, I just want to know where I'm going wrong.
The files carried over from MAC should have the archive and hidden properties only, so to ensure that you only pick those up, I'd suggest that you only select those for deletion. The DEL command has an /A option which selects the files to delete based upon their attributes. As soon as you use the /A option it picks up all attributes as if you'd selected them. If you do not exclude any attributes using the - prefix it will delete them all, (except for Read-only, unless you've included the /F option too). For example DEL /F /A ._* will delete those with Read-only, System, Hidden, Archive, unIndexed and reparse points (L). In this case your wanting those with just H and A, so to exclude all others you should use their exclusion prefix. Additionally as ._* is a global wildcard, you'll probably want to use the /Q option to prevent a prompted request for confirmation.
Del /S /Q /A:HA-R-S-I-L ._*
As a note-worthy point, if you have MAC directories transferred over too, those may have file partners. For example the ready for archiving hidden directory .Trashes will be partnered with a ready for archiving hidden file named ._.Trashes along side it. These would be deleted using the wildcard above, so if you have those type of directories, and you're not removing those too, you may wish to use a different method in order to preserve their partner files.
You could do that from the command-line via a for-loop. (From a batch-file you need to escape the % characters with another, %%):
For /F "Delims=" %A In ('Dir /B /S /A:HA-D-R-S-I-L ._* 2^>NUL') Do #If Not Exist "%~dpxA\" Del /A "%A"
In this case, the DIR command selects all of the files using the same methodology as the DEL command used, and passes those files as metavariables to the Do portion. We then delete each passed file, as long as it isn't a partner to a directory. We can do that using the /A option alone, because DIR has preselected only those we want. To perform the check for a partner, we just use a simple IF NOT EXIST statement, remembering that when checking for the existence of a directory, we use a trailing backslash. The check is performed by expanding each metavariable, %A to its drive:, \path\ and .extension using %~dpxA. The expansion of D:\MyDirectory\SubDirectory\._.Trashes would return D:\MyDirectory\SubDirectory\.Trashes so the check performed will effectively be If Not Exist "D:\MyDirectory\SubDirectory\.Trashes\". If that directory does not exist then it is deleted using Del /A "D:\MyDirectory\SubDirectory\._.Trashes".
System files! D'oh!
DEL /AH /S ._*

Use wildcards in del command

Goal is to delete all jpg and png files in a folder.
del "ToDelete/*/*.jpg"
del "ToDelete/*/*.png"
pause
This code doesn't delete any of files. Are wildcards used correctly?
Wildcards can only be used in the last element of a path. You can use a for /D loop to loop through the parent directories of the files to delete:
for /D %%D in ("ToDelete\*") do (
del "%%~D\*.jpg" "%%~D\*.png"
)
Add the /Q switch to delete without prompting.
Note: In Windows, use \ as path separators as / might cause trouble.
In case you want to delete files in any directory depth, use switch /S of del:
del /S "ToDelete\*.jpg" "ToDelete\*.png"
Add the /Q switch to delete without prompting.

How to solve "The directory is not empty" error when running rmdir command in a batch script?

I am making a batch script and part of the script is trying to remove a directory and all of its sub-directories. I am getting an intermittent error about a sub-directory not being empty. I read one article about indexing being the culprit. I disabled WSearch but I eventually got the error again. Here's the command:
rmdir /S /Q "C:\<dir>\"
I experienced the same issues as Harry Johnston has mentioned. rmdir /s /q would complain that a directory was not empty even though /s is meant to do the emptying for you! I think it's a bug in Windows, personally.
My workaround is to del everything in the directory before deleting the directory itself:
del /f /s /q mydir 1>nul
rmdir /s /q mydir
(The 1>nul hides the standard output of del because otherwise, it lists every single file it deletes.)
I'm familiar with this problem. The simplest workaround is to conditionally repeat the operation. I've never seen it fail twice in a row - unless there actually is an open file or a permissions issue, obviously!
rd /s /q c:\deleteme
if exist c:\deleteme rd /s /q c:\deleteme
I just encountered the same problem and it had to do with some files being lost or corrupted. To correct the issue, just run check disk:
chkdsk /F e:
This can be run from the search windows box or from a cmd prompt. The /F fixes any issues it finds, like recovering the files. Once this finishes running, you can delete the files and folders like normal.
enter the Command Prompt as Admin and run
rmdir /s <FOLDER>
I had a similar problem, tried to delete an empty folder via windows explorer. Showed me the not empty error, so I thought I try it via admin cmd, but none of the answers here helped.
After I moved a file into the empty folder. I was able to delete the non empty folder
As #gfullam stated in a comment to #BoffinbraiN's answer, the <dir> you are deleting itself might not be the one which contains files: there might be subdirectories in <dir> that get a "The directory is not empty" message and the only solution then would be to recursively iterate over the directories, manually deleting all their containing files... I ended up deciding to use a port of rm from UNIX. rm.exe comes with Git Bash, MinGW, Cygwin, GnuWin32 and others. You just need to have its parent directory in your PATH and then execute as you would in a UNIX system.
Batch script example:
set PATH=C:\cygwin64\bin;%PATH%
rm -rf "C:\<dir>"
Im my case i just moved the folder to root directory like so.
move <source directory> c:\
And then ran the command to remove the directory
rmdir c:\<moved directory> /s /q
I had "C:\Users\User Name\OneDrive\Fonts", which was mklink'ed ( /D ) to "C:\Windows\Fonts", and I got the same problem. In my case
cd "C:\Users\User Name\OneDrive"
rd /s Fonts
Y (to confirm the action)
helped me. I hope, that it helps you too ;D
What worked for me is the following. I appears like the RMDir command will issue “The directory is not empty” nearly all the time...
:Cleanup_Temporary_Files_and_Folders
Erase /F /S /Q C:\MyDir
RMDir /S /Q C:\MyDir
If Exist C:\MyDir GoTo Cleanup_Temporary_Files_and_Folders
The reason rd /s refuses to delete certain files is most likely due to READONLY file attributes on files in the directory.
The proper way to fix this, is to make sure you reset the attributes on all files first:
attrib -r %directory% /s /d
rd /s %directory%
There could be others such as hidden or system files, so if you want to play it safe:
attrib -h -r -s %directory% /s /d
rd /s %directory%
Windows sometimes is "broken by design", so you need to create an empty folder, and then mirror the "broken folder" with an "empty folder" with backup mode.
robocopy - cmd copy utility
/copyall - copies everything
/mir deletes item if there is no such item in source a.k.a mirrors source with
destination
/b works around premissions shenanigans
Create en empty dir like this:
mkdir empty
overwrite broken folder with empty like this:
robocopy /copyall /mir /b empty broken
and then delete that folder
rd broken /s
rd empty /s
If this does not help, try restarting in "recovery mode with command prompt" by holding shift when clicking restart and trying to run these command again in recovery mode
one liner:
if exist folder rmdir /Q /S folder
I'm using this in a NPM script like so (Javascript) :
//package.json
"scripts": {
"start": "parcel --no-cache",
"clean": "if exist dist rmdir /Q /S dist",
"deploy": "npm run clean && parcel build --no-source-maps && firebase deploy"
},
Open CMD as administrator
chkdsk c: /F /R
Press the “Y” key if asked to check your disk the next time your system restarts.
Restart the machine. After that just delete the folder.
The easiest way I found to do this is:
rm -rf dir_name
it worked on zsh - macOS, it should work on windows cmd as well.
if you need to delete a folder on Windows with a batch file you will need to use PowerShell and this is how it is done:
rmdir .\directory_name\ -Recurse
Similar to Harry Johnston's answer, I loop until it works.
set dirPath=C:\temp\mytest
:removedir
if exist "%dirPath%" (
rd /s /q "%dirPath%"
goto removedir
)
Force delete the directory (if exists)
Delete.bat
set output_path="C:\Temp\MyFolder"
if exist %output_path% (
echo Deleting %output_path%
attrib -r /s /d %output_path%
rd /s /q %output_path%
)
I've fixed this before my making sure there wasn't extra whitespace in the name of the directory I was deleting. This is more of a concern when I had the directory name contained within a variable that I was passing to RD. If you're specifying your directly in quotes then this isn't helpful, but I hope that someone like me comes along with the same problem and sees this. RD /S /Q can work, as I noticed the issue started happening when I changed something in my batch script.
I can think of the following possible causes:
there are files or subdirectories which need higher permissions
there are files in use, not only by WSearch, but maybe by your virus scanner or anything else
For 1.) you can try runas /user:Administrator in order to get higher privileges or start the batch file as administrator via context menu. If that doesn't help, maybe even the administrator doesn't have the rights. Then you need to take over the ownership of the directory.
For 2.) download Process Explorer, click Find/Find handle or DLL... or press Ctrl+F, type the name of the directory and find out who uses it. Close the application which uses the directory, if possible.

While searching for .pst files FOR keeps looping when it reaches a hidden shortcut folder

The following command creates an infinite loop which is not what I want since I am iterating through files and it needs to end sometime...
Here is what I have:
cd C:\
FOR /R %i IN (*.pst) do #echo %i
See what happens is that when it reaches AppData and finds a .pst (in AppData\Local\Microsoft\Outlook) there is a shortcut folder inside AppData\Local called "Application Data" which loops back to AppData\Local but keeps adding it's name to the address like so:
%AppData%\Local\Application Data\Application Data\Application Data\Microsoft\Outlook\%filename%.pst
What could I add to my code to keep it from looping or much better to completely ignore shortcuts so that the loop ends when it finds all the files that I need?
-----------Edit-------------
This seems to do something similar:
dir /s /b *.pst
You can filter out reparse points with DIR /A-L.
However, using DIR /A-L /S won't work also, because reparse point contents are not reparse points, so, try this:
Instead of FOR use:
FindFiles.bat *.pst c:\
Create a FindFiles.bat file with:
#ECHO OFF
:GetDirFiles %1=Wildcard %2=Path
FOR %%f IN ("%~f2\%~1") DO ECHO %%~ff
FOR /F "DELIMS=" %%d IN ('DIR /B /AD-L "%~f2"') DO CALL :GetDirFiles %1 "%~2\%%d"
This will recursivelly get all directories which are not reparse points and echo items matching pattern for each directory.
Ok, I recommend you use forfiles which should be on your computer if your using windows 7. Type forfiles /? for more info. Try this:
forfiles /p "C:\" /s /m "*.pst" /c "cmd /c (Echo #path)"
That should work perfectly. Im looking in ways of doing this with a for /r loop. It probably involves a dir check in a for /r /d. Tell me if this works fine for you.
Mona

Accelerating file deletion on windows

I wish to delete a lot of files on windows quickly as possible.
Got any suggestions on how to do it (in batch script, windows command or ant script etc.)?
I was thinking of the following directions:
Parallel deletion. It apperats that when I delete a bunch of files it takes longer than deleting groups of these files parallely.
Any (quick!) way to make these files on creation time to "sit" closely on the disk?
Any optimizations in deletion, given that the deletion is permanat?
Any lazy mechanism windows has. That is, it doesn't really delete the files at that moment, but creates the effect that it did (you won't see it on the cmd.exe dir command or on the gui, and you could create a new file with a name that used to exist etc.) and windows will continue the real deletion in the background.
Any input will be extremely helpfull,
Thank you.
I find that the command line tends to be faster with file operations. For example, to delete C:\Parent\Folder\OldFolder, open the Command Prompt and type:
CD "C:\Parent\Folder"
Attrib -R -S -H "OldFolder\*.*" /S /D /L
RD /S /Q "OldFolder"
You could try using the commmand line as Hand-E-Food said. You can use these commands to delete files and remove folders.
del /f /s /q
rd /s /q
Or use wildcards to wipe all files out from a directory:
del C:\filestodel\*.* /f /s /q

Resources