如何找到一个批处理文件的当前目录下,然后用它的路径? [英] How do I find the current directory of a batch file, and then use it for the path?
问题描述
我有我打算分发给我们的客户能够运行的软件编写一个批处理文件。
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:\\ Users \\用户名\\桌面
运行。你将不得不批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:\Users\Username\Desktop"
to run. You would have the batch CD to that location.
但是,如果他们从文档上运行哪些呢?我不知道用户名,所以我必须在某种程度上已批找到。任何code和/或指令将是巨大的。
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:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
和用户启动mybat.bat工作目录\\我的文档\\文件夹中。,所以你只需要在你的脚本写的子文件夹名称:
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
@Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
总之,工作目录保存在%CD%的变量,并在蝙蝠推出存储在参数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屋!