如何在批处理文件中使用FOR / F时创建子文件夹

时间:2014-11-02 01:09:24

标签: windows batch-file for-loop cmd

我想处理子文件夹中的文件夹和文件,并且在处理之后想要将文件移动到新位置(文件夹)。

当前命令正在运行但它没有创建子文件夹并在同一文件夹中生成所有新文件(即输出文件夹)

for /F %%i in (filelist.txt) do (process.exe %%i > output\%%~nxi)

我需要它将它保存在与其源文件夹相同的文件夹结构中。

filelist.txt(源文件夹)是:

c:\backup\oldwork\browse.asp
c:\backup\oldwork\capital.asp
c:\backup\oldwork\make.asp
c:\backup\oldwork\conf\config.asp
c:\backup\oldwork\conf\global.asp

我希望我的脚本生成输出(目标文件夹),如:

c:\backup\output\browse.asp
c:\backup\output\capital.asp
c:\backup\output\make.asp
c:\backup\output\conf\config.asp
c:\backup\output\conf\global.asp

目前,For / F命令正在生成如下输出:

c:\backup\output\browse.asp
c:\backup\output\capital.asp
c:\backup\output\make.asp
**c:\backup\output\config.asp** (not following directory structure)
**c:\backup\output\global.asp**

2 个答案:

答案 0 :(得分:0)

您的代码不起作用的原因与输出内容有关:

output\%%~nxi

如果您查看文档,您会看到:

You can now use the following optional syntax:

    %~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

The modifiers can be combined to get compound results:

    %~nxI       - expands %I to a file name and extension only

由于您只是扩展名称和扩展名,因此忽略文件夹结构。

解决方案:

在您的情况下我会做的是,将您的filelist.txt更改为以下格式:

browse.asp
capital.asp
make.asp
conf\config.asp
conf\global.asp

然后改变你的for循环:

for /F %%i in (filelist.txt) do (process.exe c:\backup\oldwork\%%i > output\%%i)

哪个应该适合你。

答案 1 :(得分:0)

for /F %%i in (filelist.txt) do (md "output%%~pi"&process.exe %%i > "output%%~pnxi")

应该完成任务,创建output\backup\oldwork\browse.asp等(由于缺乏对所需结果的充分描述)