如何找到批处理文件的当前目录,然后将其用作路径? [英] How do I find the current directory of a batch file, and then use it for the path?

查看:35
本文介绍了如何找到批处理文件的当前目录,然后将其用作路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,我打算将其分发给我们的客户以运行软件任务.

I have a batch file that I intend to distribute to our customers to run a software task.

我们将它们作为文件夹或 .zip 与其中的文件一起分发.在里面,有批处理文件和另一个包含运行批处理所需文件的文件夹.

We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.

通常,当您进行批处理时,您会键入文件所在的路径.但我不知道文件在哪里.文件仍将保留在主文件夹中,但我需要让批处理找到该文件夹​​才能运行文件.

Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.

例如:如果他们在桌面上有主文件夹并运行它,则需要类似于 "C:UsersUsernameDesktop" 来运行.您可以将批处理 CD 放到该位置.

So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:UsersUsernameDesktop" to run. You would have the batch CD to that location.

但是如果他们从文档中运行它呢?我不知道用户名,所以我必须以某种方式让批处理找到这个.任何代码和/或说明都会很棒.

But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.

推荐答案

不需要知道文件在哪里,因为当你启动一个 bat 文件时,工作目录就是它启动的目录(主文件夹"),所以如果你有这个结构:

There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:

.mydocumentsfoldermybat.bat
.mydocumentsfoldersubfolderfile.txt

而用户启动mybat.bat",工作目录为.mydocumentsfolder",所以你只需要在你的脚本中写上子文件夹名称:

And the user starts the "mybat.bat", the working directory is ".mydocumentsfolder", so you only need to write the subfolder name in your script:

@Echo OFF
REM Do anything with ".SubfolderFile1.txt"
PUSHD ".Subfolder"
Type "File1.txt"
Pause&Exit

反正工作目录存放在%CD%"变量中,bat启动的目录存放在参数0中. 那么如果你想知道任何电脑上的工作目录你可以这样做:

Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:

@Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit

这篇关于如何找到批处理文件的当前目录,然后将其用作路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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