我想使用 Windows CMD 批处理自动将文件移动到部分相似的文件夹中。
首先,有下面几个文件。
29100.txt
29103.doc
30011.ppt
30013.txt
40100.xls
其次,将根据文件名创建各个文件夹,并将其移动到各个文件夹中。
29100.txt -> move to \29100\29100.txt
29103.doc -> move to \29103\29103.doc
30011.ppt -> move to \30011\30011.ppt
30013.txt -> move to \30013\30013.txt
40100.xls -> move to \40100\40100.xls
最后,我想将部分同名文件移动到通用命名文件夹中。
29100\29100.txt -> move to \291\29100.txt
29103\29103.doc -> move to \291\29103.doc
30011\30011.ppt -> move to \300\30011.ppt
30013\30013.txt -> move to \300\30013.txt
40100\40100.xls -> move to \400\40100.xls
因此,我向 Superuser 上的用户询问并进行了如下回复,我的背景知识非常贫乏。对不起。而且它不需要像 Second explain 那样制作每个文件夹名称,只需直接移动到常用文件夹名称,如 \291、\300、\400,我解释得不好,再次抱歉。
@echo off
setlocal enabledelayedexpansion
for %%i in (*) do mkdir "%%~ni"
for %%i in (*) do move "%%~i" "%%~ni"
for /d %%i in (*) do (
set "x=%%i"
echo "%%i" "!x:~0,3!"
ren "%%i" "!x:~0,3!"
)
Oh! I found the answer from other question and I modified little
for /f "delims=" %%F in (
'Dir /b *.*^|findstr "^[0-9]" '
) do call :subr "%%F"
exit /b
:subr
set "file=%~n1"
set "fold=%file:~0,3%
if not exist "%fold%" md "%fold%"
move %1 "%fold%"
exit /b
I don't know exactly the meaning of the batch but it works perfectly for me. Thank you all!