Help us understand the problem. What is going on with this article?

[バッチ] バッチのあるフォルダのパスを取得する

もくじ
https://qiita.com/tera1707/items/4fda73d86eded283ec4f

やりたいこと

バッチの中で、バッチ自身が置かれているフォルダのパスを取得したい。

やり方

%~dp0を使う。

サンプル

sample.bat
@echo off
set SELFPATH=%~dp0
echo %SELFPATH% > target.txt
pause

今バッチがあるフォルダパスが出る。

target.txt
C:\Users\masa\Desktop\test\ 

備考

@echo off
通常はバッチに書いた1行1行の処理がコマンドプロンプトに表示されるが、これを書くと、そこから先は処理が画面にでなくなる。ただし、自分でechoを書いたり、何かのコマンドが出力するものは画面にでる。

set SELFPATH=%~dp0
「SELFPATH」という変数を作って、そこに自分のパスを入れている。
変数を使う際は、その変数名を%と%で挟む。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away