Here is a compilation of useful Windows terminal commands for managing files in bulk.
I was thinking of calling this post “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 cheat sheet of some useful commands for managing multiple files and folders in Windows, and other operations 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 write a program or script just to solve a problem. Command console to the rescue!
Of course, you can modify or parameterize them according to your needs. And even suggest some to me if you think they are interesting. If I think of any more, I’ll add them here.
List Files or Folders
We start with the dir command and some of its available parameters (there are more, use dir /? to see them all).
list files and folders
dir
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 header
dir /b
show recursively (in 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, simply use the > operator.
dir > output.txt
Remember you can play with the parameters we saw before, for example to list only one type of file, or sort by date, etc.
Get a Visual Representation of Files
To get a visual representation in ASCII characters of a folder and file tree, 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
├───folder A
│ └───subfolder A1
└───folder B
And if we also want the files, we do:
tree my_folder /f
Which would return:
my_folder
│ 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. > nombre_archivo.txt
create folder
mkdir nombre_carpeta
And now let’s move on to more complicated examples.
Create a Sequence of Folders or Files
To create folders or files in numerical sequence, for example folders numbered from “hola 0” to “hola 10”.
📁my-folder/
├─📄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 hola.md”
Or the equivalent version for folders 📁
for /L %i in (0, 1, 10) do mkdir “%i hola”
The for /L loop is especially useful when you need to generate a large number of folders automatically.
Create Files or Folders Based on 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/
├─ 📄line01.md
├─ 📄line02.md
├─ 📄line03.md
└─ 📄line04.md
We can do it with this command for files 📄
for /f “tokens=*” %i in (fichero.txt) do echo. > “%i.md”
Or this other one for folders 📁
for /f “tokens=*” %i in (fichero.txt) do mkdir “%i.md”
Create Folders and Move File Inside
A somewhat rarer need, but one I have a lot of times. Take each file in a folder, make a folder with the same name, and move the file inside.
📁my-folder/ 📁my-folder/
├─📄file01.md --> ├─📁file01/📄file01.md
├─📄file02.md ├─📁file02/📄file02.md
└─📄file03.md └─📁file03/📄file03.md
We can achieve it 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 this blog’s posts), and I need to create a folder for each one, to add images, etc.
Concatenate Several Text Files
If we have a folder with several text files, and we want to concatenate them into a single file, we can use this command:
for %i in (*.txt) do type “%i” >> archivo_salida.txt && echo. >> archivo_salida.txt && echo. >> archivo_salida.txt
You can change the type of file you want to concatenate (.md, .bat, whatever), and the output file name.
If you also want to add the original file name to the concatenated file, and then its content, we can change the command like this.
for %i in (*.txt) do echo %i% >> archivo_salida.txt && type “%i” >> archivo_salida.txt && echo. >> archivo_salida.txt && echo. >> archivo_salida.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 archivo_origen directorio_destino
move or rename files or directories
move archivo_origen archivo_destino
And now let’s move on to more complicated cases. In many cases, you can use copy or move, depending on what you need.
Copy a File to Multiple Folders
Now we want to copy the same file into all the folders in a directory. For example, imagine you want to copy a readme.txt file into a gazillion 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 directories in the current directory and copy the “readme.txt” file to each one.
Copy Folders Without the Files
Sometimes, you only want to duplicate the folder structure without the files. You can achieve it 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 path_source path_destination /t
Or with this one, if we also want folders and all their subfolders:
xcopy path_source path_destination /t /e
Rename Files
Let’s move on to the group of renaming files. We start by remembering how to rename a file.
ren path_file new_name
Add Prefix to Multiple Files
One of the most common requests, add a prefix or a suffix to all files in a folder.
📁my-folder/ 📁my-folder/
├─📄file01.md --> ├─📄prefix - file01.md
├─📄file02.md ├─📄prefix - file02.md
└─📄file03.md └─📄prefix - file03.md
This command adds the prefix “prefix - ” to all 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 - sufix”
Of course, you can modify the (*.*) to filter a type of file. For example, only .jpg files, etc.
Rename Files in Folders Recursively
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/📄readme.md
├─📁folder02/📄readme.txt ├─📁folder02/📄readme.md
└─📁folder03/📄readme.txt └─📁folder03/📄readme.md
We can do it like this:
for /R %G in (readme.txt) do ren “%G” “readme.md”
This command will search for all files named readme.txt recursively in all subdirectories and rename them as “readme.md”.
Delete Files or Folders
Finally, some basic commands to delete files and folders.
Delete a file
del nombre_fichero
Delete all files in the current directory
del /Q /F .
Delete directory
rmdir nombre_directorio
Delete folders and all their content recursively
rmdir /S /Q nombre_directorio

