简体   繁体   中英

How to operate files in other folders by Windows batch script

I create a .bat file in directory "D:\\xx\\1\\" and get its absolute pathname by %~dp0. Can I operate some other files in directories "D:\\xx\\2\\""D:\\xx\\3\\""D:\\xx\\4\\"... by relative path with my batch script?

It is possible to use call "%~dp0..\\2\\other.bat" and call "%~dp0..\\3\\other.bat" and so on, see How to call a batch file that is one level up from the current directory?

But let us assume there is a batch file in D:\\xx\\2\\ , D:\\xx\\3\\ , D:\\xx\\4\\ , ... with same name as in D:\\xx\\1\\ and with exactly the same content. Then the batch file should start with following code:

@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.

I have no idea why each subdirectory should have same batch file as it would make much more sense to have one batch file in D:\\xx\\ which processes all files in all subdirectories. But without knowing the code of already used batch file to process the files in D:\\xx\\2\\ it is not really possible suggest how to rewrite the code.

Another variant which calls the batch file in D:\\xx\\1\\ on each non hidden subdirectory in D:\\xx\\ after making the current subdirectory the current directory.

@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.

Both batch files just run main code if the batch file is stored in root directory of a drive.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains
    %~dp0 – drive and path of argument 0 which is the batch file itself and
    %~nx0 – name and extension of the batch file.
  • echo /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • set /?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM