【问题标题】:Want to copy multiple files of different names to same name (filename) folders.想要将多个不同名称的文件复制到同名(文件名)文件夹中。
【发布时间】:2014-05-15 23:54:24
【问题描述】:

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

【问题讨论】:

  • 先尝试做某事(一些代码尝试)......然后你会更容易从 SO 用户那里获得帮助......

标签: batch-file copy command xcopy


【解决方案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”表示“名称”,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

【讨论】:

    【解决方案2】:

    在 Linux 中你可以输入

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

    注意空格。

    【讨论】:

    • 我认为他在 Windows 上.. 因为他提到了 batch-file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 2019-04-22
    • 2018-09-06
    • 2020-06-18
    相关资源
    最近更新 更多