Extract date from date & time string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Extract date from date & time string

#1 Post by SIMMS7400 » 02 Aug 2018 02:17

Hi Folks -

I have a text file that looks like this:

Code: Select all

"Code1"|"Alias1"|"PFR-4115-100"|"Smith, John"|"John.Smith@Emali.com"|10/5/2018 5:41:45|"Complete"
"Code1"|"Alias1"|"PFR-4115-100"|"Smith, John"|"John.Smith@Emali.com"|4/5/2018 15:41:45|"Complete"
I need to extract the date (6th token) and place in a variable. What's the easiest way to go about this? I've tried string manipulation but I'm getting tripped up.

Here is what I have so far - see !DATESTRING! variable:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
PUSHD "%LOCALEXPORTPATH%"
	
	SET "YEAR=%date:~-4,4%"
	SET "MONTH=%date:~-10,2%"
	SET "DAY=%date:~-7,2%"
	
	IF "%MONTH%"=="01" SET "MONTH=1"
	IF "%MONTH%"=="02" SET "MONTH=2"
	IF "%MONTH%"=="03" SET "MONTH=3"
	IF "%MONTH%"=="04" SET "MONTH=4"
	IF "%MONTH%"=="05" SET "MONTH=5"
	IF "%MONTH%"=="06" SET "MONTH=6"
	IF "%MONTH%"=="07" SET "MONTH=7"
	IF "%MONTH%"=="08" SET "MONTH=8"
	IF "%MONTH%"=="09" SET "MONTH=9"
	IF "%DAY%"=="01"   SET "DAY=1"
	IF "%DAY%"=="02"   SET "DAY=2"
	IF "%DAY%"=="03"   SET "DAY=3"
	IF "%DAY%"=="04"   SET "DAY=4"
	IF "%DAY%"=="05"   SET "DAY=5"
	IF "%DAY%"=="06"   SET "DAY=6"	
	IF "%DAY%"=="07"   SET "DAY=7"
	IF "%DAY%"=="08"   SET "DAY=8"	
	IF "%DAY%"=="09"   SET "DAY=9"
	
	SET "SEARCH=%MONTH%/%DAY%/%YEAR%"
	FOR /F "usebackq tokens=1-7 delims=|" %%A IN ("%MSACCES_EXP%") DO (
		
		SET "FLAG="
		SET "CODE=%%~A"
		SET "CODEALIAS=%%~B"
		SET "CODEPARENT=%%~C"
		SET "NAME=%%~D"
		SET "EMAIL=%%~E"
		SET "DATESTRING=%%~F" & SET "DATESTRING=!DATESTRING:~0,10!" & SET "DATESTRING=!DATESTRING: =!"
		SET "STATUS=%%~G"

		ECHO "%SEARCH%" | FINDSTR "!DATESTRING!" >nul 2>&1 && SET "FLAG=Y"
	
		IF DEFINED FLAG (
			ECHO "%%A" | FINDSTR /C:"PFP" >nul 2>&1 && SET "STRING=PFP"
			ECHO "%%A" | FINDSTR /C:"PFI" >nul 2>&1 && SET "STRING=PFI"
			ECHO "%%A" | FINDSTR /C:"PFC" >nul 2>&1 && SET "STRING=PFC"
			ECHO "%%A" | FINDSTR /C:"PFR" >nul 2>&1 && SET "STRING=PFR"

			IF "!STATUS!"=="Complete" CALL :EMAIL 0
			IF "!STATUS!"=="Rejected" CALL :EMAIL 1
		)
	)
POPD
Thanks!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Extract date from date & time string

#2 Post by Squashman » 02 Aug 2018 08:53

The date and time are separated by a space. So you can use a FOR /F command just like you are doing now to split up the date and time into two tokens.

Post Reply