For Loop through text file only outputs last line - batch-file

I think I am about to have an a brain aneurysm. I am trying to print the lines of a text file to a new file, but it only outputs the last line. I have done this a dozen times, now I am not sure what I am doing wrong.
for /F "tokens=*" %%A in (results.txt) do (
echo %%A
) > imsofrustrated.txt
PAUSE
Am I literally retarded? This is in reference to a previous question I posted.
FINDSTR - Stop on last string match

> is to redirect output and overwrite whatever is in the file.
>> is to redirect output and append to the file.
for /F "tokens=*" %%A in (results.txt) do (
echo %%A
) >> imsofrustrated.txt

Related

First character disappearing from command reading [Batch file]

I'm trying to read the output of a command (which outputs into multiple lines), and use an arbitrary number of those lines. Because I know neither the number of total lines, nor the number of lines that will be used, I need to analyse and possibly use each line in a loop, which is why I have setlocal enabledelayedexpansion.
Below is a snippet of the code that shows the process of taking the command and reading each line (not using it yet, just reading it to make sure this works (which it doesn't)):
#echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('svn status') do (
echo %%i
set file=%%i
echo *!file!
)
The problem that I'm running into is that the %%i values that are being read in are not correct in the for line. The first character is missing from the first line of the input (which is important because I use the first line to decide whether or not to use that line).
The output I get from my code looks like this:
Dir0\TestDoc7.txt
? StatusFile.txt
Whereas if I run this code:
copy /y NUL StatusFile.txt >NUL
>StatusFile.txt (
svn status
)
(Which is just another way of me seeing what the real output of svn status is) I get a proper output into the text file:
! Dir0\TestDoc7.txt
? StatusFile.txt
I'm probably making a fairly clear mistake as I'm rather new to batch scripting.
Thanks in advance.
The cause is EnableDelayedExpansion which will eat the exclamation marks,
Your choice of tokens=* will also strip all leading spaces from the lines.
#echo off
for /f "tokens=1*" %%A in ('svn status') do (
if "%%A" equ "!" (
Rem do whatever
) else If "%%A" equ "?" (
Rem do something else rest of the line is in %%B
) else (
Rem no ! or ? first space sep. content is in %%A rest of the line is in %%B
)
)

Batch Script: Merge Content From .txt List to one file

On Windows 10 I have this .bat:
#echo off
for /f "delims=" %%i in (filelist.txt) do (
echo %%~nxi >> output.txt
type "%%~ni*" >> output.txt
echo. >> output.txt
echo. >> output.txt
)
Exit
Now what this does is:
reads filelist.txt, which contains names of .txt files like:
20180808173105 (without ".txt"
searches for those files: 20180808173105.txt
copies name of files (without ".txt") into output.txt
inserts content of files
inserts two blank lines
repeats whole process for all files named in filelist.txt
--> It works fine! (or do you see any exception where this might malfunction?)
This inserts the full contents of a text file according to a list.
Can I modify it, so
not the whole content of a .txt, but only a part of it is inserted?
For example, everything from just after "title:" to just before "<!--"
if the filelist had a hierarchical structure (outline), it could
be preserved, like so:
#201508081213
###201609101219
to
#201508081213
TEXT
###201609101219
TEXT
I am using this to convert Outlines (using only the file names) to a rough first draft of text for writing articles and blogs
#echo off
2> output.txt echo.
#>&3 (
echo Debug Information
echo -----------------
)
for /f "delims=" %%A in (filelist.txt) do (
for %%B in ("%%~nA*") do call :read "%%~B"
) >> output.txt
exit /b
:read
setlocal
set "line="
echo(%~nx1
for /f "usebackq delims=" %%C in ("%~1") do (
set "line=%%C"
for /f "tokens=*" %%D in ('call echo "%%line:~0,4%%"') do (
#>&3 echo File: "%~1" Test: %%D == "<!--"
if %%D == "<!--" (
#>&3 echo Found: "<!--"
echo.
echo.
exit /b 0
)
)
call echo(%%line%%
)
echo.
echo.
exit /b 0
Note
Not the best language for this task. Had to avoid
enabledelayedexpansion as to known use of ! in <!--.
Used call even though < and some other characters
could cause issue.
for /f loops do not normally process empty lines so
hope that is not a problem.
Hierarchical structure depends on the order in filelist.txt.
The structure of a document can vary and I cannot consider
what the correct order might be. The use of a wildcard gives
some doubt. A filename #a will find #a1 and #a2 so
placement of 3 headings is unknown.
I have left in the std stream 3 messages for your
understanding of operation with the <!-- test.
Std stream 1 is the output that goes to file.
Operation
The file output.txt is erased before echoing text to the file.
The 1st for loop reads each line of the filelist.txt file.
The nested for loop gets the filenames with a wildcard.
Each call to label :read passes the argument of a filename.
In the label of :read, the filename is echoed.
A for loop reads lines from the filename.
Each line is stored in variable named line.
The nested for loop will use call echo to expand
the variable line and echoes the 1st 4 characters.
A comparison is done to test if it is <!-- and if so,
echo 2 newlines and then the label is exited.
If not, echo each line. echo 2 newlines is done at
the end of the label before exiting.

Combine lines in text file using batch

I want to make a program that takes the content of the second line of a text file and puts it on the first. (It doesn't matter if the second doesn't get edited)
for /f "tokens=1" %%t in (file.txt) do set string1=%%t
for /f "tokens=2" %%t in (file.txt) do set string2=%%t
echo %string1%%string2%>file.txt
I have two issues hat I can't seem to be able to fix.
One: the loops only include the first word of each line in the variables.
Two: Echo doesn't replace the first line of the file with the variables given and instead writes ECHO command deactivated (I have the French version of Windows 10 and simply translated what got written in the file, the text in English Windows version might be slightly different, but you get the idea)
If you have any suggestions, I would appreciate if you explain what the code you provide does (I always like to learn)
Your question is not clear and can be understood in several different ways. Anyway, this management is simpler with no for command:
#echo off
setlocal EnableDelayedExpansion
< file.txt (
rem Takes the content of the first line
set /P "line1="
rem Takes the content of the second line and puts it on the first
set /P "line2="
echo !line1!!line2!
rem It doesn't matter if the second line doesn't get edited
echo !line2!
rem Copy the rest of lines
findstr "^"
) > output.txt
move /Y output.txt file.txt
The FOR command uses a space as a delimiter by default. So you have to tell it to not use any delimiters with the DELIMS option. Also, you should be able to do this with a single FOR /F command. Just hold the previous line in a variable.
#ECHO OFF
setlocal enabledelayedexpansion
set "line1="
(for /f "delims=" %%G in (file.txt) do (
IF NOT DEFINED line1 (
set "line1=%%G"
) else (
echo !line1!%%G
set "line1="
)
)
REM If there are an odd amount of lines, line1 will still be defined.
IF DEFINED line1 echo !line1!
)>File2.txt
EDIT: I think I completely misunderstood your question. Once you clarify your question I will repost a code solution if needed.
Use skip to omit the first line and write the 2nd line twice. In general an edit of a file implies a rewrite to a new file and possibly a rename to retain the old file name.
:: Q:\Test\2018\07\25\SO_51508268.cmd
#Echo off
Set "flag="
( for /f "usebackq skip=1 delims=" %%A in ("file1.txt") Do (
If not defined flag (
Echo=%%A
Set flag=true
)
Echo=%%A
)
) >file2.txt
Del file1.txt
Ren file2.txt file1.txt
After running the batch a file1.txt with initially numbered lines 1..5 looks like this:
> type file1.txt
2
2
3
4
5

Windows 8.1's interpreter does not accept FOR input file

I am relearning DOS/batch & seems different.
Script should create a text file (it does, verified) then read it line by line looking for a beginning word and discarding lines thru that line as it builds TMP.txt.
It should then create a file of remaining lines up to and including a finishing word and ignore the remainder.
Then it will look for a line containing a specific word.
Problem: I get ("inFile.txt") was unexpected. The reams of text I have pulled down into my reference directory has not been helpful as it appears (does't it always) correct from what I expect.
Here is the script to and a bit after that point:
REM Append a final line to input text
echo IDSS >> inFile.txt
REM Bookend the node's text with BEGwd and FINwd.
REM Read inFile.txt file- Ignore %%A if BEGwd not found,
REM else write remaining lines to TMP.
for /F "tokens=*" %%A ("inFile.txt") DO ( <<---- Problem line
if "*%BEGwd%*" == "*%%A*" echo "%%A" >> TMP.txt
)
REM Read TMP look for FINwd- Write non-%%B lines to TMP2
REM until FINwd
for /F "tokens=*" %%B ("TMP.txt") DO (
if "*%FINwd%*" == "*%%B*" goto SEARCH
echo "%%B" >> TMP2.txt
)
Am also unsure if batch will accept my IF command compare with *'s?
Appreciating any assistance there too.
for /F "tokens=*" %%A ("inFile.txt") DO ( <<---- Problem line
Correct syntax is for /F "USEBACKQtokens=*" %%AIN("inFile.txt") DO (
the usebackq option is required because you have "quoted your filename".

Batch Script File Output and Input

I want to get the last modified date of every file in a directory in windows, output that to a text file, and then read each line of that text file as input.
Below are the contents of my .bat file.
#echo off
WMIC DATAFILE WHERE "PATH='\\Users\\n63206\\Desktop\\Project\\'" GET Name, LastModified > out.txt
for /f %%a in (out.txt) do (
echo %%a
)
echo test
After I run the script, only "test" gets printed out. When I open out.txt, I see the last modified dates that I output to it displayed fine, and if I create a new text file (cannot use out.txt) and manually enter some lines in there, my for loop can print out all the lines in that file. Any advice to help me figure out the issue here is greatly appreciated!
The problem is that the generated file is a UTF-16, little endian, BOM prefixed file. That is, it contains two bytes per character and is prefixed by two characters, 0xFF 0xFE.
for /f is not able to read this file.
Your options are:
1 - Read the information as it is generated
for /f "delims=" %%a in (
' wmic datafile where "path='\\Users\\n63206\\Desktop\\Project\\'" get name^,lastmodified '
) do echo %%a
2 - Once the file is generated, convert the file while reading it
wmic datafile where "path='\\Users\\n63206\\Desktop\\Project\\'" get name,lastmodified > out.txt
for /f "delims=" %%a in ('type out.txt') do echo %%a
3 - Convert the file while generating it
wmic datafile where "path='\\Users\\n63206\\Desktop\\Project\\'" get name,lastmodified | find /v "" > out.txt
for /f "delims=" %%a in (out.txt) do echo %%a

Resources