批处理:如何从当前目录运行树命令? [英] Batch: How to run tree command from current directory?

查看:126
本文介绍了批处理:如何从当前目录运行树命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会将当前目录与工作目录混淆,但是无论我试图从当前所在的文件夹中创建一个运行tree命令的批处理文件.

I may be confusing current directory with working directory but regardless I am trying to make a batch file that runs the tree command from the folder it's currently in.

我有一个名为"Network_Switch_Backup"的文件夹,其中包含一个脚本,一些其他项目以及一个名为"backups"的子文件夹.

I have a folder called "Network_Switch_Backup" with a script, some other items and a subfolder called "backups".

这已用于测试目的:

tree U:\Desktop\Network_Switch_Backup\backups /f

但是,由于我将其压缩并发送到其他计算机和用户,显然这是不切实际的,因为他们可以将文件夹放置在桌面以外的任何位置.

But as I will be zipping it and sending it to different computers and users clearly this isn't practical since they could put the folder anywhere besides the desktop.

我查看了其他线程,并尝试了其他方法,这看起来是最有前途的(但仍然没有用):

I've looked at other threads and amongst other things I tried, this looked the most promising (but still did not work):

tree %cd%\backups /f

但是从cmd运行时tree %cd%\downloads /f可以很好地工作,所以我有点困惑.

However tree %cd%\downloads /f works perfectly fine when running from cmd so I'm just a bit confused.

推荐答案

建议在批处理文件中引用可执行文件以全限定文件名运行,这意味着全路径+文件名+文件扩展名,尤其是在以下位置:可执行文件是众所周知的.这使批处理文件独立于环境变量PATHEXTPATH的值. PATH在许多运行Windows的计算机上常常定义不正确.

It is advisable in batch files to reference executables to run with full qualified file name which means full path + file name + file extension, especially if the storage location of the executable is well known. That makes the batch file independent on the values of the environment variables PATHEXT and PATH. PATH is quite too often not correct defined on many computers running Windows.

的完全限定名称为%SystemRoot%\System32\tree.com.

环境变量SystemRoot没有定义为系统 user 环境变量,例如PATHPATHEXT,但是在执行批处理文件时定义了.因此,使用此 Windows环境变量是非常安全的.

Environment variable SystemRoot is not defined as system or user environment variable like PATH and PATHEXT, but is nevertheless defined on execution of a batch file. So it is very safe to use this Windows environment variable.

"..."未被识别为内部或外部命令,可操作程序或批处理文件的原因是什么?非常详细地解释了Windows命令处理器如何在命令提示符下或批处理文件中查找未使用完整限定文件名指定的可执行文件和脚本.

What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? explains very detailed how Windows command processor finds executables and scripts not specified with full qualified file name on command prompt or in a batch file.

在编写应用程序或脚本时,需要考虑两个目录:

There are two directories which need to be taken into account on coding an application or script:

  1. application/script目录是包含程序或脚本的目录.
  2. 当前目录或工作目录是执行程序或脚本的目录.

例如,批处理文件存储在目录"%UserProfile%\Desktop"中.只需双击用户桌面上的批处理文件,Windows就会将批处理文件的目录设置为当前目录.因此,脚本目录是执行批处理文件时的当前目录.但是,如果通过右键单击该批处理文件并右键单击上下文菜单选项以管理员身份运行来执行此批处理文件,则通常根据目录%SystemRoot%\System32从目录%SystemRoot%\System32执行存储在"%UserProfile%\Desktop"中的批处理文件.用户帐户权限以及对当前用户的用户帐户控制设置.在为什么以管理员身份运行"为什么会更改,详细说明了在执行批处理文件之前将%SystemRoot%\System32设置为当前目录的原因. (有时)批处理文件的当前目录?

For example a batch file is stored in directory "%UserProfile%\Desktop". Windows sets the directory of the batch file as current directory on simply double clicking the batch file on user's desktop. Therefore the script directory is the current directory on execution of the batch file. But if this batch file is executed by right clicking on the batch file and left clicking on context menu option Run as administrator, the batch file stored in "%UserProfile%\Desktop" is usually executed from directory %SystemRoot%\System32 depending on user account permissions and on user account control setting of current user. The reason for making %SystemRoot%\System32 the current directory before executing the batch file is explained in detail by answer on Why does 'Run as administrator' changes (sometimes) batch file's current directory?

MSDN文章命名文件,路径和命名空间详细说明了如何引用相对于当前目录的文件和文件夹.当前目录根本不包含在文件/文件夹参数字符串中,或​​者由.\表示.

The MSDN article Naming Files, Paths, and Namespaces explains in detail how to reference files and folders relative to current directory. The current directory is simply not included in file/folder argument string or alternatively represented by .\.

如果需要知道当前目录的驱动器和路径,例如在运行批处理文件时将其输出,则存在动态环境变量CD(Current Directory的缩写).启用延迟扩展%CD%!CD!会扩展到当前目录的完整路径除当前目录是驱动器的根目录外,不以反斜杠结尾.在命令提示符窗口set /?中运行时输出的帮助输出在上一个帮助页面上简要解释了动态环境变量CD.

In case of drive and path of current directory needs to be known, for example to output it on running a batch file, there is the dynamic environment variable CD (short for Current Directory). %CD% or !CD! with delayed expansion enabled expands to full path of current directory which does not end with a backslash, except the current directory is the root directory of a drive. The help output on running in a command prompt window set /? explains dynamic environment variable CD briefly on last help page.

批处理文件通常需要设计为引用具有相对于批处理文件目录的路径的文件或文件夹.在这种情况下,建议不要使用当前目录,因为当前目录实际上可以是任何目录.

Batch files need to be designed very often to reference files or folders with a path relative to directory of the batch file. In this case it is not advisable to use the current directory because the current directory can be really any directory.

在命令提示符窗口中运行call /?时的帮助输出说明了如何从批处理文件中引用批处理文件的参数.参数0始终是批处理文件本身.

The help output on running call /? in a command prompt window explains how arguments of a batch file can be referenced from within a batch file. Argument 0 is always the batch file itself.

%~dp0引用批处理文件的驱动器和路径.该文件路径始终以反斜杠结尾,但是当然可以包含空格或这些字符之一&()[]{}^=;!'+,`~,这些字符要求将整个文件/文件夹参数字符串括在双引号中.因此,%~dp0必须串联在批处理文件中,并且在该字符串之后没有其他反斜杠,并且必须将整个参数字符串括在双引号中,以确保安全工作.

%~dp0 references drive and path of the batch file. This file path ends always with a backslash, but of course can contain a space or one of these characters &()[]{}^=;!'+,`~ which require entire file/folder argument string to be enclosed in double quotes. So %~dp0 must be concatenated in a batch file without an additional backslash after that string and the entire argument string must be enclosed in double quotes to work safely.

因此,用于引用批处理文件目录中的子目录Backups的命令行与当前目录所在的目录无关,即:

So the command line to use to reference the subdirectory Backups in directory of the batch file independent on which directory is the current directory is:

%SystemRoot%\System32\tree.com "%~dp0Backups" /F

cmd.exe的错误在中详细解释了,用%〜dp0引用的批处理文件路径有时会在更改目录时更改的原因是什么?在批处理文件中使用%~dp0时应考虑?.

A bug of cmd.exe explained in detail on What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory? should be taken into account on using %~dp0 in a batch file.

这篇关于批处理:如何从当前目录运行树命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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