简体   繁体   中英

Find absolute path to a directory if given path to start search from (in a batch file)

A user will give me an absolute path - say "C:\\src" (passed into %1 in the batch file). Then, I need to find the directory entitled "SQLScripts" that is in some subfolder of "C:\\src".

How can I find the absolute path to the "SQLScripts" directory? Also, I'm not worrying about multiple instances of the SQLScripts directory existing.

From my Googling, the solution may involve a for loop and some batch modifier such as %~$PATH:1. This link may be beneficial - Special Batch Characters .

All solutions need to work on Windows XP and above.
Note that I'm constrained to using a batch file, and can't use other "easier" methods such as a simple Python code snippet to solve my problem.

Thanks!

This code saves the path to SQLScripts into %SQLSCRIPTSPATH% variable, and it works on WinXP:

DIR SQLScripts /B /P /S>tmp.txt
for /f "delims=" %%a in (tmp.txt) do set SQLSCRIPTSPATH=%%a
del tmp.txt

EDIT:

Better solution (without using a temporary file) suggested by Joe :

for /f "tokens=*" %%i in ('DIR SqlScripts /B /P /S') do SET SQLSCRIPTSPATH=%%i

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