Language: EN


Windows Commands for File and Folder Management

I bring you a compilation of useful Windows terminal commands for managing files in bulk.

I was thinking of calling the entry “commands for content creators”, because I mainly use them for the blog. But the truth is that some are also useful for programmers and developers.

In any case, here is a list of some useful commands for managing multiple files and folders in Windows, and other operations that I need to do from time to time.

With a simple command, you can save a lot of time. It can also save you from having to create a program or a script just to solve a problem. Command prompt power!

Of course, you can modify or parameterize them according to your needs. And, also, suggest one if you find it interesting. If I think of any more, I’ll add them here.

List files or folders

Let’s start with the dir command and some of its available parameters (there are more, use dir /? to see all).

# list files and folders

# show only files with an extension (ext in the example)
dir *.ext

# show all types of elements (also junctions, for example)
dir /a

# show without headers
dir /b

# show recursively (in the current folder and subfolders)
dir /b

# show sorted (s by size, e by extension, d by date)
dir /o s 

And now let’s see some more elaborate examples.

List files to a text file

If you want to save a text file with the information output by dir, you just have to use the > operator

dir > output.txt

Remember that you can play with the parameters we have seen before, for example to list only one type of files, or sort by date, etc.

Get a visual representation of files

To get a visual representation in ASCII characters of a tree of folders and files, we can use the tree command. Very useful for documentation, or to explain something in an email etc.

If we only want the folder structure we do,

tree folder

Which would return this

├───folder A
│   └───subfolder A1
└───folder B

And if we also want the files, we do

tree my_folder /f

Which would return

│   file 0.txt

├───folder A
│   │   file A.txt
│   │
│   └───subfolder A1
│           file A1.txt

└───folder B
        file B.txt

Commands to create folders or files

Again, let’s start with the basic commands, remembering how to create an empty file or folder.

# create empty file
echo. > file_name.txt

# create folder
mkdir folder_name

And now let’s go for more complicated examples.

Create sequence of folders or files

To create folders or files in numerical sequence, for example the numbered folders from “hello 0” to “hello 10”.

  ├─📄01 hello/
  ├─📄02 hello/
  ├─📄03 hello/
  └─📄04 hello/

We can use this command to create files 📄

for /L %i in (0, 1, 10) do echo. > "%i"

Or the equivalent version for folders 📁

for /L %i in (0, 1, 10) do mkdir "%i hello"

The for /L loop is especially useful when you need to generate a large number of folders in an automated way.

Create files or folders according to a text file

Now suppose you have a text file with a list of names, and you want to convert each one into a file or folder.

📄file.txt   -->  📁my-folder/
                      ├─ 📄
                      ├─ 📄
                      ├─ 📄
                      └─ 📄

We can do it with this command for files 📄

for /f "tokens=*" %i in (file.txt) do echo. > ""

Or this one for folders 📁

for /f "tokens=*" %i in (file.txt) do mkdir ""

Create folders and move file inside

A somewhat rarer need, but one that I have many times. Take each of the files from a folder, make a folder with the same name, and move the file inside.

📁my-folder/               📁my-folder/  
  ├─📄   -->    ├─📁file01/📄
  ├─📄          ├─📁file02/📄
  └─📄          └─📁file03/📄

We can achieve this with this command.

for %i in (*) do mkdir "%~ni" && move "%i" "%~ni"

I use it because, normally, I have several text files (which are the content of the entries of this blog), and I need to create a folder for each one, to add the images etc.

Concatenate multiple text files

If we have a folder with multiple text files, and we want to concatenate them into a single file, we can use this command

for %i in (*.txt) do type "%i" >> output_file.txt && echo. >> output_file.txt && echo. >> output_file.txt

You can change the type of file you want to concatenate (.md, .bat, whatever), and the name of the file.

If you also want to add to the concatenated file, the name of the original file, and then its content, we can change the command like this.

for %i in (*.txt) do echo %i% >> output_file.txt && type "%i" >> output_file.txt && echo. >> output_file.txt && echo. >> output_file.txt

Commands to copy or move files

Again, we start with the basics, seeing how to copy or move a file.

# copy files to another directory
copy source_file destination_directory

# move or rename files or directories
move source_file destination_file

And now let’s go for more complicated cases. In many cases you can use copy or move, depending on what you need.

Copy a file to several folders

Now we want to copy the same file to all the folders in a directory of folders. For example, imagine you want to copy a file readme.txt to hundreds of folders

📁my-folder-A/    -->    📁my-folder-B/
  ├─📁folder01            ├─📁folder01/📄readme.txt
  ├─📁folder02            ├─📁folder02/📄readme.txt
  ├─📁folder03            ├─📁folder03/📄readme.txt
  └─📁folder04            └─📁folder04/📄readme.txt

We can do it with a single command

for /D %a in (*) do (xcopy /Y readme.txt "%a\")

This command uses a for /D loop to go through all the directories in the current directory and copy the file “readme.txt” to each of them.

Copy folders without the files

Sometimes, you just want to duplicate the folder structure without the files. You can achieve that with these commands:

📁my-folder-A/               ->    📁my-folder-B/
  ├─📁folder01/📄files.*            ├─📁folder01/❌empty
  ├─📁folder02/📄files.*            ├─📁folder02/❌empty
  ├─📁folder03/📄files.*            ├─📁folder03/❌empty
  └─📁folder04/📄files.*            └─📁folder04/❌empty

We can achieve it with the following command,

xcopy source_path destination_path /t

Or with this one, if we also want folders and all their subfolders

xcopy source_path destination_path /t /e

Rename files

Let’s go with the group of renaming files. Let’s start by remembering how to rename a file

ren file_path new_name

Add prefix to multiple files

One of the most common requests, add a prefix or a suffix to all the files in a folder.

📁my-folder/               📁my-folder/  
  ├─📄   -->    ├─📄prefix -
  ├─📄          ├─📄prefix -
  └─📄          └─📄prefix -

This command adds the prefix “prefix - ” to all the files in the current directory. You can customize the prefix according to your needs.

for %a in (*.*) do ren "%a" "prefix - %a"

Similarly, adding a suffix would be like this

for %a in (*.*) do ren "%a" "%a - suffix"

Of course, you can modify the (*.*) to filter a type of files. For example, only .jpg files, etc

Rename files in recursive folders

Another example, you have to rename a file in a series of folders, and recursively in all their subfolders.

📁my-folder/                         📁my-folder/  
  ├─📁folder01/📄readme.txt   -->    ├─📁folder01/📄
  ├─📁folder02/📄readme.txt          ├─📁folder02/📄
  └─📁folder03/📄readme.txt          └─📁folder03/📄

We can do it like this,

for /R %G in (readme.txt) do ren "%G" ""

This command will search for all the files with the name readme.txt recursively in all subdirectories and rename them as “”.

Delete files or folders

Finally, some basic commands to delete files and folders.

# Delete a file
del file_name

# Delete all files in the current directory
del /Q /F *.*

# Delete directory
rmdir directory_name

# Delete folders and all its contents recursively
rmdir /S /Q directory_name