Windows cmd all files in directory

How to delete all files and folders in a folder by cmd call

I want to delete all files and folders in a folder by system call.

I may call like that:

Do you know an easier way?

12 Answers 12

No, I don’t know one.

If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:

This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).

Note that within a batch file you need to double the % within the for loop:

del c:\destination\*.* /s /q worked for me. I hope that works for you as well.

I think the easiest way to do it is:

The last «\» in the path is the important part.

Yes! Use Powershell:

If the subfolder names may contain spaces you need to surround them in escaped quotes. The following example shows this for commands used in a batch file.

To delete folder with all files in it:

To delete all files from specific folder (not deleting folder itself) is a little bit complicated. del /s *.* cannot delete folders, but removes files from all subfolder. So two commands are needed:

You can create a script to delete whatever you want (folder or file) like this mydel.bat :

Few example of usage:

One easy one-line option is to create an empty directory somewhere on your file system, and then use ROBOCOPY (http://technet.microsoft.com/en-us/library/cc733145.aspx) with the /MIR switch to remove all files and subfolders. By default, robocopy does not copy security, so the ACLs in your root folder should remain intact.

Also probably want to set a value for the retry switch, /r , because the default number of retries is 1 million.

I had an index folder with 33 folders that needed all the files and subfolders removed in them. I opened a command line in the index folder and then used these commands:

I separated them into two lines (hit enter after first line, and when asked for more add second line) because if entered on a single line this may not work. This command will erase each directory and then create a new one which is empty, thus removing all files and subflolders in the original directory.

Batch files: List all files in a directory with relative paths

Concerning Windows batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list?

For example, if I want all the .txt files in the current directory and subdirectories with their full paths, I can do

and I will get something like

I will get something like

But what I really want is:

5 Answers 5

You could simply get the character length of the current directory, and remove them from your absolute list

The simplest (but not the fastest) way to iterate a directory tree and list relative file paths is to use FORFILES.

The relative paths will be quoted with a leading .\ as in

To remove quotes:

To remove quotes and the leading .\ :

or without using delayed expansion

This answer will not work correctly with root paths containing equal signs ( = ). (Thanks @dbenham for pointing that out.)

EDITED: Fixed the issue with paths containing ! , again spotted by @dbenham (thanks!).

Alternatively to calculating the length and extracting substrings you could use a different approach:

store the root path;

clear the root path from the file paths.

Here’s my attempt (which worked for me):

The r variable is assigned with the current directory. Unless the current directory is the root directory of a disk drive, it will not end with \ , which we amend by appending the character. (No longer the case, as the script now reads the __CD__ variable, whose value always ends with \ (thanks @jeb!), instead of CD .)

In the loop, we store the current file path into a variable. Then we output the variable, stripping the root path along the way.

Читайте также:  Canon hp laserjet 1010 драйвер для windows

Command to list all files in a folder as well as sub-folders in windows

I tried searching for a command that could list all the file in a directory as well as subfolders using a command prompt command. I have read the help for «dir» command but coudn’t find what I was looking for. Please help me what command could get this.

6 Answers 6

The below post gives the solution for your scenario.

/S Displays files in specified directory and all subdirectories.

/B Uses bare format (no heading information or summary).

/O List by files in sorted order.

If you want to list folders and files like graphical directory tree, you should use tree command.

There are various options for display format or ordering.

Check example output.

Answering late. Hope it help someone.

An addition to the answer: when you do not want to list the folders, only the files in the subfolders, use /A-D switch like this:

An alternative to the above commands that is a little more bulletproof.

It can list all files irrespective of permissions or path length.

I have a slight issue with the use of C:\NULL which I have written about in my blog

But nevertheless it’s the most robust command I know.

If you simply need to get the basic snapshot of the files + folders. Follow these baby steps:

  • Press Windows + R
  • Press Enter
  • Type cmd
  • Press Enter
  • Type dir -s
  • Press Enter

Following commands we can use for Linux or Mac. For Windows we can use below on git bash.

List all files, first level folders, and their contents

List all first-level subdirectories and files

Iterate all files in a directory using a ‘for’ loop

How can I iterate over each file in a directory using a for loop?

And how could I tell if a certain entry is a directory or if it’s just a file?

17 Answers 17

This lists all the files (and only the files) in the current directory:

Also if you run that command in a batch file you need to double the % signs.

nxi ) . This thread can be really useful too: stackoverflow.com/questions/112055/…. – Sk8erPeter Dec 21 ’11 at 21:25

  • . files in current dir: for %f in (.\*) do @echo %f
  • . subdirs in current dir: for /D %s in (.\*) do @echo %s
  • . files in current and all subdirs: for /R %f in (.\*) do @echo %f
  • . subdirs in current and all subdirs: for /R /D %s in (.\*) do @echo %s

