2010-07-06 21:29:58 +0000 2010-07-06 21:29:58 +0000
68
68
Advertisement

DOSコマンドで現在のフォルダ名を取得する?

Advertisement

DOSコマンドでカレントフォルダ名(カレントディレクトリパスではなく)を取得することは可能でしょうか?可能であれば、どのようにして取得するのでしょうか?

for /f "delims=\" %%a in ("%CD%") do set CURR=%%a
echo.DIR: %CURR%

注意: 上記の試みは、私が文字列をトークン化して最後のトークンを CURR 変数として取得しようとしたものです。

Advertisement

回答 (9)

95
95
95
2010-07-06 22:27:07 +0000
for %I in (.) do echo %~nxI

または.batスクリプト内での最短の方法を見つけました。

for %%I in (.) do echo %%~nxI

または.batスクリプト内で変数に値を取得する。

for %%I in (.) do set CurrDirName=%%~nxI
echo %CurrDirName%

説明。 http://www.robvanderwoude.com/ntfor.php

nxはファイル名と拡張子のみを意味します。

31
31
31
2010-08-07 14:55:35 +0000

バッチファイルの現在の場所を知りたい場合は、(お使いのWindowsがそれほど古いリリースでない場合は)「DOSボックス」ウィンドウでfor /?と入力してください。下にスクロールします。読み込んでください。

%0 - as the name how this batchfile was called
%~d0 - as the drive letter where this batchfile is located ('\' in case of share)
%~p0 - as path (without the drive letter) where this batchfile is located
%~n0 - as filename (without suffix) of this batchfile
%~x0 - as filename's suffix (without filename) of this batchfile
%~a0 - as this batchfile's file attributes
%~t0 - as this batchfile's date+time
%~z0 - as this batchfile's filesize
%~dpnx0 - as this batchfile's fully qualified path+filename
[... and then some more ...]

0x1&

これは多くの場合に有効です。バッチファイルが mytest.bat と呼ばれているとします。これを様々な方法で呼び出すことができます。

  1. ..\..\to\mytest.bat …………………………. (相対パス)
  2. d:\path\to\mytest.bat ……………………… (フルパス)
  3. \fileserver\sharename\mytest.bat(リモート共有上のパス)

…と、変数の中で常に正しい値を得ることができます。

16
Advertisement
16
16
2011-11-17 09:20:12 +0000

個人的にはTomsの答えが好きだったのですが、dir名のドットに苦戦するまでは。

for /f "delims=\" %%a in ("%cd%") do echo topmost dir: %%~nxa
```0x1&というヒントがありました。
5
5
5
2013-08-06 13:11:55 +0000

Tomさんの回答は良いのですが、ディレクトリ名にピリオドが入っている場合(例えばwxwidgets-2.9.4)はフルネームしか出力されません。そのため、.4が拡張子として扱われているので、これはwxwidgets-2.9と出力されてしまいます(そう、ディレクトリ名なのに!)。

フル出力名を取得するには、最後に拡張子を追加する必要があります。

FOR %I IN (.) DO Echo %~nI%~xI

バッチファイルモードの場合。

FOR %%I IN (.) DO Echo %%~nI%%~xI

もちろん、バッチファイルに変数を設定しても構いません。

FOR %%I IN (.) DO SET CurrentD=%%~nI%%~xI
``` 0x1&
4
Advertisement
4
4
2012-03-15 10:19:31 +0000
set "MyPath=%~dpnx0" & call set "MyPath=%%MyPath:\%~nx0=%%" 
echo MyPath=%MyPath%

これはパス名

の". “とスペースで動作します。

  1. ファイル名全体(driveletter-path-filename-extension)をMyPath Var

  2. ファイル名と拡張子をMyPath var

  3. ファイル名と拡張子を`

set "MyPath=%~dpnx0" & call set "MyPath=%%MyPath:\%~nx0=%%" 
echo MyPath=%MyPath%

これはパス名

の”. “とスペースで動作します。

  1. ファイル名全体(driveletter-path-filename-extension)をMyPath Var

  2. ファイル名と拡張子をMyPath var

  3. ファイル名と拡張子を var

から削除します。パスの末尾にバックスラッシュが必要な場合は 2番目のセットコマンドでMyPathの後の0x6&を削除します、例:

set "MyPath=%%MyPath:%~nx0=%%"
4
4
4
2012-04-24 15:54:31 +0000

現在のdirを変数に取得することができます。ワンライナー:

set a=%cd%
echo %a%
```でチェック
2
Advertisement
2
2
2016-10-10 02:11:12 +0000

単純に

for %%d in ("%CD%") do echo %%~nxd

または

set "sPath=."
for %%d in ("%sPath%") do set "sDirName=%%~nxd"

パスの末尾のバックスラッシュに注意してください。

1
1
1
2011-12-07 18:59:40 +0000

このスレッド](https://stackoverflow.com/questions/280969/windows-batch-loop-over-folder-string-and-parse-out-last-folder-name)の私の答えは、3行で説明しています。

@echo off
SET "CDIR=%~dp0"
:: for loop requires removing trailing backslash from %~dp0 output
SET "CDIR=%CDIR:~0,-1%"
FOR %%i IN ("%CDIR%") DO SET "PARENTFOLDERNAME=%%~nxi"
ECHO Parent folder: %PARENTFOLDERNAME%
ECHO Full path: %~dp0
pause>nul
1
Advertisement
1
1
2015-05-28 16:08:10 +0000

これはバッチファイルから私のために動作します。これは現在の作業ディレクトリ名を返します。

pushd %1 & for %%i in (.) do @echo %%~ni
Advertisement