0
点赞
收藏
分享

微信扫一扫

Windows批处理的一些方法

彭维盛 2022-05-03 阅读 69
经验分享

Windows批处理的一些方法

读取配置文件

@echo off & setlocal enabledelayedexpansion

title "数据导出M"

::系统存放目录
set sys_disk=E:/数据信息梳理/unload
set zeng=1
set quan=0
::echo %sys_disk%
::cd  /d %sys_disk%
::echo %cd%

::读取config.ini配置文件
for /f  "tokens=1,2 delims==" %%b in (%sys_disk%/conf/conf.ini) do (
  if "%%b"=="user" set user=%%c
  if "%%b"=="pass" set pass=%%c
  if "%%b"=="dbsid" set dbsid=%%c
  
  if "%%b"=="FTPAddress" set FTPAddress=%%c
  if "%%b"=="TargetPath" set TargetPath=%%c
  if "%%b"=="ftpuser" set ftpuser=%%c
  
  ::由于密码中有!字符,disabledelayedexpansion可读取到!
  setlocal disabledelayedexpansion
  if "%%b"=="ftppass" set ftppass=%%c
  setlocal enabledelayedexpansion
)

获取年月日

for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime /value') do (
  set t=%%a
)
::echo %t%
set Today=%t:~0,4%%t:~4,2%%t:~6,2%
set ToYear=%t:~0,4%
set toMonth=%t:~4,2% 

计算字符串长度

:stringLenth
	::字符串长度计算子程序 
	set thestring=%~1
	::@echo 字符串 %thestring%
	::参数%1 为字符串"%str%",%~1则去掉"%str%"的双引号。 
	if not defined theString (
	   ::设置如果字符串为空,长度为0
		set Return=0
		set %2=0
		goto :eof
	)
	set Return=0
	
:stringLenth_continue
	set /a Return+=1
	set thestring=%thestring:~0,-1%
	::偏移量为 1,截取倒数第1位前的所有字符
	if defined thestring goto stringLenth_continue
	::参数%2 为返回变量 num的名称,不能含空格或特殊字符
	if not "%2"=="" set %2=%Return%
	goto :eof
	
	
使用方法:

call :stringlenth 12345678 num
echo %num%

获取指定日期(Today)的前一天

@echo off & setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime /value') do (
  set t=%%a
)
::echo %t%
set Today=%t:~0,4%%t:~4,2%%t:~6,2%
::echo,%Today%

::set /p Today=输入一个日期(YYYMMDD):

::set Today=%1
::echo "%Today%"

:yesterday
	SET year=%Today:~,4%
	SET month=%Today:~4,2%
	SET day=%Today:~6,2%
	 
	REM /*   是否是闰年  */
	SET /a ytype="!(year%%4)&!(!(year%%100)|!(year%%400))"
	REM ECHO %ytype%

	 
	REM /*   获取昨天日期  */
	IF %month%%day%==0101 GOTO CASE01
	IF %month%%day%==0201 GOTO CASESMALLMONTH
	IF %month%%day%==0301 GOTO CASE03
	IF %month%%day%==0401 GOTO CASESMALLMONTH
	IF %month%%day%==0501 GOTO CASEBIGMONTH
	IF %month%%day%==0601 GOTO CASESMALLMONTH
	IF %month%%day%==0701 GOTO CASEBIGMONTH
	IF %month%%day%==0801 GOTO CASEBIGMONTH
	IF %month%%day%==0901 GOTO CASESMALLMONTH
	IF %month%%day%==1001 GOTO CASESMALLMONTH
	IF %month%%day%==1101 GOTO CASE11
	IF %month%%day%==1201 GOTO CASE12
	 
	IF %day%==02 GOTO CASESINGLENUM
	IF %day%==03 GOTO CASESINGLENUM
	IF %day%==04 GOTO CASESINGLENUM
	IF %day%==05 GOTO CASESINGLENUM
	IF %day%==06 GOTO CASESINGLENUM
	IF %day%==07 GOTO CASESINGLENUM
	IF %day%==08 GOTO CASESINGLENUM
	IF %day%==09 GOTO CASESINGLENUM
	 
	SET /A day=day-1
	SET yestoday=%year%%month%%day%
	GOTO unload
 
:CASE01
	SET /A y=%year%-1
	SET yestoday=%y%1231
	GOTO unload
:CASE03
	IF 1==%ytype% (
		SET /A m=%month:~1,1%-1
		SET yestoday=%year%0%m%28
	) ELSE (
		SET /A m=%month:~1,1%-1
		SET yestoday=%year%0%m%29
	)
	GOTO unload	
:CASESMALLMONTH
	SET /A m=%month:~1,1%-1
	SET yestoday=%year%0%m%31
	GOTO unload
:CASEBIGMONTH
	SET /A m=%month:~1,1%-1
	SET yestoday=%year%0%m%30
	GOTO unload
:CASE11
	SET /A m=%month:~1,1%-1
	SET yestoday=%year%%m%30
	GOTO unload
:CASE12
	SET /A m=%month:~1,1%-1
	SET yestoday=%year%%m%31
	GOTO unload
:CASESINGLENUM
	SET /A d=%day:~1,1%-1
	SET yestoday=%year%%month%0%d%
	GOTO unload
	
:unload
   echo 昨天日期 !yestoday! 
   

上个月最后一天的日期

@echo off
setlocal enabledelayedexpansion
rem 假设系统日期格式为yyyy-mm-dd
set "year=%date:~0,4%"
set "month=%date:~5,2%"
set /a "month=1!month!-101, month=month+(^!month)*12"
rem 上月是否2月或小月
set /a "f=^!(month-2), s=^!(month-4)|^!(month-6)|^!(month-9)|^!(month-11)"
rem 今年是否闰年
set /a "leap=^!(year%%4) & ^!^!(year%%100) | ^!(year%%400)"
rem 上月有!d!天
set /a "d=f*(28+leap)+s*30+(^!f&^!s)*31"
set /a "year1=year-^!(month-12)"
set "month=0!month!"
set "month=!month:~-2!"
set "d=0!d!"
set "d=!d:~-2!"
:: 上月最后一天
set lastmonthdate=!year1!-!month!-!d!
echo,%lastmonthdate%
pause
举报

相关推荐

0 条评论