Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 666 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to delete files/subfolders in a specific directory at the command prompt in Windows

#11
Use:

del %pathtofolder%\*.* /s /f /q

This deletes all files and subfolders in `%pathtofolder%`, including read-only files, and does not prompt for confirmation.

Reply

#12
You can do it by using the following command to delete all contents and the parent folder itself:

RMDIR [/S] [/Q] [drive:]path
Reply

#13
Use [Notepad][1] to create a text document and copy/paste this:

rmdir /s/q "%temp%"
mkdir "%temp%"

Select *Save As* and file name:

> delete_temp.bat

Save as type: All files and click the <kbd>Save</kbd> button.

It works on any kind of account (administrator or a standard user). Just run it!

I use a temporary variable in this example, but you can use any other! PS: For Windows OS only!

[1]:

[To see links please register here]

Reply

#14
I tried several of these approaches, but none worked properly.

I found this two-step approach on the site *[Windows Command Line][1]*:

forfiles /P %pathtofolder% /M * /C "cmd /c if @isdir==FALSE del @file"

forfiles /P %pathtofolder% /M * /C "cmd /c if @isdir==TRUE rmdir /S /Q @file"

It worked exactly as I needed and as specified by the OP.

[1]:

[To see links please register here]


Reply

#15
@ECHO OFF

SET THEDIR=path-to-folder

Echo Deleting all files from %THEDIR%
DEL "%THEDIR%\*" /F /Q /A

Echo Deleting all folders from %THEDIR%
FOR /F "eol=| delims=" %%I in ('dir "%THEDIR%\*" /AD /B 2^>nul') do rd /Q /S "%THEDIR%\%%I"
@ECHO Folder deleted.

EXIT

...deletes all files and folders underneath the given directory, but not the directory itself.
Reply

