Batch command to take only first line from input - batch-file

I'm looking for a DOS batch program that takes a file:
First input line
Second input line
Third input line...
And outputs "First input line"

you can just get the first line like this
set /p firstline=<file
echo %firstline%

Assuming you mean the Windows cmd interpreter (I'd be surprised if you really were still using DOS), the following script will do what you want:
#echo off
setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (infile.txt) do (
if !first!==1 echo %%i
set first=0
)
endlocal
With an input file of infile.txt as:
line 1
line 2
line 3
this will output:
line 1
This will still process all the lines, it just won't print those beyond line 1. If you want to actually stop processing, use something like:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in (infile.txt) do (
echo %%i
goto :endfor
)
:endfor
endlocal
Or you could just go get your hands on Cygwin or GnuWin32 and use the head program. That's what I'd do. But, if that's not an option (some workplaces don't allow it), you can create a similar cmd file in Windows itself as follows (winhead.cmd):
#echo off
setlocal enableextensions enabledelayedexpansion
if x%1x==xx goto :usage
if x%2x==xx goto :usage
set /a "linenum = 0"
for /f "usebackq delims=" %%i in (%1) do (
if !linenum! geq %2 goto :break1
echo %%i
set /a "linenum = linenum + 1"
)
:break1
endlocal
goto :finish
:usage
echo.winhead ^<file^> ^<numlines^>
echo. ^<file^>
echo. is the file to process
echo. (surround with double quotes if it contains spaces).
echo. ^<numlines^>
echo. is the number of lines to print from file start.
goto :finish
:finish
endlocal

why not use the more +1 command via a pipe?
e.g.
type something | more +1

Related

Batch file Cout math examples from txt file

