0

I'm using a script to store the content of a txt file in a variable. Now I have a lot of these files, and I want to go over every single one of them in a for loop.

Pseudo code:
for (i = 1, i < 100, i += 1) (
set var=file+i.txt
[do stuff with file]
)

How can I make something like this?

JanJansen
  • 9
  • 2

1 Answers1

0

cmd.exe supports a for loop, like:

for %c in ( file*.txt ) do process %c

It supports a fair number of options for things like getting only the base name of the file in question, so if (for example) you wanted to the .txt files and produce a file with the same base name and the extension changed to, say, .dat, that's pretty easy to do.

In this case, the apparent intent (gleaned from multiple comments) is to step through some files named tweetNNN.txt, where NNN is some number from 1 to 100. The content (not just the name) of that file is then to be passed on a cURL command line as data in a request.

The easiest way to do this is probably to use the @ character on the cURL command line, something like this:

for /l %c in (1, 1, 100) do echo "language=english&text=" > stage.txt 
    && copy stage.txt + tweet%c.txt 
    && curl -d@stage.txt text-processing.com/api/sentiment/ >> results.txt

(Note: I've formatted this with line-breaks, but it needs to be entered as a single line).

I put together a quick test, using a couple of files:

tweet1.txt: "what complete crap. hated every minute"
tweet2.txt: "Best movie of the years. Loved it"

Running the previous command in a directory containing those two files produced a results.txt containing:

{"probability": {"neg": 0.82811145456964252, "neutral": 0.18962854533013332, "pos": 0.17188854543035748}, "label": "neg"}
{"probability": {"neg": 0.10467714372495518, "neutral": 0.080508941181180751, "pos": 0.89532285627504482}, "label": "pos"}

That seems a close enough fit with the content of the files that I think we can safely conclude that the text was analyzed as desired.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • But how do I store the file in a variable? – JanJansen Dec 21 '15 at 17:45
  • You can store the file *name* in a variable like `set file_name = %c` (I'm guessing you don't want to try to stuff the entire contents of a file into an environment variable--unless the file is tiny, that would be a really bad idea). – Jerry Coffin Dec 21 '15 at 17:48
  • I actually do want that, because the file is very tiny (it's a tweet) the files are called "tweet (x)" where x is the number of tweets saved. Any idea how I could loop through them and save the contents of the files in a variable to use further on in the script? – JanJansen Dec 21 '15 at 17:53
  • I don't know of a way to do that, at least without writing an actual program (e.g., in something like C or C++). You'd generally be better off writing something to process the file contents. – Jerry Coffin Dec 21 '15 at 17:55
  • Well I use set var=tweet.txt to select the file and set /p var=<%var% to load the content into a variable. I just need to put the correct number to the selected tweet. I'm so close haha. So set file_name = tweet (%c) would be perfect – JanJansen Dec 21 '15 at 17:59
  • It's a shame "for /l %c (1, 1, 100) ( set var=tweet (%c).txt" doesn't work – JanJansen Dec 21 '15 at 18:05
  • Why do you want the content of the file in an env variable? It's usually pretty easy to just process the file. – Jerry Coffin Dec 21 '15 at 18:09
  • I'm running a sentiment analysis using curl: curl -d "language=dutch&text=%var%" http://text-processing.com/api/sentiment/>uitkomst.txt – JanJansen Dec 21 '15 at 18:17
  • It uses the content of an exported tweet in a txt file (%var%) – JanJansen Dec 21 '15 at 18:17
  • @JanJansen: cURL supports putting the data into a file, then passing the file name, and concatenates pieces together correctly, so you can do something like `curl -d"language=dutch" -d@tweetN.txt ...` (where `tweetN.txt` is the name of the file containing the tweet you want to analyze. – Jerry Coffin Dec 21 '15 at 18:25
  • I just can't seem to figure out how this works. Thanks for your help though. – JanJansen Dec 21 '15 at 18:29
  • I just want to figure out how I can loop trough tweetN.txt where N goes from 1 to 100 – JanJansen Dec 21 '15 at 18:38