Windows cmd 按文件类型对文件进行排序并将它们存储在文件夹中

Mat*_*hák 1 sorting cmd file-type batch-file

我想要一个批处理文件,它将按文件类型对文件进行排序并将它们分类到文件夹中。例如,我将在某个文件夹中运行此批处理文件,而 .PDF 文件将保存在“PDF”文件夹中,与其他文件类型相同。可以在命令行中做到这一点吗?谢谢你。

mih*_*dis 5

请将下面的代码放在一个 .bat 文件中,并将其与文件一起保存到您的文件夹中并运行它。

@echo off
rem For each file in your folder
for %%a in (".\*") do (
    rem check if the file has an extension and if it is not our script
    if "%%~xa" NEQ ""  if "%%~dpnxa" NEQ "%~dpnx0" (
        rem check if extension forlder exists, if not it is created
        if not exist "%%~xa" mkdir "%%~xa"
        rem Copy (or change to move) the file to directory
        copy "%%a" "%%~dpa%%~xa\"
    )
)
Run Code Online (Sandbox Code Playgroud)