ディレクトリ "D:\ xx \ 1 \"に.batファイルを作成し、その絶対パス名を%〜dp0で取得します。 "D:\ xx \ 2 \" "D:\ xx \ 3 \" "D:\ xx \ 4 \"ディレクトリ内の他のファイルを、バッチスクリプトで相対パスで操作できますか?Windowsのバッチスクリプトで他のフォルダのファイルを操作する方法
1
A
答えて
1
call "%~dp0..\2\other.bat"
とcall "%~dp0..\3\other.bat"
を使用するというように、How to call a batch file that is one level up from the current directory?
を参照してくださいしかし、私たちはD:\xx\2\
でバッチファイルがあると仮定しましょう、D:\xx\3\
するD:\xx\4\
可能です... D:\xx\1\
のように正確と同じ名前で同じ内容。その後、バッチファイルは、次のコードで起動する必要があります:
@echo off
if "%CalledByBatch%" == "1" goto MainCode
rem Determine path to parent directory of the batch file by getting path
rem of the batch file with a backslash at end and removing this backslash.
set "ParentDirectory=%~dp0"
set "ParentDirectory=%ParentDirectory:~0,-1%"
rem The batch file is stored in root of a drive if the path to this batch
rem file ends now with a colon. In this case just execute the main code.
if "%ParentDirectory:~-1%" == ":" set "ParentDirectory=" & goto MainCode
rem Get path to parent directory of the batch file with a backslash at end.
for /F "delims=" %%I in ("%ParentDirectory%") do set "ParentDirectory=%%~dpI"
rem Define marker that this batch file is called by itself from same or
rem a different subdirectory of the parent directory of the batch file.
set "CalledByBatch=1"
rem For each non hidden subdirectory in parent directory of this batch file
rem check if the subdirectory contains also a batch file with same name as
rem this batch file. Make this directory the current directory if this
rem condition is true, call the batch file now executing main code and
rem restore initial current directory.
for /D %%I in ("%ParentDirectory%*") do if exist "%%I\%~nx0" (
pushd "%%I"
call "%~nx0"
popd
)
rem Delete the environment variable and exit processing of this batch file.
set "CalledByBatch="
goto :EOF
:MainCode
echo Running %~nx0 in "%CD%" ...
rem Here is the main code executed on each directory.
私はそれがすべてのサブディレクトリ内のすべてのファイルを処理しD:\xx\
内の1つのバッチファイルを持ってはるかに理にかなって、各サブディレクトリには、同じバッチファイルを持っていなければならない理由はわかりません。しかし、すでに使用されているバッチファイルのコードを知らずに、D:\xx\2\
のファイルを処理することは、コードを書き直す方法を示唆するものではありません。
D:\xx\
の各非隠しサブディレクトリのバッチファイルをD:\xx\1\
で呼び出す別の変形では、現在のサブディレクトリを現在のディレクトリにした後です。
@echo off
if not "%BatchFile%" == "" goto MainCode
rem Determine path to parent directory of the batch file by getting path
rem of the batch file with a backslash at end and removing this backslash.
set "ParentDirectory=%~dp0"
set "ParentDirectory=%ParentDirectory:~0,-1%"
rem The batch file is stored in root of a drive if the path to this batch
rem file ends now with a colon. In this case just execute the main code.
if "%ParentDirectory:~-1%" == ":" set "ParentDirectory=" & goto MainCode
rem Get path to parent directory of the batch file with a backslash at end.
for /F "delims=" %%I in ("%ParentDirectory%") do set "ParentDirectory=%%~dpI"
rem Define marker that this batch file is called by itself from same or
rem a different subdirectory of the parent directory of the batch file.
set "BatchFile=%~f0"
rem For each non hidden subdirectory in parent directory of this batch
rem file call exactly this batch file for executing the main code.
for /D %%I in ("%ParentDirectory%*") do if exist "%%I\%~nx0" (
pushd "%%I"
call "%BatchFile%"
popd
)
rem Delete the environment variable and exit processing of this batch file.
set "BatchFile="
goto :EOF
:MainCode
echo Running %~nx0 in %CD% ...
rem Here is the main code executed on each directory.
バッチファイルがドライブのルートディレクトリに格納されている場合、どちらのバッチファイルもメインコードを実行します。
使用されているコマンドとその動作方法を理解するには、コマンドプロンプトウィンドウを開き、次のコマンドを実行して、コマンドごとに表示されているすべてのヘルプページをすべてよく読んでください。
call /?
は...
%~dp0
説明 - バッチファイル自体と
%~nx0
ある引数0のドライブとパス - 名やバッチファイルの拡張子を。echo /?
for /?
goto /?
if /?
popd /?
pushd /?
rem /?
set /?
関連する問題
- 1. のMongoDBとバッチスクリプト操作
- 2. バッチスクリプトでファイルを開き、それを操作する
- 3. フォルダの操作
- 4. Windowsバッチスクリプト:2つのファイルの作成日を比較します
- 5. この操作方法Windowsフォームナビゲーション
- 6. SSISプロジェクトにファイルを追加する方法その他のフォルダ
- 7. MacシステムでWindowsベースのポップアップを操作する方法
- 8. 2レベルのフォルダから画像を操作する方法
- 9. 1つのフォルダから他のフォルダにファイルをコピーする方法#
- 10. Windows ACLのアクセス許可を操作する最善の方法
- 11. Windowsアプリケーションの他のフォルダにあるCrystal Reportパスを設定する方法
- 12. AntのWindowsバッチスクリプト
- 13. FTPフォルダからすべてのファイルをコピーするバッチスクリプト
- 14. .docファイルを操作する方法
- 15. Windowsの起動時にバッチスクリプトを実行する方法は?
- 16. JavaでWindowsファイルのアクセス許可を操作する
- 17. Windowsのファイルを操作するための最小のOS
- 18. Unixシェルスクリプトでファイル内の要素を操作する方法
- 19. vbaのopenイベントでxslmファイルを操作する方法
- 20. Google App Engineのデータストアでファイルを操作する方法
- 21. WP7でファイル操作を行う方法
- 22. .pharファイルの操作方法は?
- 23. ランダムな名前のファイルの名前を変更するWindowsバッチスクリプト?
- 24. Node.JSで静的ファイルのフォルダを作成する方法は?
- 25. Windowsで共有操作をトリガする方法8
- 26. JavaプログラムでWindowsにフォルダを作成する方法は?
- 27. SVN&reviewboardのWindowsバッチスクリプト
- 28. ファイルとその他の操作のコピーでのGcloud-rubyバックオフ
- 29. バッチスクリプトで既存のフォルダを取得
- 30. PythonのBoost INFOファイルを操作する最善の方法
'.. \ 2' http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135を参照してください。 – ACatInLove