2017-12-29 24 views
1

ディレクトリ "D:\ xx \ 1 \"に.batファイルを作成し、その絶対パス名を%〜dp0で取得します。 "D:\ xx \ 2 \" "D:\ xx \ 3 \" "D:\ xx \ 4 \"ディレクトリ内の他のファイルを、バッチスクリプトで相対パスで操作できますか?Windowsのバッチスクリプトで他のフォルダのファイルを操作する方法

+1

'.. \ 2' http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135を参照してください。 – ACatInLove

答えて

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 /?
関連する問題