本文列出了可用于在 Windows 11/10中通过命令行(Command-Line )管理文件和文件夹(manage files and folders )的各种命令。尽管许多用户更喜欢使用图形用户界面来管理文件以获得无忧体验,但有些用户还使用命令行界面来执行文件管理任务。无论如何,最好知道执行任务的替代解决方案。
在本指南中,我将创建一个有用的命令列表,可用于在 Windows 10 PC 上进行文件或文件夹管理。要对文件或文件夹执行特定任务,您需要在CMD中输入一个专用命令。让我们看看这些命令!
通过CMD管理文件和文件夹的命令(CMD)
以下是在Windows 11/10中使用命令提示符(Command Prompt)管理文件和文件夹时应了解的命令:
1]在CMD中创建文件或文件夹
要创建文件夹,请键入文件夹名称以及要创建文件夹的位置。这是命令:
mkdir <folder name with path>
例如;
mkdir C:\Users\KOMAL\Documents\TWC
要创建特定大小(以字节为单位)的文件,请使用以下命令:
fsutil file createnew file.txt 4000
代替file.txt
,输入文件名及其扩展名和完整路径。并且,4000
是以字节为单位的文件大小。
相关(Related):如何使用命令提示符和 PowerShell 创建多个文件夹(Create Multiple Folders using Command Prompt and PowerShell)。
2]删除CMD中的文件或文件夹
您可以使用以下命令删除文件夹:
rmdir <folder name with path>
为了删除文件,命令是:
del "<filename with path>"
如果要删除当前文件夹中的所有文件,请输入命令:
del *
要仅删除具有特定扩展名的文件,例如 png,请使用以下命令:
del *.png
如果要删除文件名中包含特定字符串的文件,例如xyz,可以使用以下命令:
del *xyz*
3]在特定文件夹中查找文件(Find Files)
要根据不同的参数在文件夹中查找文件,首先需要使用以下命令导航到文件夹:
cd "<folder name with location>"
现在,您可以使用以下命令在特定文件夹中查找超过 n 天的文件:( find files older than n days)
forfiles /s /m *.* /d -n /c "cmd /c echo @file
替换-n
为天数。例如,如果您想查找超过 2 天的文件,请键入-2
.
要查找大于特定大小的文件(find files larger than a specific size),请使用以下命令:
forfiles /S /M * /C "cmd /c if @fsize GEQ 3741824 echo @path"
在上面的命令中,3741824
是文件大小来搜索大于这个大小的文件。
4]一次重命名(Rename)文件夹中存在的所有文件扩展名
您还可以在CMD中(CMD)批量重命名文件扩展名。假设(Suppose)您要将所有图片的文件扩展名重命名为JPG,您可以使用以下命令:
ren *.* *.jpg
5]获取文件创建时间和日期
要检查特定文件的创建时间和日期,请使用以下命令:
dir /T:C filename
6]检查文件中的字符串
要查找文件中包含特定字符串的所有行,可以使用以下命令:
findstr string file-name
例如,要在文本文件中显示所有带有“twc”的行,您需要输入命令:
findstr twc twc.txt
请记住,上述命令区分大小写。
要查找具有任何指定字符串的句子,请使用如下命令:
findstr /C:"string1 string2 string3..." filename
7]检查(Check)文件夹中的所有隐藏文件(Hidden)
使用以下命令获取目录中隐藏文件的列表:
dir /A:H /B
8]在CMD中压缩文件
压缩文件夹中文件的命令是:
compact /c filename
9]通过CMD隐藏/取消隐藏文件
要隐藏文件,使用的命令是:
attrib + h filename
您可以使用以下命令再次取消隐藏文件:
attrib -h filename
10]Set/ Unset Read-Only属性
要使文件只读,命令是:
attrib +R filename
如果要从文件中删除只读属性,命令是:
attrib -R filename
11]重命名文件/文件夹的命令
rename oldfilename.pdf newfilename.pdf
12]在CMD中读取文件内容
您可以使用以下命令读取CMD中的文本文件内容:
more filename
13]在默认应用程序(Default Application)中(File)打开(Open)文件
您可以通过输入一个简单的命令在其默认应用程序中打开文件:
"filename-with-path"
14]Move File / Folder到不同的位置
假设您想将TWC12.pdf文件移动到G 盘的TWC文件夹中,请使用以下命令:
move TWC12.pdf G:\TWC\
移动具有特定扩展名的所有文件的命令:
move *.png G:\TWC\
要移动以特定字母开头的文件,比如 A,命令是:
move A* G:\TWC\
同样,您可以使用如下命令移动文件夹:
move foldername <new location>
例如:
move TWC1 G:\TWC\
15]复制文件的命令
您可以使用以下命令将文件从一个位置复制到另一个位置:
copy Sourcefolder DestinationFolder
希望本文能帮助您学习一些有用的命令,通过Windows 11/10中的命令行管理文件和文件夹。
Useful Commands to Manage Files and Folders using Command Prompt
This article lists down variоus commands that you can use to manage files and folders through Command-Line in Windows 11/10. Although a lot of users prefer using a graphical user interface to manage files for a hassle-free experience, some also use the command-line interface to perform file management tasks. In any case, it is always better to know alternative solutions to execute a task.
In this guide, I will be creating a list of useful commands that you can use for file or folder management on your Windows 10 PC. To perform a specific task on files or folders, there is a dedicated command that you need to enter in CMD. Let’s check out these commands!
Commands to Manage Files and Folders through CMD
Here are the commands that you should know to manage files and folders using Command Prompt in Windows 11/10:
1] Create a File or Folder in CMD
To create a folder, type the folder name with the location where you want to create the folder. Here is the command:
mkdir <folder name with path>
For example;
mkdir C:\Users\KOMAL\Documents\TWC
To create a file of a specific size (in bytes), use the below command:
fsutil file createnew file.txt 4000
In place of file.txt
, enter the filename with its extension and full path. And, 4000
is the file size in bytes.
Related: How to Create Multiple Folders using Command Prompt and PowerShell.
2] Delete Files or Folder in CMD
You can remove a folder using the below command:
rmdir <folder name with path>
In order to delete a file, the command is:
del "<filename with path>"
If you want to delete all files from the current folder, enter the command:
del *
To delete files with a specific extension only, say png, use the command:
del *.png
If you want to delete files with a particular string in their filename, e.g., xyz, you can use the below command:
del *xyz*
3] Find Files in a Particular Folder
To find files inside a folder based on different parameters, you first need to navigate to the folder using the command:
cd "<folder name with location>"
Now, you can find files older than n days in a specific folder using the below command:
forfiles /s /m *.* /d -n /c "cmd /c echo @file
Replace -n
with the number of days. Like if you want to find files older than 2 days, type -2
.
To find files larger than a specific size, use the command:
forfiles /S /M * /C "cmd /c if @fsize GEQ 3741824 echo @path"
In the above command, 3741824
is the file size to search files greater than this size.
4] Rename all file extensions present in a folder at once
You can also batch rename file extensions in CMD. Suppose, you want to rename the file extension of all images to JPG, you can use the below command:
ren *.* *.jpg
5] Get File Creation Time and Date
To check the creation time and date of a specific file, use the command:
dir /T:C filename
6] Check for a string inside a file
To find all lines containing a particular string in a file, you can use the command:
findstr string file-name
For example, to display all lines with “twc” in a text file, you need to enter the command:
findstr twc twc.txt
Do remember that the above command is case-sensitive.
To find sentences with any specified string, use a command like:
findstr /C:"string1 string2 string3..." filename
7] Check for all Hidden Files in a Folder
Use the below command to get a list of hidden files in a directory:
dir /A:H /B
8] Compress a File in CMD
The command to compress a file in a folder is:
compact /c filename
9] Hide/ Unhide a file through CMD
To hide a file, the command used is:
attrib + h filename
You can unhide the file again using the command:
attrib -h filename
10] Set/ Unset Read-Only attribute to a file
To make a file read-only, the command is:
attrib +R filename
If you want to remove the read-only attribute from a file, the command is:
attrib -R filename
11] Command to Rename a File/Folder
rename oldfilename.pdf newfilename.pdf
12] Read File Content in CMD
You can read text file content in CMD using the below command:
more filename
13] Open a File in Default Application
You can open a file in its default application by entering a simple command:
"filename-with-path"
14] Move File / Folder to different Location
Suppose you want to move TWC12.pdf file to TWC folder in G drive, use below command:
move TWC12.pdf G:\TWC\
Command to move all files with a specific extension:
move *.png G:\TWC\
To move files starting with a particular letter, say A, command is:
move A* G:\TWC\
Similarly, you can move a folder using a command like below:
move foldername <new location>
For example:
move TWC1 G:\TWC\
15] Command to Copy Files
You can copy files from one location to another using command:
copy Sourcefolder DestinationFolder
Hope this article helps you learn some useful commands to manage files and folders through the command line in Windows 11/10.