Examples of file management using .cmd commands in Windows

Last update: 15/12/2025
Author Isaac
  • Use of commands CMD Basic and advanced methods for managing files and folders in Windows.
  • Practical examples of automation with .cmd batch files and FOR loops.
  • Differences between file management tasks in CMD and in terminal Linux.
  • Key diagnostic and system commands useful alongside file management.

CMD terminal with file management examples

If you often work with many documents, folders, or projects, learning to use Windows commands and .cmd files It can save you a huge amount of time. Instead of clicking one by one, you can automate repetitive tasks like copying, moving, renaming, creating entire directory structures, or even performing small system audits from the console.

In this article you will see Real-world examples of file and folder management with CMDCombining basic commands with more advanced ones and loops. References to diagnostic utilities are also included, and where relevant, comparisons are made with equivalent commands on Linux-like systems to give you a more complete overview. The idea is that you can copy, adapt, and reuse these examples in your own .cmd scripts and learn to Automate file tasks in Windows.

Basic commands for managing directories and files in CMD

CMD window showing commands for managing files

Before we delve into scripts and automation, it's advisable to have a good grasp of the Essential directory and file commands that you'll use all the time in the Symbol of the system of Windows.

To navigate through system folders, you mainly use CD and CHDIRBoth do the same thing: display or change the current directory. Typical examples would be cd \ to get to the root of unity, cd .. to level up or cd /d D:\Proyectos to change drive and folder in one step.

If you want to see what's inside a folder, the star command is DIRWithout parameters, dir Lists files and subdirectories of the current directory, but with dir /b You will only get the names in simple format, and with dir /a You will also include hidden and system elements. Other useful modifiers are /o To sort (by name, size, date, etc.), and for advanced searches you can use tools from instant file search.

To create new folders you have MD y MKDIRwhich are equivalent: mkdir NuevaCarpeta o md Datos\2025The system doesn't automatically switch to that folder, it only creates it, so you'll have to navigate to it later. cd if you want to enter.

When you need to delete directories, you have RD and RMDIRThe basic use would be rmdir NombreCarpetawhich only works if the folder is empty. To delete a folder with all its contents, you usually use rmdir /s /q CarpetaWhere /s deletes subdirectories and files and /q avoid confirmations.

As for individual files, the most direct command to delete them is TIME (or its alias ERASE). You can use del archivo.txt, del /q /f *.* to force silent deletion even of read-only files or del /s *.tmp to clean temporary files from an entire folder structure.

To copy files, you use COPYwhich is used to duplicate one or more files in another location, for example copy informe.docx D:\CopiasTo move or rename them, you need to... MOVEwhich is used both to change a file's folder and to give it a new name: move viejo.txt nuevo.txt o move archivo.txt C:\Destino.

The most massive name changes are done with REN or RENAMEIts basic form is ren archivo1.txt archivo2.txtBut it also supports wildcards, which allows you to modify many files at once, something we'll see later combined with loops.

List, document, and visualize file structures

Directory tree displayed in console

One of the common tasks when managing a lot of content is obtain file listings or directory maps to document projects, make inventories, or simply have a clear reference.

The most basic command for listing is DIRBut combined with redirects, it allows you to generate reports. For example, dir /b > listado.txt Create a text file containing only the names of the items in the current folder. You can apply filters such as dir *.ext /o:-s > lista_ext.txt to list only files of a specific extension and sort them.

If you need a more visual representation of the structure, the command TREE Displays the directory tree using ASCII characters. tree Ruta You'll see the folders from that point on, and if you add the modifier /f (for example tree C:\Proyectos /fYou'll also see the files contained in each directory, and for a faster file preview, it's best to use tools like instant file preview.

These lists are very useful for technical documentation, emails or manualsbecause they allow you to visualize at a glance the organization of a project, from the main folders to the final files that make it up.

  Dual-booting Windows and Linux: How to Choose the Default Operating System

To display the contents of text files without opening an editor, CMD offers the following: TYPE. With type notas.txt The content is printed to the console. If the file is very long, it is common to combine it with MOREusing something like type log.txt | more to view the content page by page.

CMD also remembers previous commands thanks to DOSKEYThis command allows you to retrieve and edit previous commands. It's not a file listing command, but combined with pattern repetition, it can greatly speed up your workflow when creating multiple similar listings.

Mass creation of files and folders with FOR loops

When you need to create dozens or hundreds of items, doing it manually is insane. That's what the command is for. FORwhich in .cmd files becomes one of the most powerful tools for automating tasks.

A very useful variant is FOR /Lwhich iterates through a range of numbers. For example, to create numbered files you could use something like:
for /L %i in (0,1,10) do echo. > "%i hola.md"This command generates files from "0 hola.md" to "10 hola.md". If you want folders instead of files, simply change echo. > by mkdir.

