Appending content of batch to file to existing file name - batch-file

I have 2,300 text files with filenames that currently do not include the information needed, but must continue to retain their existing file name after I append content from the text files to the names. The text file content is structured and the code should grab the following content out and append to the existing filename.
Short Name = John Doe
I only want John Doe to be pulled from the content and appended to the file name. All the existing file names are unique and should remain the same, I'm just appending whatever is after "=" to the file name.
All the existing questions involve renaming the existing file, appending non-changing text to file names, or some variation thereof. It'd be great if I could have the files go into a separate directory after appending, but not required (I'll be working off copies anyway). Somehow no one is keeping the existing file name and appending content!
How to Batch Append Filename to TXT Contents, Name Output File Based on Original Input Filename, and Loop Through 200,000 Input Files
Renaming file based on its content using Batch file
How to Append to existing file name within a batch command in a for loop
#echo off
setlocal
for /F "tokens=2 delims==" %%a in ('findstr /I "LastLogedUser=" something.txt') do set "uniuser=%%a"
echo User is: %uniuser%
copy fpr_log.txt "c:\fpr_log%uniuser%.txt"
endlocal
#echo off
for %%a in (*.txt) do type append_me >> %%a
I found the code above, but the problem is it doesn't append and I only found a snippet of append code.

Related

How to insert text into multiple text files using a .bat file?

I having drouble to create a batch file that can insert text into multiple text files at once.
What I can do just now is that one file that I drop on the batch file is getting inserted with the text "I AM PROGRAMMING".
But I would like to drag multiple text files and that all of it/each every text file gets inserted with that text.
#echo off
echo I AM PROGRAMMING> "%~1"
Or if there is somehow possible to do so every text file in a specific place/folder gets inserted with a specific text (for example "I AM PROGRAMMING")?
Your solution processes each matching file in the folder, but you said I would like to drag multiple text files.
It's easy to process "multiple dragged files": %* is "all parameters". Just use a plain for loop to process each of them:
#echo off
for %%a in (%*) do echo I AM PROGRAMMING> "%%~a"
(note that this overwrites the files with the one new line; if you want to append, use >> instead of >)
In case you want to process "all .txt files in the folder of the one file I dragged":
for %%a in ("%~dp1\*.txt") do ...
Or "all files in the same folder of the one file I dragged and the same extension as the dragged file":
for %%a in ("%~dp1*%~x1") do ...
I found my solution!
#echo off
cd "yourfilepath"
for %%a in (*.txt) do (echo I AM PROGRAMMING> %%a)
I merged these two helpful information.
How can I use a batch file to write to a text file?
And
Append multiple files using a .bat

xpdf batch file: how to change file name?

I'm using a simple batch file calling the xpdf engine to convert a PDF to a TXT file. Right now, the resulting txt file's name is the same as the PDF's, except the extension has been changed to .txt of course. However, I want to add some text behind the original file name, how can I do this? For example, if there's a PDF called test.pdf, it should be converted to text and stored in a txt file called testFULL.txt.
This is the current batch file I have:
for /R %%s in (*.pdf) do "C:\xpdf\bin32\pdftotext" -raw "%%s"
Based on the comment showing that the output text file is a parameter:
for /R %%s in (*.pdf) do "C:\xpdf\bin32\pdftotext" -raw "%%s" "%%~dpnsFULL.txt"

How to copy *.rar files listed in a text file to another folder?

I have a text file containing file names and need to copy the files listed in the text file to another folder.
Only one problem is that the file names are without .rar at end in the text file.
I am looking for a batch file that can read the text file and copy all the .rar files listed in text file to another folder.
Assuming your text file looks like this and is called files.txt
file1
file2
file3
and the files in your folder are named file1.rar, file2.rar and file3.rar the following commandline can achieve what you want:
for /f "delims=" %f in (files.txt) do copy "%f.rar" "c:\some\other\folder"
How does this work?
Using the for /? details the options available and it's usage:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
file-set is one or more file names. Each file is opened, read
and processed before going on to the next file in file-set.
Processing consists of reading in the file, breaking it up into
individual lines of text and then parsing each line into zero or
more tokens. The body of the for loop is then called with the
variable value(s) set to the found token string(s). By default, /F
passes the first blank separated token from each line of each file.
Blank lines are skipped.
I used the delims option to make sure the complete line is read, including any spaces.
delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab.
In this solution the copy command is called for each line in the text file which enables fixing the missing extension and copying it to a new location.

batch file to swap out info between two files

