Batch file 将CD的标准输出放入循环中的文件

Batch file 将CD的标准输出放入循环中的文件,batch-file,Batch File,使用我在这里找到的一些组件,我构建了一个批处理文件,从批处理文件运行的目录开始循环遍历目录树 批处理文件按预期工作,但我需要将cmd.exe命令CD的输出捕获到我在运行之前创建的文件中 问题是,如果我试图将标准输出重定向到.txt文件中,我只会看到第一个找到的目录 我发现了一些使用PowerShell从命令提示符屏幕中提取列表的代码,但对我来说这是不雅观的(尽管它似乎可以工作) 我已经阅读了setlocalenabledelayedexpansion上的材料,但它似乎高于我的工资等级,因为我无法

使用我在这里找到的一些组件,我构建了一个批处理文件,从批处理文件运行的目录开始循环遍历目录树

批处理文件按预期工作,但我需要将cmd.exe命令
CD
的输出捕获到我在运行之前创建的文件中

问题是,如果我试图将标准输出重定向到.txt文件中,我只会看到第一个找到的目录

我发现了一些使用PowerShell从命令提示符屏幕中提取列表的代码,但对我来说这是不雅观的(尽管它似乎可以工作)

我已经阅读了
setlocalenabledelayedexpansion
上的材料,但它似乎高于我的工资等级,因为我无法使它工作

下面是工作代码,带有一个
Rem
ark,我认为导出到.txt文件应该在这里

我们将不胜感激

Rem  Recursively Traverse a Directory Tree

Rem  Notes:
Rem  "For /r" command can be used to recursively visit all the directories in
Rem  a directory tree and perform a command in each subdirectory.
Rem  In this case, save the output to a text file

Rem  for /r = Loop through files (Recurse subfolders).
Rem  pushd  = Change the current directory/folder and store the previous folder/path for
Rem           use by the POPD command.
Rem  popd   = Change directory back to the path/folder most recently stored by the PUSHD
Rem           command.

@echo off
CLS
echo.
echo.
Rem  FirstJob - Generate a date and save in the work file. 

Rem Grab the date/time elements and stuff them into a couple of variables
set D=%date%
set T=%time%
set DATETIME=%D% at %T%
Rem  OK. We now have the date and time stuffed into the variable DATETIME
Rem  so now stick it into our work file along with a heading.
 Echo List of Found Directories > DirList.txt
 Echo %DATETIME% >> DirList.txt
 echo. >> DirList.txt
 echo. >> Dirlist.txt

Rem  SecondJob - Do the looping stuff and save found directories to file.

Rem  Start at the top of the tree to visit and loop though each directory
for /r %%a in (.) do (
Rem  enter the directory
 pushd %%a
 CD

Rem ------------------  direct Standard Output to the file DirList.txt -----------------

Rem  exit the directory
 popd
)

: END
Rem  All finished
Echo Done!
exit /b
在:结束标记之前添加到上述纸条的额外代码行。产生所需产出的是:

powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^a')
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^c')
powershell Get-Clipboard>>DirList.txt

问题在于您的
pushd
命令,因为您更改了当前目录,所以文件
dirlist.txt
必须使用绝对路径,否则您将在每个
pushd
目录中创建它。 我在这里使用了
%~dp0
,它是批处理文件本身的路径

你可以试试

cd >> %~dp0\dirlist.txt
或者只是

echo %%a >> %~dp0\dirlist.txt
或者你可以使用一个完整的块重定向

( 
  for /r %%a in (.) do (
    pushd %%a
    echo %%a
    popd
  )
) > dirlist.txt

您是说/R中的
CD
命令吗?您已经在执行
PUSHD
,因此使用
CD
命令没有意义。
FOR
变量现在是您当前的工作目录?而且@Squashman使用CD的原因是绝对正确的。CD的使用是它被包括在内的原因,也是附加代码起作用的原因。如果没有输出到命令屏幕,则不会收集数据。我更喜欢一个更优雅的解决方案,而不是一个编码的抓取和抓取,但是我还没有成功,因为我还没有完全掌握循环中标准输出的神秘诡计。@JRinOz在
cd
echo%%a
的输出中没有区别,因为你改成了diretory
%%a
。但是绝对路径是必需的谢谢jeb我会试试的。我的一些评论在回答和评论的时间框架内是不符合顺序的,可能是由于我国政府推出的Slooow国家宽带网络。所以我很抱歉给你带来困惑。谢谢你,杰布。工作很好。问题解决了。