- 1 year ago
How to make a Batch file store more than one values in a file
Comment
1 / 1
To store multiple values in a file using a batch file, you can use a loop to write each value to the file on separate lines. Here's a simple example:
Create a new batch file (e.g.,
store_values.bat
).Use a loop to write values to the file. You can provide the values as command line arguments to the batch file. The following example demonstrates how to do this:
batch@echo off
REM Check if any values are provided as arguments
IF "%~1" == "" (
echo No values provided. Usage: store_values.bat value1 value2 value3 ...
exit /b 1
)
REM Loop through the provided values and append each value to the file
FOR %%A IN (%*) DO (
echo %%A>>values.txt
echo Stored value: %%A
)
echo All values stored in values.txt
In this example:
- The batch file takes multiple values as command line arguments (e.g.,
store_values.bat value1 value2 value3
). - The
FOR
loop iterates through each provided value. - The
echo %%A>>values.txt
command appends the value to a file namedvalues.txt
. - The
echo Stored value: %%A
command displays each stored value. - The final message indicates that all values have been stored.
After you've created the batch file, you can run it from the command prompt with the desired values:
batchstore_values.bat apple banana orange
This will create a values.txt
file containing the following:
apple
banana
orange
You can modify the batch file as needed to suit your requirements.