Computer Hope
Microsoft => Microsoft DOS => Topic started by: _unknown_ on January 25, 2016, 07:30:38 PM
-
Tried to use this script from here http://stackoverflow.com/questions/23075953/batch-script-to-find-and-replace-a-string-in-text-file-without-creating-an-extra (http://stackoverflow.com/questions/23075953/batch-script-to-find-and-replace-a-string-in-text-file-without-creating-an-extra). When I tried to assign a one long line text to the variable replace, the script run successfully. But when I tried to set a multi line to the variable replace and run the script and it always says The syntax of the command is incorrect. Any help? :( Thanks!
@echo off
set "search=<one>asdfghjklzxcvbnmqwertyuiop</one>
set "replace= <roses are ="RED">
<violets are="blue">1</violets> #this should be in a new line
<sugar is="sweet">1</sugar>" #this should be in a new line
set "textFile=A2015336045000.L2_LAC.SeAHABS_chlora.vrt"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
-
Why are you using the <and > things?
These are redirection symbols.
Also, it is not a good idea to embed the new line thing into something. If you have something that takes three lines, just use three variables for three lines.
Is there a strong reason to put new line into a message? I can't imagine why you would need it.
-
Why are you using the <and > things?
My strings contains these symbol.
Is there a strong reason to put new line into a message? I can't imagine why you would need it.
Since I have multiple lines, I don't have any idea on how to put the rest of the lines into a new line.
-
Tried using three variables for the three lines. But the output displays them in only one line.
-
If you explain your task in English then you may get a solution.
Batch files don't like multiple lines in an environment variable.
-
Thanks, Foxidrive.
From what little I understand of his post, I did a short batch file to ilustrate what it looks like to me.
REM This is a very simple batch file
REM It is named simple1.bat
echo off
set rose=Roses are Red,
set viol=Violets are blue.
set poem=If we ever wed,
set when=Say I love lyou.
echo %rose%
echo %viol%
echo %poem%
echo %when%
echo on
REM Write to a TXT file
echo off
REM Frst arrow write new file
REM two arrows add to file
@echo %rose% > new.txt
@echo %viol% >> new.txt
echo %poem% >> new.txt
echo %when% >> new.txt
type new.txt
pause
REM All Done!
Screen Shot attached.
[attachment deleted by admin to conserve space]
-
My strings contains these symbol.
And they have special meaning to the command interpreter. > and < are, as Geek mentions, redirection symbols. You have to escape them if you want to use them, by prefixing them with a Caret. For example:
C:\>echo <
The Syntax of the command is incorrect.
C:\>echo ^<
<
Since I have multiple lines, I don't have any idea on how to put the rest of the lines into a new line.
The Command Interpreter doesn't support multi-line environment variables. There are ways to create them but they are rather unpleasant and tend to be more difficult to work with then changing the problem such that it doesn't require processing of multi-line environment variables.
-
Yes you have to cancel out the sign. here is a example
@echo off
echo This is a message inside the file text.txt >> text.txt
echo @echo off
echo echo Adding the carrot sign cancels the sign out ^>^> text.bat >> text.txt
echo pause >> text.txt
-
Mybad blonde moment~ correction below
@echo off
echo Rem This is a message inside the file text.txt >> text.txt
echo @echo off ^>^> text.bat >> text.txt
echo echo Adding the carrot sign cancels the sign out ^>^> text.bat >> text.txt
echo pause ^>^> text.bat >> text.txt
-
From what little I understand of his post, I did a short batch file to ilustrate what it looks like to me.
I guess I'm getting old and crotchety, but I ask simple questions for a couple of reasons.
1) is to get a response of some kind - because why would you go to any effort when a poster can paste their question into 12 different forums, and they will not even see what you have written because it was answered somewhere else.
2) to help avoid misunderstandings. That just wastes your time when you assume they mean pie and they actually mean PI, if you follow that odd example.
I didn't even read the fellows question all the way through to be honest.
What I noticed was words that didn't describe an actual task - so I stopped reading - because of point 2 above.
-
If you explain your task in English then you may get a solution.
Let's say I have a text file named input.txt. The input.txt contains 5 lines. What I need to do now is insert "multiple lines" on the 2nd line of the input.txt and then save it again with the same file name which is input.txt(overwrite). But the thing is the lines to insert in the input.txt is indented and enclosed with < >.
input.txt
This is
an
example
of
input.txt
The multi line should be inserted after the line This is.
Multi line to insert
<Insert="Line 1">
<Insert="Line 2">file1.vrt</Line2>
<Insert="Line 2">1</Line2>
<Insert="Line 3">file2.vrt</Line3>
<Insert="Line 3">1</Line3>
<Another line="Line 4">0</Line4>
rem And so on. . .
</Insert>
-
So you are trying to edit an XML file?
-
So you are trying to edit an XML file?
The extension name of the file are *.vrt. But it says in here http://www.gdal.org/drv_vrt.html (http://www.gdal.org/drv_vrt.html) that it's based in an XML file. Maybe it's an xml file.
-
Uummm...yea.
-
Let's say I have a text file named input.txt. The input.txt contains 5 lines. What I need to do now is insert "multiple lines" on the 2nd line of the input.txt and then save it again with the same file name which is input.txt(overwrite).
Thanks, that's useful detail.
But where does the multi-line data exist? Is it inside a file?
Do you want to put all the information that is inside one file, after line one of the other file?
-
Thanks, that's useful detail.
But where does the multi-line data exist? Is it inside a file?
Do you want to put all the information that is inside one file, after line one of the other file?
Yes foxidrive, the multi-line data is inside another file. And I want to put all the information from that file, after the line one of another file. Actually, I want to edit the extension name of my sample(but unable to modify it, sorry about that). The files are .vrt not .txt. VRT files are like xml files and the content per line has < and > character.
-
This might work:
@echo off
for /f "usebackq delims=" %%a in ("file.vrt") do (
echo(%%a
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
)
pause
-
This might work:
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
Sorry to ask but what does this line means?
-
This might work:
@echo off
for /f "usebackq delims=" %%a in ("file.vrt") do (
echo(%%a
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
)
pause
How will i overwrite the output of this to the file.vrt? I have observed that when all the information from file2.vrt was inserted to file.vrt,, the 2nd line of the file.vrt was positioned right after the last character of the last line of file2.vrt when it should be on the line after the last line of file2.vrt.
output in command line:
This is
<Insert="Line 1">
<Insert="Line 2">file1.vrt</Line2>
<Insert="Line 2">1</Line2>
<Insert="Line 3">file2.vrt</Line3>
<Insert="Line 3">1</Line3>
<Another line="Line 4">0</Line4>
</Insert> an
example
of
input.txt
-
The reason it's butted up against the line above is the line endings, that I spoke to you about in your last thread on this same issue.
In your file2.vrt there is no CR/LF pair at the end of the last line, and this is something that depends on the editor or process being used to create the file2.vrt file.
Some files have that there, some don't.
-
I see. Do you know how to overwrite the output to the file.vrt?
I'm also wondering what this lines mean?
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
-
I forgot about redirecting it to a file.
This also adds the missing CR/LF
@echo off
(
for /f "usebackq delims=" %%a in ("file.vrt") do (
echo(%%a
if not defined stop type "file2.vrt" & echo(
set stop=are-we-there-yet?
))>file.tmp
move file.tmp "file.vrt" >nul
pause
Which part of the other two lines didn't you understand?
-
I forgot about redirecting it to a file.
This also adds the missing CR/LF
@echo off
(
for /f "usebackq delims=" %%a in ("file.vrt") do (
echo(%%a
if not defined stop type "file2.vrt" & echo(
set stop=are-we-there-yet?
))>file.tmp
move file.tmp "file.vrt" >nul
pause
Which part of the other two lines didn't you understand?
I don't understand the two lines :( Is this only possible with one file? or is it also possible with many files?like I have many file.vrt in a folder and I want to insert file2.vrt in all of them. I also want to add the directory of where the file2.vrt is located.
This is what I did, but no luck :(
@echo off
set "input=C:\path\to\files\"
set "output=C:\path\to\output\"
set "file2=C:\path\to\file2\"
cd /d "%in_path%"
for /f "usebackq delims=" %%a in (*.file.vrt) do (
set "fileName=%%a
echo(%%a
if not defined stop type "%file2%\file2.vrt"
set stop=are-we-there-yet?
-
I don't understand the two lines :( Is this only possible with one file? or is it also possible with many files?
Yes, more files are possible.
But it needs changed code.
if you provide accurate details in the beginning then you don't waste the time of the volunteers, and you get a working script in the first reply.
-
For what it's worth..
The general method of overwriting a file after a join operation.
Open three files, two input, one output.
Perform the join on the two input files.
Send output to the third file.
If job is free of error, then
rename the original file as a backup.
rename the output file to the original name.
else report error
end of job.
Trying to overwrite the original file "on the wing" is a very bad idea.
-
Yes, more files are possible.
But it needs changed code.
if you provide accurate details in the beginning then you don't waste the time of the volunteers, and you get a working script in the first reply.
I'm sorry I forgot about that. That is the last thing I need.
-
Yes, more files are possible.
But it needs changed code.
if you provide accurate details in the beginning then you don't waste the time of the volunteers, and you get a working script in the first reply.
I hope you can still help me about it :(
-
You've still hidden most aspects in your new thread, and posted it elsewhere too.
I dislike writing code when it's all made up - what happens is what you've already seen, the volunteer has to re-write the code again.
If the question is simple and no effort is needed then I'll offer a suggestion - but if it's complex then more work is needed to give a solution, and in the same way even more work is needed to re-write *that* script when some other aspect turns out to be important, and was hidden.
It may not seem such a horrible thing - but when it's happened for the ten thousandth time, it gets a bit irritating. :D
-
That's the last thing I need in this topic. i just had numerous files and forgot to include it.
I don't know how to solve it even when I tried and tried. Just asking for help since it's an urgent matter. I apologize.
-
After trying and trying I've come up with this code:
@echo off
set src=C:\path\to\source\files
set dst=C:\path\to\destination\files
for /f "tokens=*" %%a in ('dir /b "%src%\*.file.vrt"') do (
for /f "usebackq delims=" %%h in ("%%a") do (
echo(%%h
if not defined stop type "%src%\file2.vrt"
set stop=are-we-there-yet?
))>%dst%\%%~na.tmp
move /y %dst%\%%~na.txt %dst%\%%~na.vrt >nul
)
But after running this code, the output files from the dst folder looks likes this: (It happens to all the files I have in src folder)
Can't figure out what is causing this error and how to fix it.
C:\path\to\destination\files>(
echo(<This is line 1 of file.vrt>
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
)
<This is line 1 of file.vrt>
<Insert="Line 1">
<Insert="Line 2">file1.vrt</Line2>
<Insert="Line 2">1</Line2>
<Insert="Line 3">file2.vrt</Line3>
<Insert="Line 3">1</Line3>
<Another line="Line 4">0</Line4>
</Insert>
C:\path\to\destination\files>(
echo( This is line 2 of file.vrt
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
)
This is line 2 of file.vrt
C:\path\to\destination\files>(
echo( <This is line 3 of file.vrt>
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
)
<This is line 3 of file.vrt>
C:\path\to\destination\files>(
echo( <This is line 4 of file.vrt>
if not defined stop type "file2.vrt"
set stop=are-we-there-yet?
)
<This is line 4 of file.vrt>
and so on. . . . . . . .
This error also appears at the end of the line:
C:\path\to\destination\files>move /y C:\path\to\destination\files\
%~na.txt C:\path\to\destination\files\%~na.vrt 1>nul
The system cannot find the file specified.
-
Is this what your asking for?
@echo off
set nl=^& echo.
echo %nl%Multiple%nl%lines%nl%variable%nl%
or maybe this?
@echo off
setlocal EnableDelayedExpansion
(set nl=^
%=DONT_CHANGE_THIS_LINE=%
)
echo !nl!multiple!nl!lines!nl!in!nl!a!nl!variable!nl!
its not really in one variable but it should solve your problem.
You can even add some style to your code if you really wanted to.
@echo off
set nl=^& echo.
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» %nl% º º %nl% º A better example of this! º %nl% º Hopefully This helped? º %nl% º º %nl% ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ %nl%
pause
rather than this creating a multi lined variable, it creates a variable that represents the new line it's self
-
Is this what your asking for?
@echo off
set nl=^& echo.
echo %nl%Multiple%nl%lines%nl%variable%nl%
or maybe this?
@echo off
setlocal EnableDelayedExpansion
(set nl=^
%=DONT_CHANGE_THIS_LINE=%
)
echo !nl!multiple!nl!lines!nl!in!nl!a!nl!variable!nl!
its not really in one variable but it should solve your problem.
You can even add some style to your code if you really wanted to.
@echo off
set nl=^& echo.
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» %nl% º º %nl% º A better example of this! º %nl% º Hopefully This helped? º %nl% º º %nl% ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ %nl%
pause
rather than this creating a multi lined variable, it creates a variable that represents the new line it's self
Thanks but foxidrive's code is working. But the code only works on single file and I forgot to include in my question that I have more file.vrt files.
-
@echo off
set "src=C:\path\to\source\files"
set "dst=C:\path\to\destination\files"
pushd "%src%"
(
for /f "delims=" %%a in ('dir /b /a-d "*.vrt" ') do (
set "stop="
for /f "usebackq delims=" %%b in ("%%a") do (
echo(%%b
if not defined stop type "file2.vrt" & echo(
set stop=are-we-there-yet?
)))>"%dst%\%%a"
That's untested.
Keep in mind that you make it harder for the volunteers by not saying what you really want to do, and they are spending their own free time to assist you.
People asking for help with a scripting technique don't need to provide lots of detail, but people who need help writing a complete script do need to say what they are doing!
i just had numerous files and forgot to include it.
"forgot" is not what happened here. :D
-
I predict 5 pages...
-
I predict 5 pages...
At 3 and counting. :-\
-
Thanx for your contribution...i had already noticed though.
-
Thanks but foxidrive's code is working. But the code only works on single file and I forgot to include in my question that I have more file.vrt files.
so your basically trying to do this if i am correct, use the example below
you have a variable name "a"
and you basically want %a% to equal everything in specific lines?
set a=
line1
line2
line3
so that %a% can reuse both lines 1,2, & 3 from just calling %a%?
That's not a easy thing to do in batch files, i mean it's possible but it's a complete pain in
doing.
maybe you could cheat the code lol.
command 1 >> "temp"
command 2 >> "temp"
command 3 >> "temp"
set /p OUTPUT= < "temp"
del "temp"
echo %OUTPUT%
-
It does not work.
Zack, why post code that does not work.
Rather than prove anything, it paints you as a novice. ::)
-
We've known that for quite awhile...
-
It does not work.
Zack, why post code that does not work.
Rather than prove anything, it paints you as a novice. ::)
My bad long day, i advised a better solution, the file will turn each individual line into its own variable, which can be recalled throughout multiple lines. also i was hoping that people would know not actualy to type command 1,2, or 3. it was just an example. hope this helps now that my brain is working correctly lol.
you could select from a specific line from your "file.txt" or whatever you need, just be sure to replace it's file name with a different file name that has the code you need to use.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in (file.txt) do (
set /a c+=1
set x[!c!]=%%a
)
set x
pause
what ever appears in the prompt, add percentage signs around it.
for example; if you had a line that said "echo hello" inside "file.txt" and you wanted to find that specific line and turn it into a variable, it would display this.
x[1]=echo hello
so in your original batch file you would turn it into %x[1]% and it will display "echo hello" from line x[1] inside of "file.txt". if you wanted it to store more lines to make your variable larger you could do so as you wish. you can also put your %x[1]% variable inside the second variable (%[x2]%) if you wanted a single variable with multiple lines. then use my previous command "set nl=^& echo." inside of it as well to avoid two lines of code to appear on the same line. thus resulting in one multi lined variable.
working on an example so give me a minute
-
:/ zzZ so tired
-
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem represents the new line
(set nl=^
%=DONT_CHANGE_THIS_LINE=%
)
echo hello >> file.txt
echo. >> file.txt
echo friends >> file.txt
echo created file.txt with the words "hello" and "friends" inside of it
pause
rem turns all lines into a variable
for /f "delims=" %%a in (file.txt) do (
set /a c+=1
set x[!c!]=%%a
)
set x
pause
echo your variables are
echo %x[1]%
echo %x[3]%
pause
echo your final multi lined variable
echo %x[1]%!nl!%x[3]%
echo well sorta lol.
-
echo well sorta lol.
Sorta is only valid playing horseshoes with my grand daughter.
-
Sorta is only valid playing horseshoes with my grand daughter.
Sorta doesn't imply that the code doesn't work, implies rather that the variable are beside each other than combined into one (not a big deal really). it just a work around. from what ive heard is that he/she wants a variable that contains multiple lines of code, and when he/she runs that variable it runs all the code inside of it just as any other variable with one line would do. it doesn't imply that the code above doesn't work as it does just fine with variables set beside each other, and if you wanted to honestly you could just put the contents of the variable inside the other variable?
I was implying that it may not be a multi lined variable because its not just one variable, but does just the same so.... idk try it, it should work, that is if this even is what Unknown is asking for. :/ it's still unclear what unknown is even asking for, it would be helpful if he/she would post his/her code. think none of us here are actually going to go through the pain to make a real multi lined variable in batch with hardly any explanation of what it's for, but a work around seems more logical, still think he should explain better so we can find a solution
-
At 3 and counting. :-\
yeah, i hope it doesn't go that long :-\
-
On last time. Don't put multiple;e lines into a variable.
It is forbidden, not allowed, senseless, worthless and futile.
It is like trying to brush your teeth with hand soap.
Stop with the lines in a variable .
Tell us what your really need to do.
Putting lines into a variable is not what real men do. do.
Go read a book. Or get a tutor.
-
On last time. Don't put multiple;e lines into a variable.
It is forbidden, not allowed, senseless, worthless and futile.
It is like trying to brush your teeth with hand soap.
Stop with the lines in a variable and we will tell you how to do what your want to do/.
Putting lines into a variable is not what real men do. do.
Go read a book. Or get a tutor.
Well im not the one who asked for this, and i didn't really put multiple lines in a variable, just called the "echo." command and turned it into a variable. and added after some regular code, just trying to help. im not a big fan of the whole multi lined variable thing myself. kinda is nonsense really. im sure in some very rare case it could be useful but still it's not like it's the most biggest breakthrough in computer programming ever, and even it did have a purpose, what are the odds and why would it even be that important? multi lined variables are for people who are to lazy to either copy and paste the code or edit it them self's. so i'm with you on this one.
almost 5 pages too, patio, i think your a wizard.
-
On last time. Don't put multiple;e lines into a variable.
It is forbidden, not allowed, senseless, worthless and futile.
It is like trying to brush your teeth with hand soap.
Stop with the lines in a variable .
Tell us what your really need to do.
Putting lines into a variable is not what real men do. do.
Go read a book. Or get a tutor.
Wait, what? I'm not a batch developer but I know that in literally any language I've seen it's totally normal to have multiple lines in a variable, all a new line is is a special character. Your forum post has multiple lines in it and I'd hazard a guess that at some point in the rendering of this page the body of your post is loaded into a single variable in the backend of this forum.
multi lined variables are for people who are to lazy to either copy and paste the code or edit it them self's. so i'm with you on this one.
They are for people who need to handle strings that contain multiple lines. In the initial question for example they wanted to replace a single line in the input with multiple lines, this is totally standard practice in many programs. Being lazy doesn't come into it at all, besides, in fact, copying and pasting code is generally seen as the lazy way to do things - https://en.wikipedia.org/wiki/Copy_and_paste_programming (https://en.wikipedia.org/wiki/Copy_and_paste_programming)
-
@echo off
set "src=C:\path\to\source\files"
set "dst=C:\path\to\destination\files"
pushd "%src%"
for /f "delims=" %%a in ('dir /b /a-d "*.vrt" ') do (
echo Processing "%%a"
set "stop="
(
for /f "usebackq delims=" %%b in ("%%a") do (
echo(%%b
if not defined stop type "file2.vrt" & echo(
set stop=are-we-there-yet?
)
)>"%dst%\%%a.tmp"
move /y "%dst%\%%a.tmp" "%dst%\%%a" >nul
)
echo done
pause
A file should now be created for each VRT file. The redirection to the file was at the wrong level of parentheses and the move command is necessary again.
Test it on sample files first.
-
Wait, what? I'm not a batch developer but I know that in literally any language I've seen it's totally normal to have multiple lines in a variable
It's not normal in batch scripts though. :)
-
It's not normal in batch scripts though. :)
Right. Here is the fundamental way the command interpreter works:
command p1 p2 p3 ...
Batch script is for a shell tool, console e utility. One line is a full expression. Interactive work from the a console would not not normally expect multiple Lines.
Also, spaces are the default delimiter.
A batch file is not a word processor, not a database or a universal toolkit.
And yes, one could brush his teeth with hand soap. But why would he?
I am outhitting the thread. Really. I am rolling my eyes. ::)
-
Wait, what? I'm not a batch developer but I know that in literally any language I've seen it's totally normal to have multiple lines in a variable, all a new line is is a special character. Your forum post has multiple lines in it and I'd hazard a guess that at some point in the rendering of this page the body of your post is loaded into a single variable in the backend of this forum.
They are for people who need to handle strings that contain multiple lines. In the initial question for example they wanted to replace a single line in the input with multiple lines, this is totally standard practice in many programs. Being lazy doesn't come into it at all, besides, in fact, copying and pasting code is generally seen as the lazy way to do things - https://en.wikipedia.org/wiki/Copy_and_paste_programming (https://en.wikipedia.org/wiki/Copy_and_paste_programming)
Yeah, but i think it's different in batch, mainly because it was never really intended for multi lined variables, when i was talking about copy and pasting the code i was referring to pasting a code that he had already made into another file and edit it himself, instead of putting in a variable. Yeah there is a lot of languages that can do it, but it's not a good practice in batch, and there are a lot of reasons why its not such a good idea. One good reason why is the example that Geek-9pm posted above, luckily the method i posted doesn't actually create a multi lined, but mimics it's technique with single lines instead. But still, in batch i hardly find a point for this . it does nothing but make your code even more difficult to comprehend / more difficult add edits for later use / and just plane up wastes bites in your file. if you wanted to use the command for later use just copy and paste it again, what ever you wanted to change in the text, just change it with regular variables. If you needed to completely store multiple lines of code, just send it to a file and call it inside the batch file. Notice now Geek-9pm states that "it's like brushing your teeth with soap"...? It is possible but there just isn't any point because it makes things more difficult. now more powerful languages like C++ is fine, but batch can only go so far.
-
@echo off
set "src=C:\path\to\source\files"
set "dst=C:\path\to\destination\files"
pushd "%src%"
for /f "delims=" %%a in ('dir /b /a-d "*.vrt" ') do (
echo Processing "%%a"
set "stop="
(
for /f "usebackq delims=" %%b in ("%%a") do (
echo(%%b
if not defined stop type "file2.vrt" & echo(
set stop=are-we-there-yet?
)
)>"%dst%\%%a.tmp"
move /y "%dst%\%%a.tmp" "%dst%\%%a" >nul
)
echo done
pause
A file should now be created for each VRT file. The redirection to the file was at the wrong level of parentheses and the move command is necessary again.
Test it on sample files first.
Thanks!