我想要一个批处理文件,它将按文件类型对文件进行排序,并将它们分类到文件夹中。 例如,我将在一些文件夹中运行此批处理文件,.PDF文件将保存在“PDF”文件夹中,与其他文件类型相同。可以在命令行中执行此操作吗? 谢谢。
答案 0 :(得分:4)
请将下面的代码放在.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\"
)
)
答案 1 :(得分:0)
试试这个:
@echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "fol=%%~xa" & set "fol=!fol:.=!"
if not exist !fol! md !fol!
for /f %%b in ('dir /on /b *.*') do (
if %%~xb EQU .!fol! move %%~nxb !fol!
)
)
答案 2 :(得分:0)
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET destdir=c:\destdir
SET "extdone=:"
FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
SET ext=%%~xa
IF DEFINED ext (
SET extdone|FIND /i ":%%~xa:" >NUL
IF ERRORLEVEL 1 (
SET extdone=:%%~xa: !extdone!
IF EXIST "%destdir%\!ext:~1!" (
ECHO MOVE "*%%~xa" "%destdir%\!ext:~1!"
) ELSE (ECHO no MOVE "%%~xa")
)
)
)
GOTO :EOF
此批次应该按照您的要求进行。
所需命令仅用于ECHO
以用于测试目的。在您确认命令正确无误后,将ECHO MOVE
更改为MOVE
以实际移动文件。
我认为如果destinationdirectory\extensionfound
存在,您只希望移动文件。如果要为新扩展创建目录,只需添加一行
md "%destdir%\!ext:~1!" 2>nul
在SET extdone=:%%~xa: !extdone!
行之后。