Home > Net >  Capturing variables from a CSV file determined by numeric range in a Windows Batch File
Capturing variables from a CSV file determined by numeric range in a Windows Batch File

Time:02-02

I have the following data in a # delimited CSV data file called clean.txt:

1 LINK#FSN News Cart#<< DO NOT DELETE or MOVE >>#0#27000 
2 LINK#News Outro#<< DO NOT DELETE or MOVE >>#27000#33000 
3 SONG#Blow My Mind (feat. Chris Brown)#Davido#33000#199000 
4 SONG#So Good#Alex Blue#199000#361000 
5 SONG#Easy To Love (feat. Teddy Swims)#Armin van Buuren and Matoma#367000#517000 
6 SONG#Anti-Hero (Roosevelt Remix)#Taylor Swift#517000#725000 
7 SONG#Simmer (feat. Burna Boy) (Majestic Remix)#Mahalia#730000#929000 
8 SONG#Pyramid (Michael Calfan Remix)#John Dahlback#929000#1121000 
9 SONG#I Told You Once#Si Sax#1121000#1311000 
10 SONG#I'm Every Woman#Whitney Houston and SG Lewis#1311000#1538000 
11 SONG#Overnight#Tobtok and Adam Griffin#1544000#1698000 
12 SONG#Hungry (For Love)#LF SYSTEM#1698000#1840000 
13 LINK#Slogan#Theme A#1840000#1849000 
14 SONG#If You Want Me To Stay (Eric Kupper Edit)#The Lovefreekz and Nick Reach Up#1849000#2029000 
15 SONG#Never Too Late#Olivia Nelson#2029000#2220000 
16 SONG#Celestial#Ed Sheeran#2224000#2427000 
17 SONG#Out Of My Depth (feat. Nu-La)#CHANEY#2427000#2595000 
18 SONG#All By Myself#Alok, Sigala and Ellie Goulding#2595000#2761000 
19 SONG#Sweet Lies#Nathan Dawe and Talia Mar#2761000#2880000 
20 SONG#Down In Atlanta#Pharrell Williams and Travis Scott#2882000#3038000 
21 SONG#More Than Ever#Harry Shadow and Jodapac#3038000#3216000 
22 SONG#What You Gonna Do (feat. Taet)#CLIK3D#3216000#3391000 
23 SONG#Let It Go#Ryan Shepherd#3391000#3555000 
24 SONG#2 Be Loved (Am I Ready) (PNAU Remix)#Lizzo#3555000#3773000 
25 SONG#Perfect Love#Tom Westy and Mila Falls#3773000#3931000 
26 LINK#ID#Theme A#3931000#3936000 
27 LINK#Website Promo#HHUK#3936000#3974000 
28 LINK#News Intro and Bed - WITH FADE#<< DO NOT DELETE or MOVE >>#3974000#3983000

The fields on each row are:

<ITEM>#<TITLE>#<ARTISTS>#<START>#<END>

START and END are integers in milliseconds.

The objective is to display the <TITLE> - <ARTIST> that corresponds to a time between <START> and <END> The time is provided in an input variable.

The following script works with a hard-coded input variable (Dur) of 1000000 or less, but if I go above that I get multiple results and I can’t figure out why.

set /a Dur=1000000

for /F "tokens=1-5 delims=#" %%i in (clean.txt) do (

    If %Dur% GEQ %%l (if %Dur% LEQ %%m echo %%i %%l %%m TRACK %%k - %%j)
)

I have tried dividing all of the numbers in the data file by 1000 to reduce the size but that didn’t make any difference.

CodePudding user response:

Your comparisons are bein performed alphabetically; 1 as the first character in a string is always less than any other digit.

You need to force arithmetic comparison, or find a way around the string-comparison.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
set /a Dur=1200000

for /F "tokens=1-5 delims=#" %%i in (q75313286.txt) do (
 rem will set cond1 to  ve if first condition is true, -ve otherwise
 SET /a cond1=%dur% - %%l
 rem ditto second condition
 SET /a cond2=%%m - %dur%
 ECHO !cond1!!cond2! |FIND "-">NUL
 rem if either is negative, errorlevel will be 0
 IF ERRORLEVEL 1 echo %%i %%l %%m TRACK %%k - %%j
 rem If %Dur% GEQ %%l (if %Dur% LEQ %%m echo %%i %%l %%m TRACK %%k - %%j)
)
GOTO :EOF

Always verify against a test directory before applying to real data.

I used a file named q75313286.txt containing your data for my testing.

  • Related