Hello I have txt file with math examples:
12;+;56
893;+;354
756;-;231
987;-;884
14;*;15
45;*;33
1024;/;10
120;/;30
12345;+;5667
15747;-2344
And I have batch file for counting. I want to display examples + resul in batch file
I have this code, but it return result 0.
#echo off
cls
echo Examples:
echo.
set result=0
for /f "eol=# delims=; tokens=1,2,3" %%A in (examples.txt) do (
set result=%%A%%B%%C
echo %%A%%B%%C = %result%
)
pause
You need to use set /a to do arithmetic calculations. You'll also need delayedepxansion and let's use usebackq in order to use double quotes if the input file is in a path containing spaces.
#echo off & set result=0
setlocal enabledelayedexpansion & cls
echo Examples:
echo(
for /f "usebackq tokens=1-3*delims=;" %%A in ("examples.txt") do (
set /a result=%%A%%B%%C
echo %%A%%B%%C = !result!
)
pause
Check you examples.txt file as well as you forgot a semicolon on the last example.

Bat read text obtain variable

I found help on this topic. User: Hackoo had a great piece of code. I ran it on a text file, and it created 5 different variables as expected. It worked perfect. I would like to know how to prompt the user to select which variable they would like to keep. Then take there input and place it into a separate text file. Is that possible? Here is the code which works to obtain the variables.
#echo off
set "File2Read=file.txt"
If Not Exist "%File2Read%" (Goto :Error)
rem This will read a file into an array of variables and populate it
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%File2Read%"') do (
set /a count+=1
set "Line[!count!]=%%a"
)
rem Display array elements
For /L %%i in (1,1,%Count%) do (
echo "Var%%i" is assigned to ==^> "!Line[%%i]!"
)
pause>nul
Exit
::***************************************************
:Error
cls & Color 4C
echo(
echo The file "%File2Read%" dos not exist !
Pause>nul
exit /b
::***************************************************

Batch file to count files matching first 6 characters in file name and output file group and its count

I would like to have a batch file to count all file names with common prefix and output the file group and its count. I have these files in a directory:
A1110601.zip
A1110602.zip
A1110603.zip
A1120601.zip
A1120602.zip
I want to group the first 3 by A11106*.zip and the last two by A11206*.zip.
My desired output is:
A11106: 3
A11206: 2
I have tried to copy sample codes from the forum, but they don't fulfilled my desired output.
Here is the code I have so far. But the output is not as described above.
#echo off
title Store Data Counter
:recurse
set I=1
echo "files counter"
FOR /f "tokens=*" %%A IN ('dir /a-d /b "Z:\StoreData\A11106*.zip"') do (call :showfiles "%%A")
echo A111: %I%
FOR /f "tokens=1" %%A IN ('dir /a-d /b "Z:\StoreData\A11206*.zip"') do (call :showfiles "%%A")
echo A112: %I%
pause
goto :eof
:showfiles
echo %1
set /a I+=1
goto :eof
The following batch script should do what you want -- let us call it mask-count.bat:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
:LOOP
set "MASK=%~1"
if defined MASK (
call :SUB "%~1"
shift /1
goto :LOOP
)
endlocal
exit /B
:SUB
set "ARG=%~1"
setlocal EnableDelayedExpansion
set "NUMF=0"
for /F "skip=4 tokens=1" %%L in ('
2^> nul dir /A:-D /N /-C "!ARG!"
') do (
set "NUMF=!NUMD!"
set "NUMD=%%L"
)
echo(!ARG!: %NUMF%
endlocal
exit /B
To use this script, provide the applicable file masks as command line arguments; for instance:
mask-count.bat "A11106*.zip" "A11206*.zip"
This would lead to the following output when applied to your directory:
A11106*.zip: 3
A11206*.zip: 2
Here is a batch code for this task.
#echo off
setlocal EnableExtensions
for %%I in ("*.zip") do call :CountFile "%%~nI"
for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J
endlocal
goto :EOF
:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~0,6%"
if "Group#%FileGroup%" == "" (
set "Group#%FileGroup%=1"
) else (
set /A Group#%FileGroup%+=1
)
goto :EOF
"*.zip" can be extended with full path to the ZIP files to count.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
set /?
setlocal /?
And read also the Microsoft article about Using command redirection operators for an explanation of 2>nul used here to redirect the error message output by command SET from STDERR to device NUL to suppress it in case of no *.zip file could be found at all.
The redirection operator > must be escaped here with ^ to be interpreted on executing command SET and not on parsing the FOR command line. Otherwise 2>nul without ^ would be interpreted as redirection for command FOR defined at an invalid position in the command line which would result in a batch execution exit because of a syntax error.

Use of relational operators when reading a file from CMD

I am trying to create a command line program which outputs numbers from a file only greater than (larger than) specified one.
Say like there's a file output.txt with lots of numbers (one per line) and I need to get the only ones less than 5000.
Here's a part of my code, but it doesn't work as expected:
CHOICE /C 12
SET /P Comparative_number="Input a number: "
IF %ErrorLevel%==2 GOTO LESS_OPERATION
FOR /F %%A IN (%OutputFile%) DO IF %%A GTR %Comparative_number% ECHO %%A
ECHO. & ECHO End of output & EXIT /B
:LESS_OPERATION
FOR /F %%A IN (%OutputFile%) DO IF %%A LSS %Comparative_number% ECHO %%A
ECHO. & ECHO End of output & EXIT /B
What I am doing wrong?
The command extensions are enabled by default. But if a batch script depends on command extensions as when using GTR and LSS in an IF condition is always advisable to enable them explicitly.
The integer numbers must be listed in the file referenced via environment variable OutputFile line by line, for example:
50
23
478
-3425
9071
A batch code which worked:
#ECHO OFF
SETLOCAL EnableExtensions
SET "OutputFile=C:\Temp\File With Numbers Listed Line By Line.txt"
ECHO.
ECHO Please choose the comparison:
ECHO.
ECHO 1 ... Greater Than
ECHO 2 ... Smaller Than
ECHO.
CHOICE /C:12 /N /M "Your choice: "
ECHO.
SET "Comparative_number=0"
SET /P "Comparative_number=Input a number: "
ECHO.
IF %ErrorLevel%==2 GOTO LESS_OPERATION
FOR /F "usebackq" %%A IN ("%OutputFile%") DO IF %%A GTR %Comparative_number% ECHO %%A
ECHO.
ECHO End of output
ENDLOCAL
EXIT /B
:LESS_OPERATION
FOR /F "usebackq" %%A IN ("%OutputFile%") DO IF %%A LSS %Comparative_number% ECHO %%A
ECHO.
ECHO End of output
ENDLOCAL
EXIT /B
Why your code does not work in comparison to this slightly modified code depends most likely on name of output file, how the numbers are listed in output file and how you have used command CHOICE.

set /p is not working as advertised

I've got a problem with redirecting input from a file into set/p
Here is my script:
#echo off
echo Reading: {%1}
type %1
echo(
echo Starting...
set VAR=
set /P VAR=<%1
echo VAR is {%VAR%}...
I've read elsewhere (https://stackoverflow.com/a/7827243) that the syntax I am using will work. It does not!
Here is my output:
Reading: {Fruit.txt}
Pears
Apples
Oranges
Kiwi
Grapes
Kumquat
Starting...
VAR is { ■P}...
So - What gives?
Your file is in Unicode (UTF16) format, and SET /P does not work with Unicode. The TYPE command does work with Unicode, and it converts the output to ANSI. You can redirect the output of TYPE to a new text file, and then your SET /P will work.
#echo off
type %1>new.txt
set VAR=
set /P VAR=<new.txt
echo VAR is {%VAR%}...
EDIT
To get the second line instead of the first:
#echo off
type %1>new.txt
<new.txt (
set /P VAR=
set VAR=
set /P VAR=
)
echo VAR is {%VAR%}...
If the purpose is to read lines of text from a file, why do you need the set /p command?
for /f "delims=" %%a in ('type "file name.txt"') do echo %%a
If you type the command at the cmd.exe command line, you would write %a (one % symbol) rather than %%a (i.e., double the % symbol when using in a shell script [batch file]).
Bill
Here's another technique for reading only the first line of a file using for /f and type that doesn't rely on set /p:
#echo off
setlocal enableextensions enabledelayedexpansion
set VAR=
set CURRLINE=0
for /f "delims=" %%a in ('type "test.txt"') do (
set /a CURRLINE+=1
if !CURRLINE! EQU 1 (
set VAR=%%a
goto :DONE
)
)
:DONE
echo %VAR%
endlocal
The advantage here is there's no need to write out a separate file.
However, note that either of these approaches (set /p or for /f with type) will have problems with special shell characters in the input file (<, >, |, etc.). To get around this problem, one could use a small utility I wrote a while back called shellesc.exe (http://www.westmesatech.com/sst.html) to "escape" the special characters. But if you use these tools, then you can also use linex.exe to pick the line you want and get the result with a little bit less code:
#echo off
setlocal enableextensions
set VAR=
for /f "delims=" %%a in ('type "test.txt" ^| linex -l 1 ^| shellesc') do set VAR=%%a
echo %VAR%
endlocal
Note that this approach has the additional advantage of not "choking" on special shell characters (shellesc).
HTH,
Bill

Resources