批处理脚本以从变量中使用字符串(带空格)重命名文件 [英] batch script to rename a file with string (with space) from variable

查看:539
本文介绍了批处理脚本以从变量中使用字符串(带空格)重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个名为doctitle.txt的文件,其中包含标题.我想用这个标题来重命名另一个文件,当前命名为file.pdf,所以我做了:

There is a file named doctitle.txt which contains the title. I want to use this title to rename another file, currently name file.pdf, so I did:

for /f "delims=" %%x in (doctitle.txt) do set "DOCTITLE=%%x"
move file.pdf %DOCTITLE%.pdf

如果标题字符串(即"DocumentTitle")中没有空格,则此方法很好.但是如果标题中有空格,即文档标题",则失败.

This works fine, if there no space in the title string, i.e "DocumentTitle". But fails if there is a space in the title, i.e "Document Title".

如何解决这个问题?

推荐答案

尝试:

for /f "tokens=*" %%x in (doctitle.txt) do set DOCTITLE=%%~x
move file.pdf "%DOCTITLE%.pdf"

这样,变量DOCTITLE将不会被引号引起来,因为%%~会删除所有引号.

That way, the variable DOCTITLE will not be surrounded with quotes as %%~ removes any quotes.

报价for /?:

%~I         - expands %I removing any surrounding quotes (")

这篇关于批处理脚本以从变量中使用字符串(带空格)重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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