I want to create a another file output from the batch file that I am running, meaning the raw content is inside it. Inside this batch file it would do a while or for loop until it has found the "end" or any unique word that I add at the end of the content.
I don't want to simply copy the file(which has important content) as this will expose information. Then run it by calling the file via batch.
code:
-start of batch
-check if the file a.txt exist del a.txt
-starts to create the a.txt file
>start of content:
>"the quick brown fox jump over the lazy dog"
>lynx: unique word
>end of content
-writes the content to a.txt using for loop or while until it finds the unique word
-runs the a.txt file
-deletes the a.txt file
-exit
Hope someone can help. Thank you!
Or if someone can suggest a better way to do it. I would appreciate it
The line with the for command itself will need quotes.
For loops work fine until they hit a line that contains quote marks then everything turns to custard...
You can't get there form here.
Maybe try vbscript instead?
#echo off
setlocal enabledelayedexpansion
if exist a.txt del a.txt
copy nul a.txt > nul & :: Not needed, but in your specs
set line[1]=This is what you asked for.
set line[2]=Not really the best method.
set line[3]=But it will work.
set line[4]=
set line[5]=linux
set line[6]=[end]
for /l %%i in (1,1,1000) do (
if "!line[%%i]!"=="linux" goto :end
if "!line[%%i]!"=="[end]" goto :end
if "!line[%%i]!"=="" echo.>>a.txt
if NOT "!line[%%i]!"=="" echo !line[%%i]!>>a.txt
)
:end
:: Broken Out Of Loop
ren a.txt a.bat
call a.bat
del a.bat
Using the line[n] method is rather wasteful of memory, I would reccomend moving the data from one file to another.
Related
I want to make a .bat files that can allow me to delete the file if a certain string of characters appears inside. For example, if I want to delete files that have the string harp inside of it and the files in the folder are:
harp1.txt
mellowharp.txt
highharplow.txt
I want to be able to delete all of these files. I figured out how to do it if it starts or ends with the string, by merely writing
del harp*.*
del *harp.*
But I can't figure out how to do it if harp is found in the middle of the string.
I've tried things like putting an asterisk on each side of harp for a catch all case, but obviously that doesn't work.
This is literally my first day and experience with .bat files, and I would really appreciate any help I get on this, but please by gentle and explain EVERYTHING because I'm coming into this with no knowledge whatsoever.
Well, since using del *harp*.* didn't work for you, you could iterate through all the files in the specified directory then delete when the substring "harp" is found in the file name.
This should do it:
#echo off
for /f %%F in ('dir /b * ^| find /i "harp"') do del %%F
echo Files Successfully Deleted...
pause>nul
exit
Make sure this .bat file is in the same directory as your other files.
Explanation:
The for command loops through the files in the directory and assigns them to the variable %%F and searches for the substring "harp" in the file name. Then, when found, the do del %%F tells the program to delete the file (%%F). Read more about for statements here.
The echo command simply prints a line of text to the console window. To read more about echo click here.
The pause>nul pauses the program and waits for the user to press a key, but without any text being displayed on screen. (The normal text that would appear would be press any key to continue...). More about pause here.
The exit command stops the program. More on exit here.
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%
)
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