如何将命令的输出重定向到在环境变量中定义了名称的文件? [英] How to redirect output of a command to a file of which name is defined in an environment variable?

查看:169
本文介绍了如何将命令的输出重定向到在环境变量中定义了名称的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个文件夹下存在的所有文件的绝对路径存储到一个文本文件中,例如temp.txt.

I want to store absolute path of all the files present under a folder into a text file, let's say temp.txt.

我正在使用以下命令执行此任务:

I am using this command for this task:

dir /s/b/a-d > D:\my work\temp.txt

当我重定向到硬编码到批处理文件中的文件名时,上述命令可以正常工作.

The above command is working fine when I am redirecting to a file name written hard coded into the batch file.

但是当我使用下面的代码所示的变量时,我没有将输出重定向到文件temp.txt.

But when I am using a variable as shown in below code, I am not getting output redirected to the file temp.txt.

@echo off
setlocal enabledelayedexpansion
REM I am running this script from D:\my work\
set origDir= %cd%
cd C:\
set temporaryFile="!origDir!\temp.txt"
dir /s/b/a-d > !temporaryFile!

为什么 DIR 的输出未写入D:\my work\temp.txt?

推荐答案

为什么没有字符串输出,且带有'echo%var 在命令行上使用'set var = text'后是否%%'?在命令行上出现了什么问题:

The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? what is wrong on command line:

set origDir= %cd%

命令 SET 不会忽略等号后的空格.因此,环境变量origDir的字符串值用前导空格字符定义.

The space after the equal sign is not ignored by command SET. The string value of the environment variable origDir is therefore defined with a leading space character.

接下来是命令行

set temporaryFile="!origDir!\temp.txt"

在您的情况下会导致字符串" D:\my work\temp.txt",并且双引号将分配给环境变量temporaryFile.

results in your case in string " D:\my work\temp.txt" with the double quotes being assigned to environment variable temporaryFile.

因此,由于文件名以空格开头,因此用双引号引起来的文件名无效.

So the file name with path enclosed in double quotes is invalid because of beginning with a space character.

您的任务的可能代码是:

A possible code for your task is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "origDir=%CD%"

rem Current directory path usually does not end with a backslash. But on
rem current directory being root of a drive it ends with a backslash which
rem should be removed to correct concatenate it with backslash and file name.

if "%origDir:~-1%" == "\" set "origDir=%origDir:~0,-1%"
set "temporaryFile=%origDir%\temp.txt"
cd /D C:\
dir /A-D /B /S >"%temporaryFile%"
endlocal

这里不需要使用延迟的环境变量扩展.

The usage of delayed environment variable expansion is not needed here.

仅当还使用参数/D并且指定目录不在UNC引用的网络共享上时,命令 CD 会将当前目录更改为与当前驱动器不同的驱动器上的指定目录路径.

The command CD changes the current directory to specified directory on a different drive than current drive only if parameter /D is also used and the specified directory is not on a network share being referenced with UNC path.

具有启用的命令扩展名的命令 PUSHD 也适用于UNC路径,而使用 POPD 可以恢复初始当前目录.

The command PUSHD with enabled command extensions works also for UNC paths and with POPD the initial current directory can be restored.

好吧,命令 ENDLOCAL 会在批处理文件中还原初始当前目录上方的文件.这可以在以D:\my work为当前目录的命令提示符窗口中运行此批处理文件时看到.运行批处理文件后执行的echo %CD%输出D:\my work,尽管批处理文件已更改为驱动器C:的根目录.阅读此答案,以获取有关命令 SETLOCAL ENDLOCAL 的详细信息.

Well, the command ENDLOCAL restores in batch file above also the initial current directory. This can be seen on running this batch file from within a command prompt window with D:\my work being current directory. echo %CD% executed after running the batch file outputs D:\my work although the batch file changes to root directory of drive C:. Read this answer for details about the commands SETLOCAL and ENDLOCAL.

也可以,但不建议使用:

Also possible but not recommended:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "origDir=%CD%"
if "%origDir:~-1%" == "\" set "origDir=%origDir:~0,-1%"
set "temporaryFile="%origDir%\temp.txt""
cd /D C:\
dir /A-D /B /S >%temporaryFile%
endlocal

环境变量temporaryFile保留具有全路径的文件名,该文件名已经用双引号引起来,因此 DIR 命令行在Windows命令解释器进行如下预处理后执行:

The environment variable temporaryFile holds the file name with full path already enclosed in double quotes and therefore the DIR command line is executed after preprocessing by Windows command interpreter as:

dir /A-D /B /S  1>"D:\my work\temp.txt"

通常不建议将已经用双引号括起来的文件夹或文件名分配给环境变量,因为如果稍后在批处理代码中将变量值与其他字符串连接在一起,这很容易导致意外的行为,请参见例如如何使用空格设置环境变量?

In general it is not advisable to assign a folder or file name already enclosed in double quotes to an environment variable because this can easily result in unexpected behavior if the variable value is concatenated later in batch code with other strings, see for example How to set environment variables with spaces?

但是此任务的最短版本是单个命令行:

But the shortest version for this task is the single command line:

@dir C:\* /A-D /B /S >temp.txt

单个命令行与上面所有这些命令的两个批处理文件完全相同.它在当前目录D:\my work中创建一个文件temp.txt,该文件具有整个驱动器C:上所有文件的名称,该文件的完整路径具有当前用户帐户有权列出目录项的完整路径.

That single command line does exactly the same as the two batch files with all those commands above. It creates in current directory D:\my work a file temp.txt with the names of all files on entire drive C: with full path for which the current user account has permissions for listing directory entries.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • dir /?
  • echo /?
  • endlocal /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • cd /?
  • dir /?
  • echo /?
  • endlocal /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

还请阅读有关使用命令重定向操作符的Microsoft文章.

Read also the Microsoft article about Using Command Redirection Operators.

这篇关于如何将命令的输出重定向到在环境变量中定义了名称的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