bat批处理针对带有空格路径的处理方法

最近在写windows平台下tomcat的数据采集脚本,发现bat针对空格路径处理很费劲,就简单的动了下小心思,自己写了个办法,因为我没真是没在网上找的更好的办法,随说有人说加双引号,我是要做路径拼接,这个不能很直接的解决问题

思路

  • 无论输入的路径是否带空格或者是否带双引号,都按照带双引号的来处理,做去除双引号处理,函数MoveQuotationMarks

  • 进行文件路径的拼接,addFile

  • 再次将拼接后的路径加上双引号


@echo off



echo please input
set /p filepath=

rem echo %filepath%
rem call:MoveQuotationMarks
rem echo %filepath%
rem call:AddQuotationMarks
rem echo %filepath%

call:addFile web.xml

echo %filepath%

type %filepath% >> 123.txt
:MoveQuotationMarks
	set filepath=%filepath:"=%
goto:eof

:AddQuotationMarks
	set filepath="%filepath%"
goto:eof

:addFile
	call:MoveQuotationMarks
	if "%filepath:~-1%"=="/" (
		set filepath=%filepath%%1
	) else (
		if "%filepath:~-1%"=="/" (
			set filepath=%filepath%%1
			) else (
				set filepath=%filepath%\%1
			)
	)
	call:AddQuotationMarks

got:eof

posted @ 2019-09-23 09:23  随时静听  阅读(4777)  评论(0编辑  收藏  举报