1

USING BATCH/BASH On Windows: I'm wondering how to search a text file (.txt) line by line as each line is its own string, look for the location directory (e.g, C:...) which is part of this string. and just take out that specific part and store in a variable.

This is to be done for each line though.

Any help would be appreciated.

TestRecID TestUserID 5 2017-04-20 TestTAtRec No 2.560 No C:\Test1\Test3 Tester
TestRecID TestUserID 5 2017-04-20 TestTAtRec No 2.560 No C:\Test2\Test2 Tester
TestRecID TestUserID 5 2017-04-20 TestTAtRec No 2.560 No C:\Test3\Test1 Tester

Presume that the above are each row in the text file. At each space in the line that is where the different columns would have been in the DB.

The Expected Result would be to have:

C:\Test1\Test3 --> Variable 1
C:\Test2\Test2 --> Variable 2
C:\Test3\Test1 --> Variable n

Stored in some variable.

I really can't overstate enough how much of a newb I am with Batch/Bash for Windows

Follow Up Question:

for /f "tokens=9 delims= " %%a in (%findfile%) do echo %%a 

I want to then store the %%a in a variable_name

I thought it would be a case of:

for /f "tokens=9 delims= " %%a in (%findfile%) do echo %%a
set variable_name = %%a

But it is not working??

Was Answered but I got given out to for asking a question in an answer,

The Answer given:

for /f "tokens=9 delims= " %%b in (%findfile%) do set "location=%%b"

EDIT Working Loop for taking unique variables (Thanks to @Stephen):

REM Get Locations from textfile:
for /f "skip=1 tokens=9 delims= " %%a in (%findfile%) do set "_%%a=yes"
REM Translate into proper variables:
set count = 0
for /f "tokens=1* delims==#" %%a in ('set _') do (
    set /a count+=1
    set _location[%count%]=%%a
)
set _location

This is the FFMPEG function I'm using but I now need it to take in each variable separately:

for %%i in (%_location%\*.mp4) do (if not exist "%%~ni\" MD "%%~ni"
    ffmpeg -i "%%i" -vframes 1 -f image2 -start_number 0 "%%~ni\%%~ni_Summary_%%3d.jpeg"
)

The %_location% part in the function above is where my issue is? of it not taking multiple variables at once