Another typical case is when you have a list of names in a text file And you want to convert each line into a file or directory. That's where this comes in. FOR /F. For example: uterine
for /f "tokens=*" %i in (nombres.txt) do mkdir "%i"
will create a folder for each line of nombres.txtSimilarly, you could generate files with echo. > "%i.md" inside the loop.

A very practical way to organize projects is to create a folder for each existing file and move into its corresponding fileWith something like:
for %i in (*) do mkdir "%~ni" && move "%i" "%~ni"
The system creates a directory with the same name as the file (without the extension) and moves it into it automatically.

FOR loops are also useful for concatenate multiple text files in one. For example:
for %i in (*.txt) do type "%i" >> salida.txt && echo. >> salida.txt
It reads all the .txt files in the folder, dumping their contents into salida.txt and adding line breaks between them to make it more readable.

If you want the resulting file to also include the name of each original file Before its content, you can modify the loop:
for %i in (*.txt) do echo %i >> salida.txt && type "%i" >> salida.txt && echo. >> salida.txtThis will give you something similar to a concatenated index.

Copying, moving, and cloning folder structures

File management in Windows isn't limited to copying a document from one place to another. Often you need to... clone entire structures, replicate a file across many folders, or move large volumes of data with precision.

For simple copies, COPY It works well: copy archivo.txt D:\DestinoIf you want to copy multiple files at once, you can use wildcards: copy *.docx C:\InformesThis command also allows you to concatenate files when using + signs, although for more complex tasks it is usually better to use FOR loops as we have seen before.

When you need something more robust, this comes into play. XCOPYwhich copies entire directory trees. With xcopy Origen Destino /t You only clone the folder structure, without files, whereas with /t /e You also include empty subdirectories. This is perfect for mounting project templates or duplicate complex architectures without yet dragging the content.

For really advanced copying, it's very popular in modern Windows. ROBOCOPYIt allows retries, resuming interrupted backups, filtering by date, size, attributes, etc. A simple example would be:
robocopy C:\Origen D:\Destino /E
This copies all files and subfolders, including empty ones. Additionally, to save space you can compress and decompress files with commands when preparing copies. It's ideal for data migrations or synchronize large folders.

If what you want is copy a single file into all subfolders of a directory (for example, a readme.txt), you can combine FOR with XCOPY:
for /D %a in (*) do xcopy /Y readme.txt "%a". The modifier /D This makes the loop iterate only through directories.

Regarding moving items, the command MOVE It is used both to move files between folders and to rename directories. For example:
move C:\Temp\archivo.txt C:\Datos\archivo.txt
O well:
move CarpetaAntigua CarpetaNueva
to rename a folder while keeping it at the same level.

  Disk Cache Leaks in Windows 11: Causes, Symptoms, and Solutions

Mass renaming and filename manipulation

Another very common task in administration with CMD is rename many files at onceFor example, to add a prefix, a suffix, or change an extension in a homogeneous way.

The key command here is REN (or RENAME). For something simple, a ren foto1.jpg viaje1.jpgBut its true power emerges when you combine it with wildcards and FOR loops. For example, to append text to all files in a folder, you could use:
for %a in (*.*) do ren "%a" "prefijo - %a".

If what interests you is add a suffix For each file, the pattern would be similar:
for %a in (*.*) do ren "%a" "%~na - sufijo%~xa"
where %~na represents the name without the extension and %~xa the original extension. This way you avoid breaking the extensions.

When you need to rename the same file in many folders and subfolders, you can use FOR /R, which recursively traverses a directory tree. An example:
for /R %G in (readme.txt) do ren "%G" "readme.md"
will search for all the readme.txt from the current directory downwards and will change them to readme.md.

Keep in mind that REN only works with names, so if you want to make more complex or pattern-based substitutions, you might want to combine CMD with other tools or languages, but for quick sorting and cleaning tasks REN together with FOR covers most needs.

Securely delete files and directories

In any administrative task, there comes a time to delete files and folders that are no longer neededDoing it right from CMD requires knowing a few options to avoid surprises or unnecessary confirmations.

For individual files, the standard command is TIMEYou can use it without parameters, but it's more convenient to work with:
del /Q archivo.log
so that it doesn't ask for confirmation, or with /F If you want to force the deletion of read-only files, or to delete many files of a specific extension throughout an entire tree, del /S *.bak It will be responsible for traversing subdirectories.

If you need to delete the contents of a folder but leave the folder empty, you can combine OF and FOR to fine-tune what you eliminate and what you don't, or to draw from del /Q /F *.* within the desired directory, checking beforehand that there is nothing critical.

Regarding directories, as seen earlier, RMDIR or RD with parameter /S They delete both the folder and everything it contains: rmdir /S /Q C:\Temp\Antiguo. Use /Q It avoids confirmations and is common in automated scripts.

Although these commands delete files for the user, at a low level they are not always unrecoverable, so in sensitive environments it is advisable to combine this cleanup with specific secure erasure toolsFrom the point of view of daily administration, however, DEL and RMDIR are usually sufficient.

