windows - 在 Windows CMD 中提取给定的祖先路径形成更深的路径
问题描述
我不知道如何使用 Win CMD 批处理脚本来获取此处描述的内容:
伪代码
set aPath=C:\just\a\long\path\to\a\file\in\the\file\system
set aDir=file
... some logic here
echo %result%
应该打印
C:\just\a\long\path\to\a\file
它应该在找到的第一次出现时停止,但最好也指定出现(可选)。
只欢迎 Windows CMD 解决方案,请不要使用 Powershell 代码或外部工具。我正在寻找一个纯 Windows CMD 解决方案。
解决方案
我不太确定你指的是什么
使用随机播放方法和自我修改代码来获取目录级别
:: Q:\Test\2019\08\29\SO_57717256.cmd
@Echo off & Setlocal EnableDelayedExpansion
set "aPath=C:\just\a\long\path\to\a\file\in\the\file\system"
set "aDir=file"
echo aPath = %aPath%
echo aDir = %aDir%
::... some logic here
set "intermediate=!apath:*%aDir%=!"
set "result=!aPath:%intermediate%=!"
:: and some vodoo there ;-)
Set i=0
Set "aPath=%aPath:\="&Set /a i+=1&Set "aPath[!i!]=%"
echo result= %result%
for /l %%L in (1,1,%i%) Do if "%aDir%"=="!aPath[%%L]!" (
echo aPath[%%L] = !aPath[%%L]! ^<^<^< matches aDir %aDir%
) else (
echo aPath[%%L] = !aPath[%%L]!
)
样本输出:
> Q:\Test\2019\08\29\SO_57717256.cmd
aPath = C:\just\a\long\path\to\a\file\in\the\file\system
aDir = file
result= C:\just\a\long\path\to\a\file
aPath[1] = just
aPath[2] = a
aPath[3] = long
aPath[4] = path
aPath[5] = to
aPath[6] = a
aPath[7] = file <<< matches aDir file
aPath[8] = in
aPath[9] = the
aPath[10] = file <<< matches aDir file
aPath[11] = system
推荐阅读
- r - 合并三个因子,使它们的因变量在 R 中求和
- javascript - 将两个数组与对象进行比较并显示删除了哪个项目
- c++ - 使用快速排序在 C++ 中排序可视化器错误
- node.js - 错误:发送 NodeJS Mongoose 后无法设置标头?
- node-red - node-red 中“更新”的正确语法
- ruby-on-rails - “或”语句在 Rails 查询中不起作用
- android - 我试图让 TextView 每 472 毫秒生成一个随机数,五次。我将如何使用循环来做到这一点?
- excel - countif 使用多个条件的唯一值
- ms-access - MS Access 中的自动编号可以随机更改吗?
- firebase - Firebase 身份验证与 Firestore