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.
Related
I know pretty much nothing about batch programming and I would like to do the following thing.
I have a .txt file that could have one or several lines and I would like to cut the last line and insert it into another .txt file. I have been doing some research before asking this question but I couldn't find anything "understandable" to me in order to achieve this.
Also, the most appropriate QA found, was this link on Stack but I don't know how to "translate" it.
This are my lines within the .txt file
S,1,______,_,__;Cutii carton 370*290*2;1.00;1.000;1;1;1;0;0;
T,1,______,_,__;0;1.00;;;;
I would like to cut the line starting with "T" from this .txt file and insert into a new .txt file.
Could anyone help me? Thanks
EDIT
Please see the print screen
get the last (non-empty) line from a text file:
for /f "delims=" %%A in (ttt.txt) do set last=%%A
appending it to another file:
echo %last%>>ttt_lastline.txt
Using this:
#For /F %%A In ('Find /C /V ""^<"%~1"') Do #Set/A _=%%A-1
#(More +%_% "%~1")>"%~dpn1_lastline.txt"
Just drag and drop your text file onto the batch file and the last non empty line will be output into a text document in the same directory as the dropped file. The output file will have the same name as the dropped file but with _lastline appended to the end.
[Edit]If you want to hard code the file name then:
#For /F %%A In ('Find /C /V ""^<"File.txt"') Do #Set/A _=%%A-1
#(More +%_% "File.txt")>"File_lastline.txt"
You may prefer the following method although it will likely be slower especially as File.txt gets larger:
#For /F "UseBackDelims=" %%A In ("File.txt") Do #Set "last=%%A"
#(Echo %last%)>"File_lastline.txt"
To cut the first "n" lines of several files and concatenate them sequentially into one, you can use:
For %f in (list-filename*.txt) do more +n(number of lines to be removed) > type >> Outputfilename.txt %f
Is there a way to make a file and name it %file_name%+.txt
basically i want to create a file and name it the content of a variable + "_temp.txt" and in the same directory.
then write to it the content of of another file without the first line. read that line
and write the content of the new file to the old one and continue that until no more lines are left and delete the new file.
does that make any sense?
for /f "skip=1 delims=" %A in (c:\windows\win.ini) do echo %A >> Newfile.txt
See help, for /?, echo /?, and a list of punctuation at Trouble with renaming folders and sub folders using Batch.
In a batch file, as for /? explains, use %%A rather than %A when typing.
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 have a text file, call it path.txt in C:\path.txt and it will only have one line of text at a time. That line of text will be a file path, call it C:\projects\test.txt.
What's the best way to first read the text from C:\path.txt and then second use the sort command in a batch file to alphabetize the file whose path is defined by the text string in path.txt ?
Lastly, I want to erase the line of text from that C:\path.txt file.
Please let me know if this is too vague or needs a better explanation and thanks in advance.
The batch file I have now reads:
FOR /F "tokens=*" %%i IN (C:\DONOTMODIFY.txt) DO #ECHO %%i
set "filename=%%i"
SORT filename /O filename
Why the artificial limitation of only one line? Anyway yes this is simple enough. Anyway this will process any number of lines in target.txt. Code off my head so test before real use.
#echo off
for /f %%i in (target.txt) do (
sort %%i /o %%i
)
echo. > target.txt
EDIT:
Instead of echo. > target.txt you could use copy /y nul target.txt > nul that actually creates a totally empty file unlike echo. that makes a blank line.
PS: a tip for future questions: Show that you actually tried something. This is not a do my program for you site.
Okay, so basically, I have a whole list of links in a plain text Notepad file, each link on a seperate line. All I am wanting to do is to add a bit of text before each link, specifically: 127.0.0.1 and a couple of spaces.
So this...
somelink.com
becomes this...
127.0.0.1 somelink.com
By now you've probably already guessed that I am trying to edit the contents of a text file to make it useable as a HOSTS file in Windows.
So I am wanting some batch file code, executable in a .bat file, which basically reads a Notepad text file, and then add "127.0.0.1 " at the beginning of each line with text on it. I am guessing this is probably a very simple piece of code for someone with some knowledge of MS DOS and batch file code, but that most certainly isn't me, and the only batch files I have ever written have been with help like now.
Thanks for any and all help in advance with this, it really is much appreciated.
read HELP FOR and then try this in a command prompt
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1 %a >>output.txt
here is some explanation and some considerations to extend it with a little more complete functionality and put it in a BAT file
FOR is the command to iterate over the lines of your input text file. Read microsoft documentation at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
input.txt is the text file that contains your list of domain names, it must reside in the current directory
output.txt will be the result file that will contain the list of domain names prefixed with 127.0.0.1, it will be created in the current directory
If you want to create a BAT file, you need to move the FOR command and edit it a little bit, changing the %a loop variable names to be %%a.
You can then place the BAT file either in the current directory, where your input resided and where the output will be created.
Alternativelly, you may place your BAT file, elsewhere. In that case, you need to invoke it with its full path.
Or you may even place it in a special directory (I have my own C:\Program Files\CMD) and add it in the PATH system variable. See here www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true how you can change your PATH for your current session. And here ss64.com/nt/path.html you may find some explanation on how to make the PATH change permanent.
Also, you might be tempted to add some flexibility to your BAT file, instead of having constant input.txt and output.txt filename, replace them with %1 and %2 which represent the first and second command line parameters of your BAT file.
the user might then want to use files which contain blanks in their filenames. They might specify them surrounding the names with quotes ". In that case, you need to add some incantation usebackq in the FOR command for it to not break havoc when the user uses quotes.
Finally, you will need to decide what to do in case the output text file already exists, you migh want to consider prevent overwriting.
So putting all this pieces together, here is a short BAT file to get you started...
#echo off
if .%2==. goto help
if not exist %1 goto helpno1
if exist %2 goto helpalready2
FOR /F "usebackq delims=" %%a in (%1) do #echo 127.0.0.1 %%a >>%2
goto :eof
:help
echo you need to specify input and output text files
goto :eof
:helpno1
echo %1 not found
goto :eof
:helpalready2
echo %2 already exist
goto :eof
welcome to BAT programming and enjoy!
here we go!
(
Set /p line1=
Set /p line2=
Set /p line3=
Set /p line4=
)<Filename.txt
echo 127.0.0.1 %line1%>Filename.txt
echo 127.0.0.1 %line2%>>Filename.txt
echo 127.0.0.1 %line3%>>Filename.txt
echo 127.0.0.1 %line4%>>Filename.txt
This will Read the first four lines of the text file, and then put in your stuff and each line back into the line it came from.
Have Fun!
In addition to PA.'s answer, if you require a specific number of spaces, you can throw them into a variable and add it to the command as well.
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1%spaces%%a>>output.txt
So the output will be
127.0.0.1 somelink.com
Batch-File flavor:
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %%a in (input.txt) do #echo 127.0.0.1%spaces%%%a>>output.txt