【问题标题】:Using a for loop in a batch file to apply a command to just the first level down of subdirectories在批处理文件中使用 for 循环将命令应用于子目录的第一级
【发布时间】:2012-09-20 20:34:05
【问题描述】:

我有一个包含大量子目录的目录,我想对它们中的每一个应用一个命令(一个在其中创建更多子目录的命令)我的问题是我将如何制定一个 FOR 循环以便它可以将该命令应用于每个目录,而不将其进一步应用到子目录的子目录中。

我对此很陌生,我已经尝试了很多不同的组合,我要做的就是在当前工作目录而不是子目录上运行命令。

【问题讨论】:

  • 继续你的问题,展示一个不起作用的例子。还显示您预期的结果与您得到的结果。一旦您的要求明确,这听起来是一个非常简单的问题。

标签: for-loop batch-file dos


【解决方案1】:

我不清楚您是要处理子文件夹还是孙子文件夹。无论哪种方式,您都将要使用 FOR /D 。

处理子文件夹:

@echo off
setlocal

::The following sets the root to the current directory.
::But root could be set to any path you choose
set "root=."

pushd "%root%"
for /d %%F in (*) do (
  rem %%F now contains a child folder.
  rem You could make a subdirectory easily enough
  md "%%F\newFolder"
)

处理孙文件夹:

@echo off
setlocal

::The following sets the root to the current directory.
::But root could be set to any path you choose
set "root=."

pushd "%root%"
for /d %%A in (*) do (
  rem %%A now contains a child folder.
  pushd "%%A"
  for /d %%B in (*) do (
    rem %%B now contains a grandchild folder
    rem You can easily create a new folder
    md "%%B\newFolder"
  )
  popd
)

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多