将文件移动到按字母顺序编目的子目录

Jam*_*Jr. 6 windows powershell command-line cmd.exe

.docx在同一目录中有多种相同类型(例如“ ”)的“文件”(不是文件夹)。

此外,在该 SAME 目录中,我有一个名为“ catalog”的子目录,其中包含按字母顺序排列的文件夹(即,以#ABCDEF、 等开头 ...)。

假设这些文件位于“ D:/documents/”,我将如何[使用 Windows CMD 或 Windows Powershell] 对文件列表进行排序,并将它们移动到“目录”文件夹中,放入正确的 [区分大小写]“字母顺序”文件夹中, 按 [case- insensitive ] 文件名的第一个字母排序?

例如: [ D:/documents/janet_henderson.docx] 将移动到 [ D:/documents/catalog/J/janet_henderson.docx]

请注意,我需要将名称以任意数字开头的文件放入“ D:/documents/catalog/#/”文件夹。


到目前为止我所拥有的

我在organize_files.bat文件中有以下内容:

move  documents\A*.docx  documents\catalog\A\
move  documents\B*.docx  documents\catalog\B\
move  documents\C*.docx  documents\catalog\C\
move  documents\D*.docx  documents\catalog\D\
move  documents\E*.docx  documents\catalog\E\
move  documents\F*.docx  documents\catalog\F\
move  documents\G*.docx  documents\catalog\G\
move  documents\H*.docx  documents\catalog\H\
move  documents\I*.docx  documents\catalog\I\
move  documents\J*.docx  documents\catalog\J\
move  documents\K*.docx  documents\catalog\K\
move  documents\L*.docx  documents\catalog\L\
move  documents\M*.docx  documents\catalog\M\
move  documents\N*.docx  documents\catalog\N\
move  documents\O*.docx  documents\catalog\O\
move  documents\P*.docx  documents\catalog\P\
move  documents\Q*.docx  documents\catalog\Q\
move  documents\R*.docx  documents\catalog\R\
move  documents\S*.docx  documents\catalog\S\
move  documents\T*.docx  documents\catalog\T\
move  documents\U*.docx  documents\catalog\U\
move  documents\V*.docx  documents\catalog\V\
move  documents\W*.docx  documents\catalog\W\
move  documents\X*.docx  documents\catalog\X\
move  documents\Y*.docx  documents\catalog\Y\
move  documents\Z*.docx  documents\catalog\Z\
Run Code Online (Sandbox Code Playgroud)
  • 有没有更好的方法来完成这个操作,比如把它放在一个循环中?
  • 但是,如何处理以数字开头的文件?
  • 检查文件名时,是否需要告诉 Windows CMD 不区分大小写?

Vom*_*yle 5

将文件排列到按字母顺序编目的子目录中

例如: [ D:/documents/janet_henderson.docx] 将移动到 [ D:/documents/catalog/J/janet_henderson.docx]

另外,注意:我希望文件名以数字开头的文件很明显进入"D:/documents/catalog/#/"文件夹。

根据您如何解释这一点和您的示例,下面是一些示例命令提示符复制和粘贴项目,以及将按照您的描述完成这些操作的批处理脚本项目。

假设

  • #已经创建了所有带字母的目录和目录。
  • 对于下面前两个示例,您永远不会MOVE将具有相同名称的相同文件放到已经存在具有相同名称的文件的目录中。
  • 对于下面最后两个示例 如果已存在具有相同名称的相同文件,您将始终使用MOVE源到目标的命令覆盖现有文件。

命令提示符复制(如果文件已存在则提示)

@ECHO ON

SET Letters=(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#

FOR %A IN %Letters% DO MOVE "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\"
FOR %B IN %Numbers% DO MOVE "%SourceDir%\%~B*.*" "%DestNumDir%\"
GOTO EOF
Run Code Online (Sandbox Code Playgroud)

批处理脚本(如果文件已存在则提示)

@ECHO ON

SET Letters=(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#

FOR %%A IN %Letters% DO MOVE "%SourceDir%\%%~A*.*" "%DestLetterDir%\%%~A\"
FOR %%B IN %Numbers% DO MOVE "%SourceDir%\%%~B*.*" "%DestNumDir%\"
GOTO EOF
Run Code Online (Sandbox Code Playgroud)

命令提示符复制(强制覆盖)

@ECHO ON

SET Letters=(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#

FOR %A IN %Letters% DO ECHO Y | MOVE "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\"
FOR %B IN %Numbers% DO ECHO Y | MOVE "%SourceDir%\%~B*.*" "%DestNumDir%\"
GOTO EOF
Run Code Online (Sandbox Code Playgroud)

批处理脚本(强制覆盖)

@ECHO ON

SET Letters=(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#

FOR %%A IN %Letters% DO ECHO Y | MOVE "%SourceDir%\%%~A*.*" "%DestLetterDir%\%%~A\"
FOR %%B IN %Numbers% DO ECHO Y | MOVE "%SourceDir%\%%~B*.*" "%DestNumDir%\"
GOTO EOF
Run Code Online (Sandbox Code Playgroud)

进一步阅读和资源