Piriwn1
Piriwn1

Reputation: 31

Setting multiple variables from 1 line in text file

I am using a sqlcmd to export 3 dates to a temp file, all 3 are in one line and i need to separate them they are formatted as [YYYY-MM-DD HH:MM:SS.000] for each one so the line in the text file looks like:

[YYYY-MM-DD HH:MM:SS.000] [YYYY-MM-DD HH:MM:SS.000] [YYYY-MM-DD HH:MM:SS.000]
set /p date=< %tmp%
setlocal enabledelayedexpansion
set st=%date%
set d1=!st:~0,19!
set d2=!st:~24,43!
set d3=!st:~48,67!

echo Date1 [%d1%]
echo Date2 [%d2%]
echo Date3 [%d3%]
setlocal disabledelayedexpansion

When I run this it should output [YYYY-MM-DD HH:MM:SS] for each date, which works for the first date.

The second date outputs as [YYYY-MM-DD HH:MM:SS.000 YYYY-MM-DD HH:MM:SS.000], the first set is the second date and the second is the third date.

The third date is output is [YYYY-MM-DD HH:MM:SS.000], adding the .000 even though the character count ends after the seconds.

Upvotes: 0

Views: 142

Answers (1)

user7818749
user7818749

Reputation:

by using the same file you are using now, but by changing the variable to %mytmp% due to %tmp% being a reserved system variable:

@echo off
set "mytmp=C:\path to temp\file.txt"
for /f "usebackq tokens=1,3,5" %%a in ("%mytmp%") do echo %%a %%b %%c

To utilize them the same as you did in your example (without the need to set variables):

@echo off
set "mytmp=C:\path to temp\file.txt"
for /f "usebackq tokens=1,3,5" %%a in ("%mytmp%") do (
    echo Date1 [%%a]
    echo Date2 [%%b]
    echo Date3 [%%c]
)

Upvotes: 2

Related Questions