Home > Mobile >  Reading parameter inputs from a txt/csv file to run a command in loop (windows batch)
Reading parameter inputs from a txt/csv file to run a command in loop (windows batch)

Time:01-03

Kindly excuse me if it seems a bit amateur. I'm not so good with windows batch files.

I am using Instaloader to download single posts. Instaloader can take an input with a content ID. I am trying to write a batch file so that it can take content IDs as an input from a csv file and download it in a loop.

Here is my sample which works but I have to input an ID every time. I would like it to take the ID from a csv file until it finishes all the rows.

@echo off
:prompt
set /p post= "Enter ID: "
instaloader --no-metadata-json --no-compress-json --no-captions --dirname-pattern={profile} --filename-pattern {profile}_{date_utc} -- -%post%
echo.
goto prompt

I should use FOR I believe but I just couldn't get it work. Thanks for any suggestions.

I tried a few samples of using FOR but it just fails. I am not sure if there is a better approach to this. I don't mind using powershell if it can be done in a more efficient way like using Import-CSV first.

The PS attempt:

foreach ($post in Get-Content '.\IG Posts.txt') 
{
 do { instaloader --no-metadata-json --no-compress-json --no-captions --dirname-pattern={profile} --filename-pattern {profile}_{date_utc} -- -$post } while (eol) 
}

Sample txt/csv (they are actual content IDs that instaloader would download:

Cm4AoMVy60x
Cm0UMTAyLJe
ClViwoRStoa

CodePudding user response:

[Theoretical and untried]

@echo off
setlocal
if "%~1"=="" echo Syntax %0 "filename containing list"&goto :eof
for /f "usebackq delims=" %%b in ("%~1") do instaloader --no-metadata-json --no-compress-json --no-captions --dirname-pattern={profile} --filename-pattern {profile}_{date_utc} -- -%%b
goto :eof

where this batch should be invoked as
thisbatch "filename"

%1 is the first parameter on the command line. %~1 is the same, minus any enclosing quotes. "%~1" is the (first paramater - enclosing quotes) enclosed in quotes.

In the for /f line, the usebackq means "the thing in quotes is a filename" and delims= means "there are no delimiters on the lines of the file".

So each line of the files is assigned to %%b in turn and instaloader is run using the contents of the line.

Note that if instaloader is a batch file, you should use call instaloader...

I'm assuming a textfile contains the required data, one to a line. If you want otherwise, please post a sample.

  • Related