0

This is my first stop so please point out any errors I have made into my post or way of resolving this problem! So, I have a small project with my friends in which we have to make a batch script in which it has to access a text file with a few words on the first lines. Selecting one word at the time, we have to access a site and find it a synonym and paste the result. Here is what I have done so far:

@echo off
title Prg1
set COS=
:b
for /f "Delims=%COS%" %%a in (word.txt) do (
 set SIN=%%a
 set COS=%SIN%
)
cd C:\
cd Folder
* finds synonim 
:Ask
echo Change the word " %SIN% " with the above word? (Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If /I "%INPUT%"=="y" goto yes 
If /I "%INPUT%"=="n" goto no
:yes
*send synonym* >> sinonim.txt 
echo       The transfer was succesful!
pause
goto :b

OK so I am sorry that I wasn't able to put the code in a box just like the other posts, I wasn't able to figure it out. Alright then, I have 2 errors:
1) At the beginning of the program I gave COS the value of space so when it would enter the for loop the delimiter would be the space too. I then asigned SIN the value of the first word in the text file. Right after that I gave COS the same value as SIN so after all the other commands would be executed, goto :b would return me to the for loop and I would have a new delimiter: the first word in the text thus SIN will then be set as the second word and the loop begins again. My problem is that even though COS got the SIN value, when it accesses the loop it one again has the space value so the script has no choice but do the same thing over and over again. What should I do?
2) When the command send synonym >> sinonim.txt executes it places the new word on a new line. Now, I have researched this for quite a while today but I couldn't solve it since the code is something like this : (grep "blabla" blah.html) >> sinonim.txt. I tried to write it as (echo|set /p=(grep "blabla" blah.html)) >> sinonim.txt but it didn't work. Again, what should I do?

I would really appreciate if you could answer both of my question StackOverflow Community! Have a great day!

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • a) you need [delayed expansion](http://stackoverflow.com/a/30284028/2152082). b) set delims to a word? No. A delimiter is a single char only (or a set of chars: `delims=Tran` defines each of the letters `T`. `r`, `a` and `n` to be a delimter. For the rest: not sure, what you want to accomplish. Could you give an example for your input file and the desired outputfile? – Stephan May 16 '16 at 05:38
  • Hi! Thanks for answering! Well my intent is to have this bash script and a text file filled with words. When I enter the bash script it would select the first word and find its synonym on the web. Then it would ask the user to if the script should add the synonym to a new text file. Then it would go back up again and have the first word as the delimiter ( which now I found out it wasn't possible) thus the script would have no other choice but choose the other words. That's pretty much it, have a text file, find their synonyms and transfer them to a new text file – Tran Stephen May 16 '16 at 07:31
  • Also I dind't understand the delayed expansion. Here it is the line of code that needs to be added everytime in the new text file without going on a new line : (grep "searchsynonym(event)" sino.html | cut -d ")" -f 5 | cut -d "," -f 1 | cut -d ">" -f 4 ) >> sinonim.txt Basically I want it to be added next to the word that was previously added with the same line of code.( ex: synonyma synonymb ) and for it to go to a new line only if there is a line between the former 2 words. ( Not the synonyms!!) – Tran Stephen May 16 '16 at 07:36
  • If you want to change `delims` during execution of the `for /F` loop, this does not work; also, the `for /F` options cannot be set by a delayed expanded variable unfortunately... – aschipfl May 16 '16 at 11:36

1 Answers1

0

Your logic is bad. You can't process a file word by word. Instead you have to process it line by line and with another for process each line word by word.
By the way: your for loop ends before anything gets done, resulting in %SIN% being the last line of your file only.

Pseudo-Code:

for every line in word.txt do
  for every word in that line do
    (get the synonyme)
    write the synonyme (without linefeed)
  next word
  write a linefeed
next line

Finally redirect the whole output to the new file.

Your code should look something like:

@echo off
setlocal enabledelayedexpansion
(
  for /f "delims=" %%a in (word.txt) do (
    for %%b in (%%a) do (
      rem replace next line with some code to get the synonyme of %%b
       set "syn=[%%b]"
       <nul set /p "line=!syn! "
    )
    echo/
  )
)>out.txt

Note: this is just a "how-to" - it ignores empty lines and some punctuation (because they are treated as standard delimiters)

Stephan
  • 53,940
  • 10
  • 58
  • 91