In a batch file, you can use the < operator to redirect input from a text file. Here's a simple example:
Assuming you have a batch file named process.bat, and you want it to accept input from a text file named input.txt. The content of input.txt might be something like this:
parameter1 parameter2 parameter3
Now, your process.bat file could look like this:
@echo off set /p param1=<input.txt set /p param2=<input.txt set /p param3=<input.txt echo Parameters from input.txt: echo Parameter 1: %param1% echo Parameter 2: %param2% echo Parameter 3: %param3%
In this example, set /p is used to read input from the text file. The batch file reads each line sequentially, assigning the values to variables (param1, param2, param3 in this case). Modify the script based on the structure of your input file.
To execute the batch file and use the input from input.txt, run the following command in the command prompt:
process.bat < input.txt
This way, the input from input.txt is redirected to the standard input of the batch file.
"Batch file read input from text file"
@echo off set /p variable=<input.txt echo Input from file: %variable%
"Batch file loop through lines in text file"
@echo off for /f "delims=" %%a in (input.txt) do ( echo Line: %%a )
"Batch file read specific line from text file"
@echo off set /a line_number=5 setlocal enabledelayedexpansion for /f "delims=" %%a in ('findstr /n "^" input.txt ^| findstr "^%line_number%:"') do ( set "line=%%a" set "line=!line:*:=!" echo Line %line_number%: !line! ) endlocal "Batch file read multiple values from text file"
@echo off setlocal enabledelayedexpansion for /f "tokens=1,2" %%a in (input.txt) do ( set "value1=%%a" set "value2=%%b" echo Values: !value1!, !value2! ) endlocal
"Batch file read and parse CSV from text file"
@echo off setlocal enabledelayedexpansion for /f "tokens=1,2,3 delims=," %%a in (input.csv) do ( set "column1=%%a" set "column2=%%b" set "column3=%%c" echo Columns: !column1!, !column2!, !column3! ) endlocal
"Batch file read lines until specific value"
@echo off set "search_value=END" setlocal enabledelayedexpansion for /f "delims=" %%a in (input.txt) do ( set "line=%%a" echo Line: !line! if /i "!line!"=="%search_value%" ( echo Found %search_value%! Exiting loop. exit /b ) ) endlocal
"Batch file read and process lines with specific condition"
@echo off setlocal enabledelayedexpansion for /f "delims=" %%a in (input.txt) do ( set "line=%%a" if "!line:~0,5!"=="Start" ( echo Processing: !line! rem Add your processing logic here ) ) endlocal
"Batch file read input file as command line arguments"
@echo off set /p arguments=<input.txt call your_script.bat %arguments%
"Batch file read and store entire file content"
@echo off setlocal enabledelayedexpansion set "content=" for /f "delims=" %%a in (input.txt) do ( set "line=%%a" set "content=!content!!line!" ) echo Entire Content: !content! endlocal
"Batch file read and replace text in file"
@echo off setlocal enabledelayedexpansion set "search=old_text" set "replace=new_text" set "output_file=output.txt" (for /f "delims=" %%a in (input.txt) do ( set "line=%%a" set "line=!line:%search%=%replace%!" echo !line! )) > %output_file% endlocal
file-handling html-encode csom zsh-completion int full-text-search database-administration asp.net-core-tag-helpers sql ihttphandler