Jay1995
  • 137
  • 1
  • 14
  • show that file fragment and the expected result – RomanPerekhrest Apr 20 '17 at 08:32
  • What should happen if multiple paths are present on a single line? – choroba Apr 20 '17 at 08:33
  • Searching a text (line) can be done with grep or awk (or other). Further filtering/replacement of matched text can be done with grep, sed, awk (or other). Please, describe your problem in more detail. – Scheff's Cat Apr 20 '17 at 08:36
  • there will never be more than one path on a line – Jay1995 Apr 20 '17 at 08:36
  • I have a text file, its storing the results line by line from a database. Each row in the textfile is a string of all the columns from the database, i need to search each row with batch and store the directory location from each row in a variable so it can then be used in another piece of batch which ive already working. – Jay1995 Apr 20 '17 at 08:38
  • will we see your input text? – RomanPerekhrest Apr 20 '17 at 08:38
  • I cant show the text file as it is not mine to share... – Jay1995 Apr 20 '17 at 08:41
  • 3
    `for /f "tokens=8,*" %%a in (file.txt) do echo %%b` or ``for /f "tokens=9" %%a in (file.txt) do echo %%a` - depends if `Tester` is part of the path or just the next column. – Stephan Apr 20 '17 at 11:33
  • @Stephan This is returning nothing, the batch file is just ending?? – Jay1995 Apr 20 '17 at 12:22
  • for /f "tokens=9 delims= " %%a in (%findfile%) do echo %%a ... This is what i got to get it working in the end, thank you @Stephan for yourway that got me there – Jay1995 Apr 20 '17 at 13:49
  • several lines in one variable? What is the purpose of that? (what do you want to do with that variable?) – Stephan Apr 20 '17 at 15:10
  • [tag:batch-file] or [tag:bash]?? There are totally different things, so read the tag info... – aschipfl Apr 20 '17 at 20:45

1 Answers1

1

As it makes no sense to store the whole output into one variable, I assume, you want one variable for each of the lines.

@echo off
setlocal EnableDelayedExpansion
set n=0
for /f "tokens=9" %%a in (file.txt) do (
  set /a n+=1
  set _Var[!n!]=%%a
)
set _Var[

Note the parantheses after do. This is called a code block and is parsed and executed like a single command. (this is why you need delayed expansion).

EDIT to remove duplicates, you can either use a lot of code to compare the (maybe) new data with each of the already existing variables or use a strange trick: use the data as variable names (and some dummy string as value), so each duplicate will just "overwrite" an already existing variable and in a second step put the data (now variable names) as values into variables.

@echo off 
SetLocal EnableDelayedExpansion
REM clear variables:
for /f "delims==" %%a in ('set _') do set "%%a="
REM get locations from textfile:
for /f "tokens=9" %%a in (file.txt) do set "_%%a=yes"

REM translate into proper variables:
set count=0
echo on
for /f "tokens=1* delims==#" %%a in ('set _') do (
  set /a count+=1
  set x=%%a
  set _var[!count!]=!x:~1!
)
set _var

Note: your data shouldn't contain exclamation marks (!) due to delayed expansion.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Is there a way of getting this to only choose unique ones and store only them in a variable? – Jay1995 Apr 24 '17 at 07:46
  • The variable at the token of 9 is a location, there will be mostly different locations and each location is then put into an FFMPEG Function, but as it stands it only takes out one location and passes that through the function.... what I'm wondering is there a way of it only taking the unique ones from the list it creates and store them in separate variables? then loop those variables into the FFMPEG function? – Jay1995 May 02 '17 at 13:06
  • @Stephen This is the error message its giving me? "La variable d'environnement _ n'est pas définie. do( était inattendu." – Jay1995 May 05 '17 at 11:42
  • That's from trying to clear variables, that don't exist. Change `... in ('set _') do ... ` to `in ('set _ 2^>nul') do `. A space is required between `do` and `(`. – Stephan May 05 '17 at 11:55
  • you're brilliant! that worked! any chance i could get you take a look a once last thing in the edited question above?? – Jay1995 May 05 '17 at 13:07
  • What is `%%3d` supposed to do? – Stephan May 05 '17 at 13:13
  • A number on the end of the image name no more than 3 digits so like, 001,002,003,etc... – Jay1995 May 05 '17 at 13:17
  • no such thing in batch. You have to write your own logic to do that. (except, it is a `FFMPEG`- thingy) – Stephan May 05 '17 at 13:20
  • I'm still getting my bearings with the whole thing and it works so i didn't question it. but its gettiing FFMPEG to take in the multiple variables is my problem – Jay1995 May 05 '17 at 13:25
  • remove `echo off` to see exactly, how the code is executed. Maybe this gives you some hints. – Stephan May 05 '17 at 13:26
  • side question: how do you get it to stop showing the stored variable with a underscore at the start of it? – Jay1995 May 09 '17 at 08:19
  • as I think that may be my problem as to why FFMPEG isn't taking taking it – Jay1995 May 09 '17 at 08:28
  • That worked perfectly for getting rid of the underscore, thank you! It still isn't working with FFMPEG though – Jay1995 May 09 '17 at 08:49
  • From what im seeing when the echo is turned on `for %%i in (%_location%\*.mp4) do (if not exist "%%~ni\" MD "%%~ni" ffmpeg -i "%%i" -vframes 1 -f image2 -start_number 0 "%%~ni\%%~ni_Summary_%%3d.jpeg" )` where the **%_location%**\*.mp4 is its showing up blank before the backslash – Jay1995 May 09 '17 at 08:56
  • in your question you do a `set "location=%%b", but in your comment above, you use `%_location%`. Typo or error in code? – Stephan May 09 '17 at 09:29
  • typo of sorts if i understand you correctly, as originally i was using `set "location=%%b"` but that was from before you helped with putting them into separate variables – Jay1995 May 09 '17 at 10:30
  • i wonder if the FFMPEG function was put within another For loop? That for loop, looping through each variable assigned within **location** and assigning them to one value in turn, then putting that value in to the FFMPEG function? Big thing is though I don't know how to go about doing that – Jay1995 May 09 '17 at 10:53
  • Ive since realised you have to get them to show up ya have to put in the array part you want so now definitely the other loop is need outside that one `for %%i in (%_location[1]%\*.mp4) do (if not exist "%%~ni\" MD "%%~ni" ffmpeg -i "%%i" -vframes 1 -f image2 -start_number 0 "%%~ni\%%~ni_Summary_%%3d.jpeg" )` – Jay1995 May 09 '17 at 13:34