I am trying to make a batch file that will write other files.
This is my file:
#echo off
set x=100
echo %x% >> output.txt
What this does is create a text file called "output.txt".
In this file it will display "100"
This is not what I want it to, I want the file to contain "%x%" literally.
The reason I am trying to do this is because I would like a batch file to create other complex batch files.
I could use copy or move to move another file containing what I want, but I need this to be a single file.
I would really appreciate some help, but it is understandably really difficult to understand.
Thanks Jason
Inside a batch file, double up your percent signs:
#echo off
set x=100
echo %%x%% >> output.txt
Just set x after you create the file:
#echo off
echo %x% >> output.txt
set x=100
This should work because you don't need to set x that early in the program (at least under those circumstances you don't).
Related
So I plan to make a little file downloader in batch and I want to display something like the latest version number. I would like to create a .txt file with something like v1.2 etc and then when you run the batch script, it uses the "echo" command and echoes exactly what it says in the text file. I've tried googling this but no luck. Thanks in advanced!
You can put this in your batch file:
#echo off
pushd "%~dp0"
type "version.txt"
This will echo out the contents of version.txt, which is on the same direction of the normal Batch file.
If you want to go more complex with a save feature telling the program what happened and stuff, do this to save:
(
echo %Version%
echo %log%
) > Save.prgmsav
That saves the version and log variable, this is how you load them:
< Save.prgmsav (
set /p Version=
set /p log=
)
The .prgmsav can be .txt too. The save name can be a variable, as well.
I'm trying to create and write to a file using .bat
#echo off
echo Jackdows loves my big sphinx >> %appdata%\data.html
echo Of quartz. >> %appdata%\data.html
exit
Works properly. However, if user runs it again, it writes the same values to the file again. So in the files, there are multiple values. Is it possible to prevent this ?
>> appends text to a file regardless of whether it exists elsewhere in the file or not. You can, however, search the file first for the string and then only append the line if it does not already exist.
#echo off
:: Appends a string to a file only if that string is not present in that file
call :ainp "Jackdaws love my big sphinx" text.txt
call :ainp "Of quartz" text.txt
call :ainp "Rule Brittania" text.txt
exit /b
:: Append If Not Present
:AINP
set "search_string=%~1"
set "search_file=%~2"
>nul find "%search_string%" %search_file%
if %errorlevel% equ 1 (
>>%search_file% echo %search_string%
)
I want to use a .bat file to read lines from .txt file then use them as commands. The text file is updated everyday so the size or the number of lines is unknown. I seem to be stuck on it. I'm completely new to batch scripting. So any kind of help is appreciated.
You can iterate over the lines of a file with
for /f "delims=" %%L in (foo.txt) do ...
To use whatever is in those lines as commands, just execute it:
for /f "delims=" %%L in (foo.txt) do %%L
A simpler way might be to just rename the file to a batch file and run it:
ren foo.txt foo.cmd
call foo.cmd
The simplest way is this:
cmd < foo.txt
This method also allows you to include lines of input data for commands in the lines following the commands. For example, try previous line with this foo.txt file:
echo Read the value given in next line
set /P var=
This is the value
echo The value read is: %var%
This feature may ve very useful in certain cases.
Could anyone assist, i crate a batch script that will create bunch of txt files from the list i have, the txt then needs to contain a few lines on text and parts of it will need to have the name of a file.
So eg.
mkdir file1.txt
then the .txt file needs to have:
this file contains info
owner of the file is 'file.txt'
data for this file is in C:\Users\'file.txt'
Please let me know should this not be clear enough
Thanks
(copy/formatted from OP's comment - OP: Use the EDIT link to edit your question (judiciously, of course))
so far i have written
#echo off
for %%a in (*.txt) do type customer=customer fromAddress=email#domain.com andOrAddress=OR toAddress=email#domain.com outputPath=E:\Exgest\customer\email#domain.com outputFormat=MIME customHeaders=true fromDate=20030901 toDate=20101231 >> (*.txt)
This failed to add a text and only created *.txt file in this folder. Then I tried running
copy >> email#domain.com
for each email address in the list, and then
copy con >> name of each file
which just added syntax is correct to the txt file. Sorry guys, I am very new at batch scripting so.
If you want to write something in a file, then you need to write this in a batch file:
#ECHO OFF
ECHO.this file contains info>>file.txt
ECHO.owner of the file is 'file.txt'>>file.txt
ECHO.data for this file is in C:\Users\'file.txt'>>file.txt
Before ECHO. write what do you want to be sended to an file with any extension.
Before what do you want to write in that file, write > if you want to keep only a single line writed, and >> if you want to write more lines in that file.
So if you have:
ECHO.thing>>file.txt
ECHO.thing2>file.txt
file.txt will looks like this:
thing2
I hope that this helps you.
P.S.: Sorry for my bad english!
What you intend to do is unclear.
What your code seems to want to do is to append "customer=customer fr...01 toDate=20101231" to each existing .TXT file. The correct syntax for this would be
#echo off
for %%a in (*.txt) do ECHO customer=customer fromAddress=email#domain.com andOrAddress=OR toAddress=email#domain.com outputPath=E:\Exgest\customer\email#domain.com outputFormat=MIME customHeaders=true fromDate=20030901 toDate=20101231 >>"%%a"
which should add the extra line at the end of each .txt file.
Note the changes - ECHO not TYPE and >>"%%a" since the metavariable %%a will contain the filename of the files *.txt - and the "quotes" ensure that "filenames containing spaces.txt" are processed properly.
#user2782162 you need to edit your question and be more specific.
I've created the below batch based on what 'I think' you are trying to do.
Please let us know if this is what you want?
Also...i'm sure #Magoo could make this a lot simpler(you're a wiz at it).
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET listloc=list.txt
:start
FOR /f "tokens=* delims= " %%a IN (%listloc%) DO CALL :process1 %%a
ECHO.
ECHO List finished - Press any key to close.
PAUSE >nul
GOTO :eof
:process1
SET fname=%2
IF "%2"=="" GOTO :eof
:process2
ECHO %1=%2 >>%fname%.txt
SHIFT
SHIFT
IF NOT "%2"=="" GOTO process2
GOTO :eof
This is assuming that your input file 'list.txt' is arranged like the below...with all the info for each 'new file' on individual lines like this....
Contents of list.txt
customer=hello1 fromAddress=email1#domain.com andOrAddress=OR1 toAddress=email1#domain.com outputPath=E:\Exgest\customer\email#domain1.com outputFormat=MIME1 customHeaders=true1 fromDate=200309011 toDate=201012311
customer=hello2 fromAddress=email2#domain.com andOrAddress=OR2 toAddress=email2#domain.com outputPath=E:\Exgest\customer\email#domain2.com outputFormat=MIME2 customHeaders=true2 fromDate=200309012 toDate=201012312
customer=hello3 fromAddress=email3#domain.com andOrAddress=OR3 toAddress=email3#domain.com outputPath=E:\Exgest\customer\email#domain3.com outputFormat=MIME3 customHeaders=true3 fromDate=200309013 toDate=201012313
The Batch will read the 'list.txt' 1 row at a time.
It will use the value for "customer" on each row as the name for the new file.
Example below show what the output for the first row will look like..
Contents of hello1.txt
customer=hello1
fromAddress=email1#domain.com
andOrAddress=OR1
toAddress=email1#domain.com
outputPath=E:\Exgest\customer\email#domain1.com
outputFormat=MIME1
customHeaders=true1
fromDate=200309011
toDate=201012311
I am trying to write a batch file to iteratively execute a fortran compiled executable. Normally one would go to the windows command prompt, type 'Model.exe'. This would bring up a dos command window asking the user to type a required file name directly in to the command window at the dos prompt.
I want to write a batch file that will do this bit for me, and also iterate this step so that I can run 10 simulations consecutively instead of having to do it by hand. This kind of shell operation would be straightforward in linux, but I do not have this available.
My pseudo code would look like this:
for /L %%run in (1,1,10) do
(set str=Sim%%run
echo.%str% > input.txt
Model.exe < input.txt)
You could break this down in to the following steps:
Assign variable 'run' a value. (e.g. 1)
Concatenate this with a string ("Sim") to make a new variable, "Sim1"
echo this to a text file ("input.txt")
Read the variable "Sim1" from file "input.txt"
Executable goes away and does its thing.
Repeat steps 1 -> 5, but with a new variable calle "Sim2" etc.
I can get the above to work if I use set str=Sim1 and then echo this directly to "input.txt", but I cannot get this to work as a loop. Am I missing something?
Best regards,
Ben
Ugh, cmd.exe's treatment of variable expansion is ugly. So, you need "delayed expansion", as follows:
setlocal enabledelayedexpansion
for /L %%i in (1,1,10) do (
set str=Sim%%i
echo !str! > input.txt
Model.exe < input.txt)
endlocal
(Of course in this particular case you could just say echo Sim%%i > input.txt but I assume there's a good reason why you want to go via another variable.)