Unfortunately I did not find any way to iterate over files and subdirs at the same time.

Just use cygwin with its bash for much more functionality.

Apart from this: Did you notice, that the buildin help of MS Windows is a great resource for descriptions of cmd’s command line syntax?

To iterate over each file a for loop will work:

for %%f in (directory\path\*) do ( something_here )

In my case I also wanted the file content, name, etc.

This lead to a few issues and I thought my use case might help. Here is a loop that reads info from each ‘.txt’ file in a directory and allows you do do something with it (setx for instance).

There is a subtle difference between running FOR from the command line and from a batch file. In a batch file, you need to put two % characters in front of each variable reference.

From a command line:

From a batch file:

This for-loop will list all files in a directory.

«delims=» is useful to show long filenames with spaces in it.

‘/b» show only names, not size dates etc..

Some things to know about dir’s /a argument.

  • Any use of «/a» would list everything, including hidden and system attributes.
  • «/ad» would only show subdirectories, including hidden and system ones.
  • «/a-d» argument eliminates content with ‘D’irectory attribute.
  • «/a-d-h-s» will show everything, but entries with ‘D’irectory, ‘H’idden ‘S’ystem attribute.
Читайте также:  Диагностика компьютера устранение неполадок при запуске windows

If you use this on the commandline, remove a «%».

Windows cmd all files in directory

Displays a list of a directory’s files and subdirectories. If used without parameters, this command displays the disk’s volume label and serial number, followed by a list of directories and files on the disk (including their names and the date and time each was last modified). For files, this command displays the name extension and the size in bytes. This command also displays the total number of files and directories listed, their cumulative size, and the free space (in bytes) remaining on the disk.

The dir command can also run from the Windows Recovery Console, using different parameters. For more information, see Windows Recovery Environment (WinRE).

Syntax

Parameters

] Specifies the drive and directory for which you want to see a listing. [ ] Specifies a particular file or group of files for which you want to see a listing. /p Displays one screen of the listing at a time. To see the next screen, press any key. /q Displays file ownership information. /w Displays the listing in wide format, with as many as five file names or directory names on each line. /d Displays the listing in the same format as /w, but the files are sorted by column. /a[[:] ] Displays only the names of those directories and files with your specified attributes. If you don’t use this parameter, the command displays the names of all files except hidden and system files. If you use this parameter without specifying any attributes, the command displays the names of all files, including hidden and system files. The list of possible attributes values are:

  • d — Directories
  • h — Hidden files
  • s — System files
  • l — Reparse points
  • r — Read-only files
  • a — Files ready for archiving
  • i — Not content indexed files

You can use any combination of these values, but don’t separate your values using spaces. Optionally you can use a colon (:) separator, or you can use a hyphen (-) as a prefix to mean, «not». For example, using the -s attribute won’t show the system files. /o[[:] ] Sorts the output according to sortorder, which can be any combination of the following values:

  • n — Alphabetically by name
  • e — Alphabetically by extension
  • g — Group directories first
  • s — By size, smallest first
  • d — By date/time, oldest first
  • Use the prefix to reverse the sort order

Multiple values are processed in the order in which you list them. Don’t separate multiple values with spaces, but you can optionally use a colon (:).

If sortorder isn’t specified, dir /o lists the directories alphabetically, followed by the files, which are also sorted alphabetically. /t[[:] ] Specifies which time field to display or to use for sorting. The available timefield values are:

  • c — Creation
  • a — Last accessed
  • w — Last written
