在批处理文件分隔符的最后一个实例后提取字符串 [英] Extracting string after last instance of delimiter in a Batch file

查看:1633
本文介绍了在批处理文件分隔符的最后一个实例后提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个由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:\Users\owner\Documents
C:\Users\owner\Videos
C:\Users\owner\Pictures\Trip

我想拆分它,使字符串变成了:

I would like to split it so that the strings become:

Documents
Videos
Trip

你们会如何建议我这样做呢?

How would you guys suggest I do this?

编辑:一个后续问题已经被问这里:<一href=\"http://stackoverflow.com/questions/20010117/for-loop-in-windows-batch-file-error-the-syntax-of-the-command-is-incorrect\">For循环在Windows批处理文件:错误:&QUOT;该命令的语法不正确&QUOT;

A follow-up question has been asked here: For loop in Windows batch file: Error: "The syntax of the command is incorrect"

推荐答案

指定一个参数后,参数变量,使用:

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屋!

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