The background: I'm using IFTTT to pull DC Metro bulletins from their RSS feed into a text file for my BBS. The biggest problem is that IFTTT cannot overwrite or append to files, so I get multiple copies of files in the directory:
red.txt
red (1).txt
red (2).txt
and so on.
I'm leaving the original (non-(x).txt
) version of the file as a place holder and concentrating on merging the other files throughout the day.
For one set of files this works:
if exist "red (1).txt" for %%f in ("red (*.txt") do type "%%f" >> red_line.txt & del "%%f"
Since there are multiple sets of files (each a different color or combination of colors), I'm trying to create a parent for
/do
loop to cycle through the different Metro lines (to make the code easier to maintain). I could always just add a new instance of the above for each of the Metro lines.
I think the parentheses in the file names are interfering with the parentheses in the loop, because it immediately breaks. There must be a way to escape the parentheses, but I've tried slashes and carets, etc.; nothing seems to work. I'm hoping there's a better way to do this.
This is the base of the code, without the myriad of things that I've tried already:
FOR %%A IN (red green orange red_orange) DO (
if exist "%%A (1).txt" for %%B in ("%%A (*.txt") do type "%%B" >> %%A_line.txt & del "%%B"
)
This is an example of what I get:
C:\sbbs\Dropbox\bbs\ifttt>(if exist "red (1).txt" for %B in ("red (*.txt") do type "%B" 1>>red_line.txt & del "%B" )
C:\sbbs\Dropbox\bbs\ifttt>(if exist "green (1).txt" for %B in ("green (*.txt") do type "%B" 1>>green_line.txt & del "%B" )
C:\sbbs\Dropbox\bbs\ifttt>(if exist "orange (1).txt" for %B in ("orange (*.txt") do type "%B" 1>>orange_line.txt & del "%B" )
C:\sbbs\Dropbox\bbs\ifttt>(if exist "yellow (1).txt" for %B in ("red_orange (*.txt") do type "%B" 1>>red_orange_line.txt & del "%B" )
This the part of the result that I'm seeing is borked:
do type "%B" 1>>
That "1" must be from the (1), right?
I also notice that my spaces are not carried over into the results:
do type "%B" 1>>orange_line.txt
bash
and most (if not all) other Unix shells, lets you say2>
,3>
, etc., to redirect file descriptors other than 1 (standard output). If you say>
or>>
in a batch file, and then run it with echo on, it will output as1>
or1>>
, just to show you what file descriptor it’s writing to. Also, if you putecho foo > foo.txt
in a batch file, it will display asecho foo 1>foo.txt
(the space after the>
disappears). So, neither of those symptoms are actual problems.