拆分自己的文件名,并在批处理脚本分配特定部分变量 [英] Split own filename and assign specific parts to variables in a batch script

查看:143
本文介绍了拆分自己的文件名,并在批处理脚本分配特定部分变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您的帮助提供的。

我从哪个名字我想使用的部分,并为其指定为变量的批处理文件。 /˚F与delims使用了我能够做这样的事,但我必须将文件重命名为不同的方案比我需要什么。
硒这里是我有:

- 当前的文件名: WHID-PRT-MFP-3,华北东部OFFICE.bat

  SET的getName =%〜N0FOR / F标记= 1 delims =  - %% G IN(%的getName%)DO(SET SITEID = %% G)FOR / F标记= 1 delims =%% E在(%的getName%)DO(SET NAME = %% E)SET的PrinterName =%名称%SET服务器名称=服务器 - %SITEID%-01

好了,这似乎工作确定,即时通讯能够得到我从文件名需要的部分,并将它们分配给变量,这样%名称%= WHID-PRT-MFP-3 %SITEID%= WHID

现在我的两难困境是,我需要的文件名是:北东办事处,WHID-PRT-MFP-3.bat
所以我一直试图让逗号后面的整条生产线( WHID-PRT-MFP-3 )给一个变量和破折号(<$ C $中的第一项C> WHID
),另一个变量,同上面的例子,但是不能看着办吧。这几个名称中使用这样不仅如我可以有两部分或长或短的名字的例子,说: WHID-PRT-1,OFFICE 1

任何意见都欢迎。

感谢


解决方案

  

北东办事处,WHID-PRT-MFP-3.bat:


  SET的getName =%〜N0REM分裂以逗号
FOR / F令牌= 2 delims =%% E在(%的getName%)DO(    逗号后REM微调空间
    FOR / F令牌= *%% S IN(%% E)DO SET NAME = %%小号    REM连字符之前拿到字和剥离前导空格
    FOR / F标记= 1 delims = - %%的S(%% E)并设置SITEID = %%小号
)SET的PrinterName =%名称%
SET服务器名称=服务器 - %SITEID%-01

thanks in advance for any help offered.

I have a batch file from which name i want to use parts and assign them as variables. Using the for /f with delims i was able to do something like it but i have to rename the file to a different scheme than what i need. Se here is what i have:

-Current filename: WHID-PRT-MFP-3, NORTH EAST OFFICE.bat

SET getname=%~n0

for /f "tokens=1 delims=-" %%G IN ("%getname%") DO (SET siteid=%%G)

for /f "tokens=1 delims=," %%E IN ("%getname%") DO (SET name=%%E) 

SET printername=%name%

SET servername=Server-%siteid%-01

OK, so that seems to work ok and im able to get the parts i need from the file name and assign them to variable so that %name%=WHID-PRT-MFP-3 and %siteid%=WHID

Now my dilema is that i need the file name to be: NORTH EAST OFFICE, WHID-PRT-MFP-3.bat So I've been trying to get the entire line behind the comma(WHID-PRT-MFP-3) to a variable and the first item in dashes(WHID) to another variable, same as the example above, but cant figure it out. This will be used with several names so not only as the example shown i could have shorter or longer names for both parts, say: WHID-PRT-1, OFFICE 1

Any ideas are welcome.

Thanks

解决方案

NORTH EAST OFFICE, WHID-PRT-MFP-3.bat:

SET getname=%~n0

rem split at comma
for /f "tokens=2 delims=," %%E IN ("%getname%") DO (

    rem trim space after comma
    for /f "tokens=*" %%S IN ("%%E") DO SET name=%%S

    rem get word before hyphen and strip leading space
    for /f "tokens=1 delims=- " %%S IN ("%%E") DO set siteid=%%S
)

SET printername=%name%
SET servername=Server-%siteid%-01

这篇关于拆分自己的文件名,并在批处理脚本分配特定部分变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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