批次檔將部分最後修改的字串取得到變數

批次檔將部分最後修改的字串取得到變數

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

進一步閱讀

相關內容