批处理文件获取部分 lastmodified 字符串到变量

批处理文件获取部分 lastmodified 字符串到变量

WMIC path Win32_Directory WHERE name="W:\\foldername" get lastmodified

返回类似这样的内容

上一次更改
20140612095434.758265-240

我想将其放入批处理文件中,以便将该字符串的粗体部分分配给变量,以便稍后在批处理文件中使用它。

任何帮助都将不胜感激!

这是我迄今为止的批处理文件,仅供参考

@echo off
@cls
net use W: \\file\home\ex-employees
cd W:
W:

REM Get user ID
set /p id="Enter ID of user to Archive: "

REM Get last modified code goes here assigned to "LM" variable
REM
REM WMIC path Win32_Directory WHERE name='W:\\rsink' get lastmodified

REM Join Variables
call set filename=%%%id%%LM%%%

call zipjs.bat zipItem -source %id% -destination .\%filename%.zip -keep yes -force no

rmdir /S /Q %id%

Echo All Done!
@pause

答案1

我想将其放入批处理文件中,将该字符串的粗体部分分配给变量

WMIC path Win32_Directory WHERE name="W:\\foldername" get lastmodified

返回类似这样的内容

上一次更改

20140612095434.758265-240

你可以使用for /f循环来实现这一点。这是一个小例子。

获取上次修改日期.cmd:

@echo off 
rem GetLastModifedDate.cmd
setlocal enabledelayedexpansion
rem skip header line
rem use findstr to remove blank lines
for /f "skip=1 tokens=*" %%d in ('WMIC path Win32_Directory WHERE name^="f:\\test" get lastmodified ^| findstr /r /v "^$"') do (
  set LM=%%d
  rem required part is alway 8 chars yyyymmdd so strip first 8 chars
  set LM=!LM:~0,8!
  )
echo %LM%
endlocal

您的批处理文件需要进行以下修改:

@echo off
@cls
setlocal enabledelayedexpansion 
net use W: \\file\home\ex-employees
cd W:
W:

REM Get user ID
set /p id="Enter ID of user to Archive: "

REM Get last modified code goes here assigned to "LM" variable
rem skip header line
rem use findstr to remive blank lines
for /f "skip=1 tokens=*" %%d in ('WMIC path Win32_Directory WHERE name^="W:\\rsink" get lastmodified ^| findstr /r /v "^$"') do (
  set LM=%%d
  rem required part is alway 8 chars yyyymmdd so strip first 8 chars
  set LM=!LM:~0,8!
  )

REM Join Variables
call set filename=%%%id%%LM%%%

call zipjs.bat zipItem -source %id% -destination .\%filename%.zip -keep yes -force no

rmdir /S /Q %id%

Echo All Done!
@pause

进一步阅读

相关内容