想要将不同名称的多个文件复制到同名(文件名)文件夹。

时间:2014-05-15 23:54:24

标签: batch-file copy command xcopy

例如,将文件file1.txt,file2.txt和file3.txt放在一个文件夹中。想要将每个文件复制到自己的文件夹中,例如,file1.txt进入file1文件夹,file2.txt进入file2文件夹。文件夹已经创建。

2 个答案:

答案 0 :(得分:1)

假设文件全部在C:\ oldfolder中,新文件夹在C:\ newfolders \

CD "C:\OLDFOLDER"
for %%a in (*.*) Do copy "%%a" "C:\newfolder\%%~na\%%a"

for循环遍历oldfolder中的所有文件,然后将每个文件复制到

C:\Newfolder\{filenamewithoutextension}\{filename.ext}

%%〜na是一种只获取没有扩展名的文件名的方法。 “n”表示“name”,a表示for循环变量。可能有许多文件路径替换:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

可以组合修饰符以获得复合结果:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

答案 1 :(得分:0)

在Linux中,您可以输入

for i in *.txt; do mv $i `basename $i .txt`/; done

注意空间。