Advanced CMD commands related to files and the system

In addition to purely file operations, the Windows Command Prompt includes a good repertoire of advanced commands that affect attributes, permissions, or data integrity, very useful once you're already familiar with the basics.

A classic is ATTRIB, which displays or modifies the attributes of a file: read-only, hidden, system, etc. For example:
attrib +r +h archivo.txt
It marks it as read-only and hidden, while with minus signs (-r -hYou would remove those attributes. This can be applied recursively with /S already directories with /DFor more in-depth inspections of attributes and encryption, consult inspect file attributes and encryption.

The associations between file extensions and file types are managed through ASSOC and FTYPEASSOC relates a file extension (for example, .txt) to a logical file type, and FTYPE associates that type with a specific command. Together they allow customize how certain files are opened from the browser or from the command line.

To work with permits NTFS is ICACLSThis tool generates, modifies, and restores access control lists (DACLs). With it, you can, for example, export folder permissions to a file and then restore them to another location—very useful when moving sensitive data between servers or disks.

If you suspect that there are corrupted files, SFC y CHKDSK They are your allies. SFC checks the integrity of system files and repairs corrupted ones using cached copies, while CHKDSK checks both the logical structure of the file system and the disk surface, being able to mark bad sectors and correct directory problems.

  exFAT vs FAT32 vs FAT vs FAT16: Differences, Advantages, and Uses Explained

For more general administrative tasks, DISKPART It allows you to manage partitions and volumes from the console, FSUTIL It offers advanced features for volumes and file systems, and BCDEDIT It is used to modify the configuration of Boot of Windows. Although they are not file commands in the classic sense, they directly affect how and where data is stored and managed.

Environment variables, .cmd scripts, and execution flow

When you start writing your own batch files .cmdIt's not enough to know individual commands: you need to control the execution flow, the variables, and the console environment.

The command SET It allows you to display, create, or modify environment variables. For example, set RUTA_PROYECTO=C:\Proyectos\Web define a variable that you can then reuse in the same script , the %RUTA_PROYECTO%To ensure that changes are limited to the scope of the batch, the following are used: SETLOCAL and ENDLOCAL, which start and end a section with local variables.

With IF You can perform conditional processing within the .cmd file, for example, checking if a file exists before copying or renaming it. Meanwhile, GOTO allows jumping to specific tags within the script to control the execution logic, and CALL It is used to invoke other batch files from a main one.

To pause a script while waiting for user intervention, you use BREAKwhich displays a generic message and waits for you to press a key, and with ECHO You can control what is displayed on the screen (including turning the display of commands on or off with echo on/off).

Commands like PUSHD and POPD They make it easy to change directories within a batch without losing track of your current location: PUSHD saves the current directory and moves to the new one, and POPD returns you to the saved directory. This is very useful when a script jumps between many different routes for making copies, listings, or cleaning.

Other commands such as SHIFT (shift parameters), REM (comments) or TITLE (change window title) complete the ecosystem for creating more readable, reusable, and easier-to-debug .cmd files.

Diagnostic and system commands useful for administrators

Although the main intention is to manage files, in day-to-day use you will often resort to diagnostic commands and system which are also executed from CMD and fit very well into maintenance scripts, and even Print files from CMD.

To get an overview of the machine, Systeminfowhich returns data about the operating system, processor, RAM, boot time, hotfix installed, etc. It is ideal for documenting equipment or conducting quick audits.

In the realm of networking, IPCONFIG It displays interface information, including IP address, mask, and gateway; NETSTAT It teaches active connections and protocol statistics; and Tracert It allows you to track the path of packets to a specific destination, which is very useful for diagnosing connectivity problems.

If you want to review running processes, Tasklist List everything that is running along with its memory usage, while TASKKILL It allows you to close a process by specifying its PID or image name, for example taskkill /PID 1234.

To find out the exact version of Windows you have MOREHowever, for more detailed information, it's best to return to SYSTEMINFO. If you want to check installed drivers, DRIVERQUERY Returns the complete list with module names and controller types.

Regarding disk maintenance, in addition to CHKDSK, the following appear Defrag to defragment mechanical drives and CLEANMGR To launch Disk Cleanup from the console. And to schedule operations for specific times you have SCHTASKSwhich allows you to create, view, modify and delete scheduled tasks.

Finally, when you want to shut down, restart, or log off directly from CMD, you can use Shutdown with different parameters (for example shutdown -s -t 60 to turn off in 60 seconds or shutdown -r to restart) and Logoff to log off the current user while keeping the computer turned on.

All these commands, combined with those for managing files and directories, allow you to build very complete .cmd scripts that automate everything from backups and content organization to small system administration and diagnostic tasks, reducing manual errors and gaining a lot of speed in daily work.

Preview unopened files with QuickLook
Related article:
File preview in Windows: QuickLook, Peek, and native panel