Windows批处理 - 遍历文件夹字符串,并解析出最后一个文件夹名称 [英] Windows batch - loop over folder string and parse out last folder name

查看:921
本文介绍了Windows批处理 - 遍历文件夹字符串,并解析出最后一个文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要抓住当前正在执行的批处理文件的文件夹名称。我一直在使用下面的语法(这是错误的present)试图遍历当前目录:

I need to grab the folder name of a currently executing batch file. I have been trying to loop over the current directory using the following syntax (which is wrong at present):

set mydir = %~p0
for /F "delims=\" %i IN (%mydir%) DO @echo %i

在我似乎无法在作为搜索字符串传递MYDIR变量值问题的夫妇。它似乎只如果我通过在命令工作;我有语法错误,不能工作的原因。

Couple of issues in that I cannot seem to pass the 'mydir' variable value in as the search string. It only seems to work if I pass in commands; I have the syntax wrong and cannot work out why.

我的想法是遍历文件夹字符串以'\\'分隔符,但是这是导致问题了。如果我设置在每个循环变量,然后最后一个值集将当前文件夹的名称。例如,假定有以下路径:

My thinking was to loop over the folder string with a '\' delimiter but this is causing problems too. If I set a variable on each loop then the last value set will be the current folder name. For example, given the following path:

C:\\ Folder1中\\ FOLDER2 \\ Folder3 \\ Archive.bat

C:\Folder1\Folder2\Folder3\Archive.bat

我希望解析出价值'Folder3'。

I would expect to parse out the value 'Folder3'.

我需要解析该值作为出它的名字将是另一个文件夹,我要创建批处理文件进一步下跌的一部分。

I need to parse that value out as its name will be part of another folder I am going to create further down in the batch file.

非常感谢,如果有人可以提供帮助。我可能会找错了树完全,因此任何其他方法将大大收到也。

Many thanks if anyone can help. I may be barking up the wrong tree completely so any other approaches would be greatly received also.

推荐答案

您为pretty接近它:)这应该工作:

You were pretty close to it :) This should work:

@echo OFF
set mydir="%~p0"
SET mydir=%mydir:\=;%

for /F "tokens=* delims=;" %%i IN (%mydir%) DO call :LAST_FOLDER %%i
goto :EOF

:LAST_FOLDER
if "%1"=="" (
    @echo %LAST%
    goto :EOF
)

set LAST=%1
SHIFT

goto :LAST_FOLDER

由于某种原因,为命令不喜欢'\\'作为分隔符,所以我转换所有'\\'到';'第一个( SET MYDIR =%MYDIR:\\ =;%

这篇关于Windows批处理 - 遍历文件夹字符串,并解析出最后一个文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