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%
)
Related
I tried doing copy *.txt "Final.txt" but I want a new line after every file being copied.
Contents of your_file.bat:
for %%a in (*.txt) do type %%a >> final.txt & echo. >> final.txt
echo. is to add empty line. & is command separator in batch file. >> is append to text file and > will create new final.txt each time.
PS: It is good to put final.txt in different directory or use another extension e.g. final.doc otherwise final.txt contents will be added to the result.
taking "a new line after every file" not too literal, type *.txt >final.txt 2>&1 could do (it adds the filename in front of every file). To answer your question literally: copy alone can't do that. You have to iterate over the files and append the contents plus an empty line to the resulting file:
(for %%A in (*.txt) do (
type %%a
echo/
))>final.tmp
ren final.tmp final.txt
Only redirecting once is much faster than opening, writing and closing the file for every file and every empty line (you won't notice it with only some files, but it can make a difference if you have many files)
Hint: Instead of an empty line (echo/) you could also easlily do some more obvious, like
echo ====== %%A ======, which will add an title with the filename before each file (same like
type *.txt >final.txt 2>&1, but more visible).
I am having a log file.
I need to loop through the entire file in batch script and if the file contains the text 226 Transfer complete I need to do following:
write to log file File complete;
move the file to different directory;
How can I do that with a batch script?
findstr /L /c:"226 Transfer complete" "yourlogfilename" >nul
if not errorlevel 1 (
echo File complete>logfile
move "the file" "destination directory\"
)
Not that hard...
If you're referring to the Windows command line, which you probably are, here's what you can do:
#echo off
type myfile.txt | findstr /C:"226 Transfer Complete"
if errorlevel 0 echo File Complete >> log.txt
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 batch file that returns a list of data. Currently, every time I run the script, it appends the newly pulled data to the end of the text file. Is there a way that I can add the new data to the beginning of the file, rather than append it to the end? I need to it work this way because the data is pulled chronologically, and I'd like the text file to be sorted from most recent to oldest.
#ECHO OFF
REM Used to log server users stats into a text file located in the local c:\userlog directory
ECHO Terminal Server Users, %Date%, %TIME%>>c:\Userlogs\UserListTest.txt
quser /server:servername>>C:\Userlogs\UserListTest.txt
START C:\Userlogs\UserListTest.txt
Exit
any help will be greatly appreciated
Following will work as you want
echo this will come at the begining of file >>log.txt
type log1.txt >> log.txt
del log1.txt
ren log.txt log1.txt
one way using temporary file.
prepend.bat :
:: copy existing file to a temporary file
copy c:\temp\existing.txt c:\temp\temp.txt
:: replace content with your new value
echo test >c:\temp\existing.txt
:: append content of temp file
for /F %%i in (c:\temp\temp.txt) do echo %%i >> c:\temp\existing.txt
:: remove tempfile
del c:\temp\temp.txt
you can try using the code below.
#ECHO OFF
:: Store the string you want to prepend in a variable
SET "text=%1"
:: copy the contents into a temp file
type userlist.txt > temp.txt
:: Now Overwrite the file with the contents in "text" variable
echo %text% > userlist.txt
:: Now Append the Old Contents
type temp.txt >> userlist.txt
:: Delete the temporary file
del temp.txt
Hope this solves your problem. :)
One of the big issues in Batch is building ordered arrays, which has to be done via files, Batch doesn't have an array construct in it. One option you can do to force things into memory and save some IOPS in batch is use a ramdisk, making sure to abstract the filepaths into variables.
First, read the contents of the file. Append that to what you want to add to the beginning and write all of that to the file.
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.