#16
None of the answers as posted on 2018-06-01, __with the exception__ of the single command line posted by __[foxidrive](

[To see links please register here]

, really deletes all files and all folders/directories in `%PathToFolder%`. That's the reason for posting one more answer with a very simple single command line to delete all files and subfolders of a folder as well as a batch file with a more complex solution explaining why all other answers as posted on 2018-06-01 using __DEL__ and __FOR__ with __RD__ failed to clean up a folder completely.

---

The simple single command line solution which of course can be also used in a batch file:

pushd "%PathToFolder%" 2>nul && ( rd /Q /S "%PathToFolder%" 2>nul & popd )

This command line contains three commands executed one after the other.

The first command __PUSHD__ pushes current directory path on stack and next makes `%PathToFolder%` the current directory for running command process.

This works also for [UNC](

[To see links please register here]

) paths by default because of command extensions are enabled by default and in this case __PUSHD__ creates a temporary drive letter that points to that specified network resource and then changes the current drive and directory, using the newly defined drive letter.

__PUSHD__ outputs following error message to handle __STDERR__ if the specified directory does not exist at all:

> The system cannot find the path specified.

This error message is suppressed by redirecting it with `2>nul` to device __NUL__.

The next command __RD__ is executed only if changing current directory for current command process to specified directory was successful, i.e. the specified directory exists at all.

The command __RD__ with the options `/Q` and `/S` removes a directory __quietly__ with all __subdirectories__ even if the specified directory contains files or folders with hidden attribute or with read-only attribute set. The system attribute does never prevent deletion of a file or folder.

Not deleted are:

1. Folders used as the current directory for any running process. The entire folder tree to such a folder cannot be deleted if a folder is used as the current directory for any running process.

2. Files currently opened by any running process with file access permissions set on file open to prevent deletion of the file while opened by the running application/process. Such an opened file prevents also the deletion of entire folder tree to the opened file.

3. Files/folders on which the current user has not the required (NTFS) permissions to delete the file/folder which prevents also the deletion of the folder tree to this file/folder.

The first reason for not deleting a folder is used by this command line to delete all files and subfolders of the specified folder, but not the folder itself. The folder is made temporarily the current directory for running command process which prevents the deletion of the folder itself. Of course this results in output of an error message by command __RD__:

> The process cannot access the file because it is being used by another process.

*File* is the wrong term here as in reality the folder is being used by another process, the current command process which executed command __RD__. Well, in reality a folder is for the file system a special file with file attribute *directory* which explains this error message. But I don't want to go too deep into file system management.

This error message, like all other error messages, which could occur because of the three reasons written above, is suppressed by redirecting it with `2>nul` from handle __STDERR__ to device __NUL__.

The third command, __POPD__, is executed independently of the exit value of command __RD__.

__POPD__ pops the directory path pushed by __PUSHD__ from the stack and changes the current directory for running the command process to this directory, i.e. restores the initial current directory. __POPD__ deletes the temporary drive letter created by __PUSHD__ in case of a UNC folder path.

__Note:__ __POPD__ can silently fail to restore the initial current directory in case of the initial current directory was a subdirectory of the directory to clean which does not exist anymore. In this special case `%PathToFolder%` remains the current directory. So it is advisable to run the command line above not from a subdirectory of `%PathToFolder%`.

__One more *interesting* fact:__
I tried the command line also using a UNC path by sharing local directory `C:\Temp` with share name `Temp` and using UNC path `\\%COMPUTERNAME%\Temp\CleanTest` assigned to environment variable `PathToFolder` on Windows 7. If the current directory on running the command line is a subdirectory of a shared local folder accessed using UNC path, i.e. `C:\Temp\CleanTest\Subfolder1`, `Subfolder1` is deleted by __RD__, and next __POPD__ fails silently in making `C:\Temp\CleanTest\Subfolder1` again the current directory resulting in `Z:\CleanTest` remaining as the current directory for the running command process. So in this very, very special case the temporary drive letter remains until the current directory is changed for example with `cd /D %SystemRoot%` to a local directory really existing. Unfortunately __POPD__ does not exit with a value greater 0 if it fails to restore the initial current directory making it impossible to detect this very special error condition using just the exit code of __POPD__. However, it can be supposed that nobody ever runs into this very special error case as UNC paths are usually not used for accessing local files and folders.

For understanding the used commands even better, open a command prompt window, execute there the following commands, and read the help displayed for each command very carefully.

+ `pushd /?`
+ `popd /?`
+ `rd /?`

[Single line with multiple commands using Windows batch file](

[To see links please register here]

) explains the operators `&&` and `&` used here.

---

Next let us look on the batch file solution using the command __DEL__ to delete files in `%PathToFolder%` and __FOR__ and __RD__ to delete the subfolders in `%PathToFolder%`.

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Clean the folder for temporary files if environment variable
rem PathToFolder is not defined already outside this batch file.
if not defined PathToFolder set "PathToFolder=%TEMP%"

rem Remove all double quotes from folder path.
set "PathToFolder=%PathToFolder:"=%"

rem Did the folder path consist only of double quotes?
if not defined PathToFolder goto EndCleanFolder

rem Remove a backslash at end of folder path.
if "%PathToFolder:~-1%" == "\" set "PathToFolder=%PathToFolder:~0,-1%"

rem Did the folder path consist only of a backslash (with one or more double quotes)?
if not defined PathToFolder goto EndCleanFolder

rem Delete all files in specified folder including files with hidden
rem or read-only attribute set, except the files currently opened by
rem a running process which prevents deletion of the file while being
rem opened by the application, or on which the current user has not
rem the required permissions to delete the file.
del /A /F /Q "%PathToFolder%\*" >nul 2>nul

rem Delete all subfolders in specified folder including those with hidden
rem attribute set recursive with all files and subfolders, except folders
rem being the current directory of any running process which prevents the
rem deletion of the folder and all folders above, folders containing a file
rem opened by the application which prevents deletion of the file and the
rem entire folder structure to this file, or on which the current user has
rem not the required permissions to delete a folder or file in folder tree
rem to delete.
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul

:EndCleanFolder
endlocal

The batch file first makes sure that environment variable `PathToFolder` is really defined with a folder path without double quotes and without a backslash at the end. The backslash at the end would not be a problem, but double quotes in a folder path could be problematic because of the value of `PathToFolder` is concatenated with other strings during batch file execution.

Important are the two lines:

del /A /F /Q "%PathToFolder%\*" >nul 2>nul
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul

The command __DEL__ is used to delete all files in the specified directory.

+ The option `/A` is necessary to process really all files including files with the hidden attribute which __DEL__ would ignore without using option `/A`.
+ The option `/F` is necessary to force deletion of files with the read-only attribute set.
+ The option `/Q` is necessary to run a quiet deletion of multiple files without prompting the user if multiple files should be really deleted.
+ `>nul` is necessary to redirect the output of the file names written to handle __STDOUT__ to device __NUL__ of which can't be deleted because of a file is currently opened or user has no permission to delete the file.
+ `2>nul` is necessary to redirect the error message output for each file which can't be deleted from handle __STDERR__ to device __NUL__.

The commands __FOR__ and __RD__ are used to remove all subdirectories in specified directory. But `for /D` is not used because of __FOR__ is ignoring in this case subdirectories with the hidden attribute set. For that reason `for /F` is used to run the following command line in a separate command process started in the background with `%ComSpec% /c`:

dir "%PathToFolder%\*" /AD /B 2>nul

__DIR__ outputs in bare format because of `/B` the directory entries with attribute `D`, i.e. the names of all subdirectories in specified directory independent on other attributes like the hidden attribute without a path. `2>nul` is used to redirect the error message output by __DIR__ on no directory found from handle __STDERR__ to device __NUL__.

The redirection operator `>` must be escaped with the caret character, `^`, on the __FOR__ command line to be interpreted as a literal character when the Windows command interpreter processes this command line before executing the command __FOR__ which executes the embedded `dir` command line in a separate command process started in the background.

__FOR__ processes the captured output written to handle __STDOUT__ of a started command process which are the names of the subdirectories without path and never enclosed in double quotes.

__FOR__ with option `/F` ignores empty lines which don't occur here as __DIR__ with option `/B` does not output empty lines.

__FOR__ would also ignore lines starting with a semicolon which is the default end of line character. A directory name can start with a semicolon. For that reason `eol=|` is used to define the vertical bar character as the end-of-line character which no directory or file can have in its name.

__FOR__ would split up the line into substrings using space and horizontal tab as delimiters and would assign only the first space/tab delimited string to specified loop variable `I`. This splitting behavior is not wanted here because of a directory name can contain one or more spaces. Therefore `delims=` is used to define an empty list of delimiters to disable the line splitting behavior and get assigned to the loop variable, `I`, always the complete directory name.

Command __FOR__ runs the command __RD__ for each directory name without a path which is the reason why on the __RD__ command line the folder path must be specified once again which is concatenated with the subfolder name.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

+ `del /?`
+ `dir /?`
+ `echo /?`
+ `endlocal /?`
+ `for /?`
+ `goto /?`
+ `if /?`
+ `rd /?`
+ `rem /?`
+ `set /?`
+ `setlocal /?`
Reply

#17
I had following solution that worked for me:

`for /R /D %A in (*node_modules*) do rmdir "%A" /S /Q`

It removes all node modules folder from current directory and its sub-folders.

This is similar to solutions posted above, but i am still posting this here, just in case someone finds it useful
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through