I need help with a batch file to do the (I suppose simple) task of reading info from a master file between two "tags" and overwriting current info in other file with the info from this master file.
Here is my situation as a simple example:
I have a file called "template.htmltplt" that is my master file if you will. Then I have a bunch of other ".html" files.
I would like the batch file to:
Go through all the html files
Delete all the lines between <!--Stuff-Start--> and <!--Stuff-End-->
Copy the content between the <!--Stuff-Start--> and <!--Stuff-End--> tags in the template.htmltplt into their correct place in the other html files.
Is this even possible and if so how?!
I have NO bat script knowledge so well commented code would be awesome!
Thanks in advance for those willing to help!
Regards,
Reinhardt
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "startstring=<!--Stuff-Start-->"
SET "endstring=<!--Stuff-End-->"
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
(
SET "block="
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "^" q23715314.txt') DO (
IF "%%b"=="%endstring%" SET "block="
IF DEFINED block ECHO(%%b
IF "%%b"=="%startstring%" SET block=Y
)
)>"%tempfile%r"
FOR /f "delims=" %%t IN ('dir /b /a-d %sourcedir%\*.html') DO (
SET "block="
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "^" "%sourcedir%\%%t"') DO (
IF "%%b"=="%endstring%" SET "block="
IF NOT DEFINED block ECHO(%%b
IF "%%b"=="%startstring%" SET block=Y&TYPE "%tempfile%r"
)
)>"%destdir%\%%~nt.html"
del "%tempfile%*"
GOTO :EOF
Not hard.
I used a file named q23715314.txt containing this data for my testing:
drop <this> line
<!--Stuff-Start-->
Insert this
and this
and even <this> line after an empty line
<!--Stuff-End-->
omit this
leave this out
And test .html file:
leave <this> line
empty line retained
<!--Stuff-Start-->
Replace this
replace this too
and substitute for <this> line
<!--Stuff-End-->
keep this
retain this too
resultant new .html file:
leave <this> line
empty line retained
<!--Stuff-Start-->
Insert this
and this
and even <this> line after an empty line
<!--Stuff-End-->
keep this
retain this too
Naturally, you'd need to set your own file and directory names, and I'd advise strongly against trying to use the same directory for the source and destination. That won't work at all. And any .html source line that starts with one or more colons will have those colons stripped-out - not that many .html lines start with colons, but it needs to be said...
How it works - block by block.
The first part would seem obvious. The directories involved are defined and the target strings, too.
Next there's a create-a-tempfile routine. Simply generate a random filename and see whether there is an existing matching filename in directory %temp%. Personally, I set up temp to be c:\temp but the code is designed to use the default. So, if the random-number generator chooses 18749 then the code looks for any file 18749... in the temporary directory. If such a file exists, then choose another random number. If it doesn't create a file named 18749a in the temporary directory. This is simply a placeholder.
Next step is to extract the required lines to a temporary file. the (block of code)>filename syntax directs any data echoed to a new file in filename - which should contain the full filename of a valid file in the temporay directory; for example c:\temp\18749r.
The code within the block first sets block to empty, then reads the file q23715314.txt line-by-line, numbering each line by prefixing it with number:. This ensures that empty lines are processed as 13:, otherwise they'd be skipped. The q23715314.txt isn't significant - it can be any file containing the required template data. I simply use qSOquestionnumber.extension in order that I can keep the data in files related to the batch file I write (called qSOquestionnumber.bat) - so the many questions using file1 and file2 can be easily individually retrieved in the case of a problem. The temporary file could be any valid filename you like, if you want to have a fixed filename. Note however that filenames containing spaces and some other symbols will need to be "quoted".
Since each line is processed by the for as it it was number:linefromfile then using tokens=1*delims=: will assign the number to %%a and linefromfile to %%b.
The block processing simply matches the line that was read from the file to the start/end string defined. block was originally "set" to empty, so it is undefined. When the startstring is matched, block is assigned a value. I used Y, but any value will do.
When the for reads the next string from the file, it finds that block is now defined, so it echoes %%b to the file c:\temp\18749r. This continues until endstring is found, when block is "set" to empty again; hence it is undefined and there is no more echoing to c:\temp\18749r.
The inner for of the second block is similar, but reversed. It reproduces each line from the file selected in the outer loop until the startstring is found, then types the contents of the tempfile and waits for the endstring when it clears blockand hence turns onthe echoing again.
The outer loop simply reads dir /b /a-d for the source directory - a directory list of simply the filenames. The "delims=" assigns the entire line to %%t and hence the outer loop is for...%%t...do (innerblock)>"%destdir%\%%~nt.html" which redirects the data echoed by the inner loop to the file with the name part of %%t (%%~nt) with the destination directory specified and the extension .html
Finally, the tempfiles are deleted.
You can't do that with a batch file. Use visual basic or pearl for something like that. Closest a batch file could do to something like that is have the output from one file be used as the input for another file but that's as specific wait can get.
Lottopix.has > prevwinum.cgi

Batch file - How to read multiple .txt file that in a folder?

Currently I just success to read from one .txt file only. Here is my code.
for /f "delims=" %%a in (C:\test\Scriptlogs\COB\log_DP_20140331_1509_CW52.txt) do SET e=%%a
My question is I got one folder contain of many .txt file, however above code only read specified .txt file. Any changes that I need to made so that it can loop through all the .txt files?
Example of .txt file name.
log_DP_20140331_1324_CW52.txt
log_DP_20345692_1234_CW51.txt
log_DP_21234324_2134_CW50.txt
FYI, folder contains more than 3 .txt files.
Any guidance ,answer or similar post to share?
Thanks for viewing, comments and answers.
You have how to iterate over a file content. The only you need is to iterate over the file list and read the content of each file. So
for %%x in (C:\test\Scriptlogs\COB\log_DP_*.txt
) do for /f "usebackq delims=" %%a in ("%%~fx") do .....
Where %%x holds a reference to each file in the set and %%~fx is the full path to the file. To avoid problems with possible spaces in filenames, i have quoted it "%%~fx", so, to indicate to for /f comand that it is not a string but a file, usebackq has been included in the for options string
What you need is the forfiles function. You can put the code you have into the body of the forfiles loop. Check out ss64.com it is a great resource for a number of scripting languages.

Resources