/s Lists every occurrence of the specified file name within the specified directory and all subdirectories. /b Displays a bare list of directories and files, with no additional information. The /b parameter overrides /w. /l Displays unsorted directory names and file names, using lowercase. /n Displays a long list format with file names on the far right of the screen. /x Displays the short names generated for non-8dot3 file names. The display is the same as the display for /n, but the short name is inserted before the long name. /c Displays the thousand separator in file sizes. This is the default behavior. Use /c to hide separators. /4 Displays years in four-digit format. /r Display alternate data streams of the file. /? Displays help at the command prompt.

Remarks

To use multiple filename parameters, separate each file name with a space, comma, or semicolon.

You can use wildcard characters (* or ?), to represent one or more characters of a file name and to display a subset of files or subdirectories.

You can use the wildcard character, *, to substitute for any string of characters, for example:

dir *.txt lists all files in the current directory with extensions that begin with .txt, such as .txt, .txt1, .txt_old.

dir read *.txt lists all files in the current directory that begin with read and with extensions that begin with .txt, such as .txt, .txt1, or .txt_old.

Читайте также:  How to mount windows partition

dir read *.* lists all files in the current directory that begin with read with any extension.

The asterisk wildcard always uses short file name mapping, so you might get unexpected results. For example, the following directory contains two files (t.txt2 and t97.txt):

You might expect that typing dir t97\* would return the file t97.txt. However, typing dir t97\* returns both files, because the asterisk wildcard matches the file t.txt2 to t97.txt by using its short name map T97B4

1.TXT. Similarly, typing del t97\* would delete both files.

You can use the question mark (?) as a substitute for a single character in a name. For example, typing dir read. txt lists any files in the current directory with the .txt extension that begin with read and are followed by up to three characters. This includes Read.txt, Read1.txt, Read12.txt, Read123.txt, and Readme1.txt, but not Readme12.txt.

If you use /a with more than one value in attributes, this command displays the names of only those files with all the specified attributes. For example, if you use /a with r and -h as attributes (by using either /a:r-h or /ar-h ), this command will only display the names of the read-only files that aren’t hidden.

If you specify more than one sortorder value, this command sorts the file names by the first criterion, then by the second criterion, and so on. For example, if you use /o with the e and -s parameters for sortorder (by using either /o:e-s or /oe-s ), this command sorts the names of directories and files by extension, with the largest first, and then displays the final result. The alphabetic sorting by extension causes file names with no extensions to appear first, then directory names, and then file names with extensions.

If you use the redirection symbol ( > ) to send this command’s output to a file, or if you use a pipe ( | ) to send this command’s output to another command, you must use /a:-d and /b to only list the file names. You can use filename with /b and /s to specify that this command is to search the current directory and its subdirectories for all file names that match filename. This command lists only the drive letter, directory name, file name, and file name extension (one path per line), for each file name it finds. Before you use a pipe to send this command’s output to another command, you should set the TEMP environment variable in your Autoexec.nt file.

Examples

To display all directories one after the other, in alphabetical order, in wide format, and pausing after each screen, make sure that the root directory is the current directory, and then type:

The output lists the root directory, the subdirectories, and the files in the root directory, including extensions. This command also lists the subdirectory names and the file names in each subdirectory in the tree.

To alter the preceding example so that dir displays the file names and extensions, but omits the directory names, type:

To print a directory listing, type:

When you specify prn, the directory list is sent to the printer that is attached to the LPT1 port. If your printer is attached to a different port, you must replace prn with the name of the correct port.

You can also redirect output of the dir command to a file by replacing prn with a file name. You can also type a path. For example, to direct dir output to the file dir.doc in the Records directory, type:

If dir.doc does not exist, dir creates it, unless the Records directory does not exist. In that case, the following message appears:

To display a list of all the file names with the .txt extension in all directories on drive C, type:

The dir command displays, in wide format, an alphabetized list of the matching file names in each directory, and it pauses each time the screen fills until you press any key to continue.

Оцените статью