batch file to get partial lastmodified string to variable

LeperMessiah

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

returns something like this

LastModified
20140612095434.758265-240

I would like to put this in a batch file to assign the bolded portion of that string to a variable so I can use it later in the batch file.

Any help is appreciated!

This is my batch file so far for reference

@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
DavidPostill

I would like to put this in a batch file to assign the bolded portion of that string to a variable

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

returns something like this

LastModified

20140612095434.758265-240

You can use a for /f loop to do this. Here is a small example.

GetLastModifiedDate.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

Your batch file with the needed modifications:

@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

Further Reading

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Characters in a batch file string variable

From Dev

Batch file: How to get variable's value using variable's name already stored in a string

From Dev

Use string in text file as a variable in batch

From Dev

split the string value stored in a variable by a batch file

From Dev

Batch file, string variable comparison, if statement

From Dev

Escaping spaces in string variable in batch file

From Dev

Is it possible to get a variable from a string in batch?

From Dev

Batch File get Specific IP into Variable

From Dev

How to skip over .lastModified() files that contain a String, and select the next .lastModified() file

From Dev

Text file: How to get the text between string1 & next instance of string2 and set as Batch variable?

From Dev

Windows batch file to find variable string in a html file

From Dev

Batch file : SET variable=string doesn't work

From Dev

How to find and replace a string in %PATH% system variable with variables in a batch file?

From Dev

Using variable in sub-string operation in batch file FOR loop

From Dev

Batch How to remove file extension stored in string %variable%?

From Dev

Windows command batch file calling variable from string in nested loop

From Dev

Batch - Search text file for specified string and if found set variable to 1

From Dev

Parsing a string variable in a batch file for LAPS / ADU&C

From Dev

How to find and replace a string in %PATH% system variable with variables in a batch file?

From Dev

Delete portion of string from variable including and after pattern with batch file

From Dev

Get last character of a string from a variable Windows NT batch CMD

From Dev

Batch-file get CPU temperature in °C and set as variable

From Dev

Love to get the output of powershell script in a variable to use in a batch file

From Dev

Love to get the output of powershell script in a variable to use in a batch file

From Dev

Batch-file get CPU temperature in °C and set as variable

From Dev

Variable doesn't get updated in the function of Batch file

From Dev

How to get a particular line from a file into a batch variable?

From Dev

Select columns with a partial string variable

From Dev

String replace with variable in batch

Related Related

  1. 1

    Characters in a batch file string variable

  2. 2

    Batch file: How to get variable's value using variable's name already stored in a string

  3. 3

    Use string in text file as a variable in batch

  4. 4

    split the string value stored in a variable by a batch file

  5. 5

    Batch file, string variable comparison, if statement

  6. 6

    Escaping spaces in string variable in batch file

  7. 7

    Is it possible to get a variable from a string in batch?

  8. 8

    Batch File get Specific IP into Variable

  9. 9

    How to skip over .lastModified() files that contain a String, and select the next .lastModified() file

  10. 10

    Text file: How to get the text between string1 & next instance of string2 and set as Batch variable?

  11. 11

    Windows batch file to find variable string in a html file

  12. 12

    Batch file : SET variable=string doesn't work

  13. 13

    How to find and replace a string in %PATH% system variable with variables in a batch file?

  14. 14

    Using variable in sub-string operation in batch file FOR loop

  15. 15

    Batch How to remove file extension stored in string %variable%?

  16. 16

    Windows command batch file calling variable from string in nested loop

  17. 17

    Batch - Search text file for specified string and if found set variable to 1

  18. 18

    Parsing a string variable in a batch file for LAPS / ADU&C

  19. 19

    How to find and replace a string in %PATH% system variable with variables in a batch file?

  20. 20

    Delete portion of string from variable including and after pattern with batch file

  21. 21

    Get last character of a string from a variable Windows NT batch CMD

  22. 22

    Batch-file get CPU temperature in °C and set as variable

  23. 23

    Love to get the output of powershell script in a variable to use in a batch file

  24. 24

    Love to get the output of powershell script in a variable to use in a batch file

  25. 25

    Batch-file get CPU temperature in °C and set as variable

  26. 26

    Variable doesn't get updated in the function of Batch file

  27. 27

    How to get a particular line from a file into a batch variable?

  28. 28

    Select columns with a partial string variable

  29. 29

    String replace with variable in batch

HotTag

Archive