问题
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