Batch, How To Read in a Blank Line of Text File - batch-file

i need to read in the first line and identify whether it is blank or string of number. If it is blank line, the program will tell this text is invalid then terminate otherwise i will use that string to do further calculation.
BUT, The code i used is below, it just can not store the blank line such that i can not use if-statement to check whether it is null or "". Any suggestion?
set "text_file=20150629_eleave_i_test.txt"
:: Get the first six characters of the first line
set /p first_six=<%text_file%
If the text file in the view of notepad++ is
1
2 00000720150625
Thank you for advance.

This should work:
set "text_file=20150629_eleave_i_test.txt"
set "first_line="
set /p first_line=< %text_file%
if not defined first_line (
echo This text is invalid
goto :EOF
)
set "first_six=%first_line:~0,6%"

#echo off
setlocal enableDelayedExpansion
set "ln="
set /p "ln=" <"20150629_eleave_i_test.txt"
if not defined ln (
echo First line is empty
) else (
for /f "delims=0123456789" %%A in ("%ln%") do echo First line is not fully numeric
) || echo First line is fully numeric

Related

Error when checking if a comma separated list is empty (blank)

I get a comma separated list with values of a field from database. This list is sometimes blank. I need to check if the list is blank, then do something. If the list is not blank, then take another action.
Problem is that I am getting an error while checking if the list is blank. Here is the code. Please note that I have simulated at the beginning of the code that values are coming from database.
Here is what I have tried earlier: If the list of VendorStores is empty (blank), take one action. Else take another action. I was getting an error when VendorStores had more than one value (comma separated) and it was being compared to blank. So now I am trying to count the values and use that count to make a decision. But I am getting same issue. When VendorStores is blank, the for loop still thinks it has 2 values - 1) ",=" 2) "". Not sure why this is the case.
:: Intention of the script is as follows:
:: If VendorStores list is blank, take one action
:: If VendorStores list has values, take another action
#echo off
:: Set values for VendorStores
:: set vendorstores=123,234,345
:: Set VendorStores to blank
set vendorstores=
echo list = "%vendorstores%"
:: Remove any blank spaces from VendorStores
set vendorstores=%vendorstores: =%
set /a c=0
echo c=%c%
SETLOCAL EnableDelayedExpansion
for %%a in ("%vendorstores:,=" "%") do (
if [%%a] NEQ [] (
set /a c=c + 1
echo VendorStore is %%a
)
echo c=!c!
)
echo c=!c!
if [!c!] EQU [0] (
echo c is equal to Zero
) else (
echo c is greater than Zero
)
echo c=!c!
endlocal
The wrong result is from the FOR command line:
for %%a in ("%vendorstores:,=" "%") do (
With inserting above this command line a line with echo on and after closing parenthesis of FOR a line with #echo off it can be easily seen why it fails.
The right command line would be:
for %%a in (%vendorstores%) do (
But using this FOR loop is not good in case of environment variable vendorstores is not defined at all.
Here is a simplified example for batch file with name Test.bat:
#echo off
set "VendorStores=%~1"
if not defined VendorStores goto EmptyList
set "VendorStores=%VendorStores: =%"
if not defined VendorStores goto EmptyList
if "%VendorStores:,=%" == "" goto EmptyList
echo List is: %VendorStores%
goto :EOF
:EmptyList
echo The list is empty.
This batch file can be executed from within a command prompt window as follows:
Test.bat
Test.bat ,,,,
Test.bat ", , , ,"
Test.bat " 123, 234, 345 "
Test.bat "376,983,305,253"
The output on those 5 batch file executions is 5 times the expected output.
:: Intention of the script is as follows:
:: If VendorStores list is blank, take one action
:: If VendorStores list has values, take another action
#echo off
:: Set values for VendorStores
:: set vendorstores=123,234,345
:: Set VendorStores to blank
set vendorstores=
echo list = "%vendorstores%"
:: Remove any blank spaces from VendorStores
set vendorstores=%vendorstores: =%
::::If there is any value in StoreCSV, then remove any comma delimiters from it as the "if" statement does not like commas
if defined vendorstores (
set "vendorstores=%vendorstores:,=%"
)
SETLOCAL EnableDelayedExpansion
if [%vendorstores%] EQU [] (
echo VendorStores is blank
) else (
echo VendorStores has one or more values
)
endlocal
Here's another option:
#Echo Off
Set "VendorStores="
For /F "EOL=, Delims=, " %%A In ("%~1") Do Set "VendorStores=%%A"
If Not Defined VendorStores (Echo VendorStores is blank
) Else Echo VendorStores has one or more values

BATCH - Remove "" and line diference in batch

I need a little help here,
How can i remove all the " from the first line without touching the second line and then join the two lines as one?
This is in a txt file and i need the change to be made in it.
line1->
curl "https://localhost:1234/wle//505?action=god&params="
line 2-> with more than 9000 characters
{"some text to join after params= that contains" and {}[]/,:"}
Hope someone can help.
Thanks
#echo off
setlocal EnableDelayedExpansion
< input.txt (
set /P "line="
< NUL set /P "=!line:"=!"
findstr "^"
) > output.txt
move /Y output.txt input.txt
edited to match comment 9000 characters, and cmd cant hold that many characters (simple: write first line without line feed, add second line):
(if there still are too many characters, jrepl.bat can help)
#echo off
setlocal enabledelayedexpansion
<c.txt (
set /p "line1="
set /p "line2="
)
<nul >c.txt set /p "=!line1:"=!"
>>c.txt echo %line2%
read the two lines into two variables, write both back to file, removing the quotes from the first one.
Note: Delayed expansion might/might not be needed for each of the variables (maybe separately), depending on present characters. (here the & in the first line needs delayed expansion).
another edit to match only the second line. Process the first line as above. For the second line, just use more +1. This will work regardless of the line length. Contra: you have to use a secondary file. Pro: no problems with special chars (in the second line).
#echo off
setlocal enabledelayedexpansion
<c.txt set /p "line1="
<nul >c.out set /p "=!line1:"=!"
more +1 c.txt >>c.out
move /y c.out c.txt

How to print multiple text files within a batch file

I have reached a little bit of a brick wall. I understand how to print a single text file within a batch file via Notepad but I am stuck when it comes to multiple text files. Here is what I have so far that really is not working correctly at all:
set /P PrinterFile= "Enter File Name:" | set PrinterFile2= "" | set PrinterFile3= "" | set PrinterFile4= "" | set PrinterFile5= ""
if %PrinterFile%== ""(
echo please enter valid file
pause
) else (
notepad.exe /P %PrinterFile%
)
pause
I did not finish the if statements to check for the others because I cannot get the first one to work. Can anyone point me in the correct direction? Thank you!
In the end it needs to be able to take from 0 to 5 text files. for example (name of batch file is printer.bat):
printer file1.txt file2.txt file3.txt
printfiles.bat
#echo off
setlocal
for %%f in (%*) do (
if exist "%%~f" (notepad.exe /P "%%~f"
) else (
if exist "%%~f.txt" (notepad.exe /P "%%~f.txt"
) else (
echo "%%~f" not found
)
)
)
[untested]
This should work. It simply processes the namelist provided as a parameter so
printfiles abc,def,ghi,yellow.txt
would print the files (abc or abc.txt) and (def or def.txt) and (ghi or ghi.txt) and (yellow.txt or yellow.txt.txt) if the files exist.
Does that solve your problem?
Full-blown version
#ECHO OFF
SETLOCAL
:: Get file list from command line
SET "filelist=%*"
IF DEFINED filelist GOTO printthem
CLS
ECHO Press ENTER to START printing
:nextfile
SET "filename="
SET /p "filename=Print : "
IF DEFINED filename SET "filelist=%filelist%,"%filename%""&goto nextfile
IF NOT DEFINED filelist ECHO no files specified&GOTO :EOF
:printthem
for %%f in (%filelist%) do (
if exist "%%~f" (notepad.exe /P "%%~f"
) else (
if exist "%%~f.txt" (notepad.exe /P "%%~f.txt"
) else (
echo "%%~f" not found
)
)
)
GOTO :EOF
First thing : load filelist from command-line, so executing printfiles abc,def,ghi,yellow.txt would assign abc,def,ghi,yellow.txt to filelist. If there are no supplied parameters, assign nothing to filelist.
If filelist is defined (ie, contains a value which implies command-line parameters were supplied) then go to the label printthem
Otherwise, clear the screen and how the message Press ENTER to START printing
Then set filename to nothing (because set /p with simply Enter will leave the value unchanged) and prompt for input with Print :. Keyboard input will be placed into filename.
if there was a (presumed, filename) entry made, then accumulate "the filename" to the end of filelist after a comma and repeat the request for a filename.
When simply Enter is used, filename will be empty, hence undefined so we proceed to :printthem provided filelist is not empty.
From there, we simply process filelist as a comma-separated series of
filenames into %%f. If the filename as-entered exists, use notepad to print it, if it doesn't, try appending .txt to the name and if even that fails, show an error message.
The %%~f removes the quotes from the item in %%f. This is then re-quoted so that filenames containing spaces may be printed.

Batch File - Find two lines then copy everything between those lines

I need to parse a text file.
I want to find the firstline in the text file
: the first line to find
set firstLine=------------------------------------------------------------------------------------------------------------------
and find the last line
:: the last line to find
set lastLine=*******************************************************************************************************************
Then I need to export to a new file everything between those two line.
echo >> M:\TESTING\Output.txt
I'm a beginner with this and I've searched for days, but am not finding how to do this.
I looked at for loops and if statements, but I'm still puzzled.
for /f "tokens=1 delims= " %%f in (M:\TESTING\*.txt) do (
:: sets then the line variable to the line just read
set line=%%f
:: the first line to find
set firstLine=------------------------------------------------------------------------------------------------------------------
:: the last line to find
set lastLine=*******************************************************************************************************************
Then if %line% = %fistLine% start the export.....
Any direction will be appreciated. thanks.
#DennisvanGils' approach is a good start and will do well in many cases.
However, it will not produce an exact copy of the text file content between the given lines:
leading whitespaces (SPACE and TAB) will be removed (due to tokens=* option),
lines starting with ; will be skipped (due to the default option eol=; of for /F), and
empty lines will be skipped as well (as for /F always skips such).
To get an exact copy of the text file portion, you could use the following code snippet:
#echo off
set "INFILE=M:\TESTING\input.txt"
set "OUTFILE=M:\TESTING\output.txt"
set "FIRSTLINE=------------------------------------------------------------------------------------------------------------------"
set "LASTLINE=*******************************************************************************************************************"
setlocal EnableExtensions DisableDelayedExpansion
set "FLAG="
> "%OUTFILE%" (
for /F "delims=" %%L in ('findstr /N "^" "%INFILE%"') do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "LINE=!LINE:*:=!"
if "!LINE!"=="%FIRSTLINE%" (
endlocal
set "FLAG=TRUE"
) else if "!LINE!"=="%LASTLINE%" (
endlocal
goto :CONTINUE
) else if defined FLAG (
echo(!LINE!
endlocal
) else (
endlocal
)
)
)
:CONTINUE
endlocal
Core function here is findstr, which is configured so that every line in the file is returned, prefixed with a line number and a colon :. Its output is then parsed by a for /F loop. Because of the prefix, no line appears to be empty and therefore every one is iterated by the loop. In the body of the loop, the prefix is removed by the set "LINE=!LINE:*:=!" command for each line.
The variable FLAG is used to decide whether or not the current line is to be output; if it is defined, that is, a value is assigned, the command echo !LINE! is executed; otherwise it is not. FLAG is set if the current line matches the string in %FIRSTLINE%; if the line matches the string in %LASTLINE%, a goto command is executed which breaks the loop. This means also that only the first block between %FIRSTLINE% and %LASTLINE% matches is output.
If there might occur multiple %FIRSTLINE% and %LASTLINE% matches within the text file and you want to output every block, replace the goto command line by set "FLAG=".
Note that this approach does not check whether %FIRSTLINE% occurs before %LASTLINE%, nor does it even check for existence of %LASTLINE% (all remaining lines to the end of file are output in case). If all this is important, the logic need to be improved and even a second loop will be required most likely.
What you should do in this case is use a variable like a boolean to know if you encountered the startline and endline yet, and to know if you have to output the lines.
Also, you should use setlocal ENABLEDELAYEDEXPANSION with ! instead of % so you can change variables in loops and ifs (for more information about that, see this. The usage of () after if is not needed in this case, since the if is on one line, but they make things easier to read in my opinion. If you want to output the start and endline too, switch the checks for the start and endlines.
#echo off & setlocal ENABLEDELAYEDEXPANSION
set start=0
:: the first line to find
set firstLine=------------------------------------------------------------------------------------------------------------------
:: the last line to find
set lastLine=*******************************************************************************************************************
for /F "tokens=*" %%A in (TEST.txt) do (
:: sets then the line variable to the line just read
set line=%%A
if "!line!"=="!lastLine!" (set start=0)
if !start! equ 1 (echo !line!>>TESTOUTPUT.txt)
if "!line!"=="!firstLine!" (set start=1)
)
This should do what you want. Note that when you encounter a startline a second time it starts reading again.

How to extract first letter from each line from a file using batch?

This is my code so far:
FOR /f %%G IN (temp.file) DO (
echo %%G > temp
first_char.exe temp > letter
set /p myLetter =<letter
echo %myLetter%
)
first_char.exe prints to the stdout the first char from the input file.
It prints:
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
(because I have 6 lines of text in temp.file).
I can't see why you need a first_char exe file to extract a single char, a simple var:~0,1 should also work.
Your ECHO is off. problem is a problem of the expansion time of %myLetter%.
It's expanded when the complete parenthesis block is parsed, at this time myLetter% is empty, therefor you got only empty lines.
setlocal EnableDelayedExpansion
FOR /f %%G IN (temp.file) DO (
set "var=%%G"
set "myLetter=!var:~0,1!"
echo !myLetter!
)

Resources