Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在批处理文件中使用FOR/F时创建子文件夹_Windows_Batch File_For Loop_Cmd - Fatal编程技术网

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

Windows 如何在批处理文件中使用FOR/F时创建子文件夹,windows,batch-file,for-loop,cmd,Windows,Batch File,For Loop,Cmd,我想处理一个文件夹和子文件夹中的文件,处理后想将文件移动到新位置(文件夹) 当前命令正在工作,但它没有创建子文件夹并在同一文件夹中生成所有新文件(即输出文件夹) 我需要它保存在相同的文件夹结构,作为其源文件夹 其中filelist.txt(源文件夹)为: 我希望我的脚本生成输出(目标文件夹),如下所示: 当前,上面的For/F命令正在生成如下输出: c:\backup\output\browse.asp c:\backup\output\capital.asp c:\backup\output\

我想处理一个文件夹和子文件夹中的文件,处理后想将文件移动到新位置(文件夹)

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

我需要它保存在相同的文件夹结构,作为其源文件夹

其中filelist.txt(源文件夹)为:

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

当前,上面的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**

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

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)
这应该适合你

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

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

您的问题是什么?在你的问题lol中甚至没有问号。基本上我想处理现有文件,比如在每个文件中添加一些文本并将其移动到某个新位置,子文件夹中的文件移动到新位置子文件夹,当前文件移动到输出文件夹(使用上面的FOR/f命令),但没有目录结构。我必须手动查找并重新创建子文件夹,如何使filelist.txt像这样?目前我正在使用:
dir%cd%\*.asp/A:-D/B/O:N/S>>%cd%\filelist.txt
首先感谢您的帮助,您的命令正在运行,但它只是省略了驱动器号并创建了
output\backup\oldwork\files
就像@Monacraft所说的那样,我在创建文件列表时必须从filelist.txt中删除完整路径,但不知道怎么做????目前我使用的是
dir%cd%\*.asp/A:-D/B/O:N/S>>%cd%\filelist.txt
如果您想要与源文本文件中的驱动器相同,只需在
输出%%~p
前面加上
%~di
即可:
“%~di”“输出%%~p
-您仍然没有明确说明所需的输出格式。如果该解决方案不起作用,请编辑您的原始帖子,以显示实际的原始文件名和结果文件名。我相信@Monacraft解决方案没有考虑到源文件必须包含源目录名,因为它被用作
process.exe的参数,源和目标文件名以及子文件夹的目录名将是相同的,它只是处理并将源文件副本放入新文件夹“output”我更新了我的问题,另外,当我尝试在(filelist.txt)中对/F%%I执行(md“%%~di”“output%%~p”和process.exe%%I>“output%%~pnxi”)时,
它仍然不起作用,并且出现错误
子目录或文件c:output%%p已经存在。
browse.asp
capital.asp
make.asp
conf\config.asp
conf\global.asp
for /F %%i in (filelist.txt) do (process.exe c:\backup\oldwork\%%i > output\%%i)
for /F %%i in (filelist.txt) do (md "output%%~pi"&process.exe %%i > "output%%~pnxi")