在批处理文件中最后一个分隔符实例之后提取字符串 [英] Extracting string after last instance of delimiter in a Batch file
问题描述
我正在编写一个由 Java 程序调用的 Windows 批处理文件.许多表示文件目录的不同字符串作为参数传递到批处理文件中.我想弄清楚如何在"的最后一个实例之后提取一个字符串.比如我有三个目录:
I'm working on writing a Windows batch file that is called by a Java program. A number of different strings denoting file directories are passed into the batch file as parameters. I am trying to figure out how to extract a string after the last instance of the "". For example, I have three directories:
C:UsersownerDocuments
C:UsersownerVideos
C:UsersownerPicturesTrip
我想拆分它,使字符串变成:
I would like to split it so that the strings become:
Documents
Videos
Trip
你们如何建议我这样做?
How would you guys suggest I do this?
这里提出了一个后续问题:Windows 批处理文件中的 For 循环:错误:命令的语法不正确"
A follow-up question has been asked here: For loop in Windows batch file: Error: "The syntax of the command is incorrect"
推荐答案
给param"变量赋值一个参数后,使用:
After assigned one parameter to "param" variable, use:
for %%a in (%param:= %) do set lastDir=%%a
只要最后一个目录没有空格,这个方法就有效.如果需要,可以修复此细节.所有参数的处理是这样的:
This method works as long as the last directory does NOT have spaces. This detail may be fixed, if needed. The processing of all parameters would be like this:
:nextParam
set "param=%~1"
if not defined param goto endParams
for %%a in (%param:= %) do set lastDir=%%a
echo Last dir: %lastDir%
shift
goto nextParam
:endParams
或者,以更简单的方式(没有空格限制):
Or, in a simpler way (with no spaces restrictions):
:nextParam
if "%~1" equ "" goto endParams
echo Last dir: %~N1
shift
goto nextParam
:endParams
这篇关于在批处理文件中最后一个分隔符实例之后提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!