batch script to rename a file with string (with space) from variable

痴心易碎 提交于 2020-02-06 02:45:30

问题


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

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".

What could be done to overcome this issue?


回答1:


Try:

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

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

Quoting for /?:

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


来源:https://stackoverflow.com/questions/31173391/batch-script-to-rename-a-file-with-string-with-space-from-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!