|
|
|
Batch Programming Append
|
Original Message
|
Name: no3h19c17
Date: November 27, 2006 at 16:29:44 Pacific
Subject: Batch Programming AppendOS: XPCPU/Ram: 1GB RAM/ 2.8 Ghz CPUModel/Manufacturer: IBM |
Comment: I am writing a batch program that generates another batch program by utilizing the append (>>) function. I have had considerable success in program batch files in this fashion up until now.
In my current project file1.bat generates file2.bat. The problem is that when file2.bat is run, it needs to generate two config.ini files. I have not been able to find a way to basically append the append code to a file. Please see the samples below for further explanation. Samples: file1.bat @echo echo Generating export configuration. >> file2.bat @echo if exist ./%%backupfolder%%/config.ini del .\%%backupfolder%%\config.ini >> file2.bat @echo @echo USERID=%%user%%/%%pass%%@%%host%% >> ./%%backupfolder%%/config.ini >> file2.bat @echo @echo FULL=y >> ./%%backupfolder%%/config.ini >> file2.bat @echo @echo FILE="%%backupfile%%.dmp" >> ./%%backupfolder%%/config.ini >> file2.bat @echo @echo GRANTS=y >> ./%%backupfolder%%/config.ini >> file2.bat @echo @echo INDEXES=y >> ./%%backupfolder%%/config.ini >> file2.bat @echo @echo CONSISTENT=y >> ./%%backupfolder%%/config.ini >> file2.bat Sample from file2.bat after being populated from file1.bat:
echo Generating export configuration. if exist ./%backupfolder%/config.ini del .\%backupfolder%\config.ini @echo USERID=%user%/%pass%@%host% @echo FULL=y @echo FILE=%backupfile%.dmp @echo GRANTS=y @echo INDEXES=y @echo CONSISTENT=y What I need is for file2.bat to be populated as follows:
echo Generating export configuration. if exist ./%backupfolder%/config.ini del .\%backupfolder%\config.ini @echo USERID=%user%/%pass%@%host% >> ./%backupfolder%/config.ini @echo FULL=y >> ./%backupfolder%/config.ini @echo FILE="%backupfile%.dmp" >> ./%backupfolder%/config.ini @echo GRANTS=y >> ./%backupfolder%/config.ini @echo INDEXES=y >> ./%backupfolder%/config.ini @echo CONSISTENT=y >> ./%backupfolder%/config.ini Any input would be greatly appreciated on this, as I have been testing numerous methods over the past week but to no avail. If any additional clarification is need, please let me know. Thanks very much, no3h19c17
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Mechanix2Go
Date: November 27, 2006 at 22:09:35 Pacific
Subject: Batch Programming Append |
Reply: (edit)Well, a few things. This forum makes it hard to read code. You seem to unterchange ./ with .\ which is a little confusing. I would stick with .\ for compatibility. In any event, I assume you mean %backupfolder% "off the current directory". If so, you don't need the .\ because the current directory is the point of reference. You can save some typing and some clutter by turning echo off once in each bat. Try something like this for your 'populated' bat. pushd %backupfolder% @echo off > config.ini >> config.ini echo USERID=%user%/%pass%@%host% >> config.ini echo FULL=y ... :: and so on ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: no3h19c17
Date: November 28, 2006 at 16:00:17 Pacific
Subject: Batch Programming Append |
Reply: (edit)Wow, I didn't realize how cluttered that would all look. I guess I could have summarized by simply listing this general code example: File1.bat @echo @echo Hello World >> config.ini >> file2.bat When the above code is run, it generates File2.bat with the code below:
@echo Hellow World I need it to generate this code in File2.bat
@echo Hello World >> config.ini Being able to accuratly append the append code (>>) is vital to my project. I have looked at you previous reply and I don't quite see how this would accomplish what I am attempting to do.
Unless ofcourse I am missing something which is entirely possible.
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: FishMonger
Date: November 28, 2006 at 16:33:35 Pacific
Subject: Batch Programming Append |
Reply: (edit)It seems to me that creating a bat file that creates another bat file which then creates an ini file makes for a lot of wasted effort. What's wrong with tacking that first step out and just have the bat file create the ini file? An even better approach would be to use a scripting lanugage better suited for this task. My preference would be Perl, but there are several MS languages to choose from that would be better than the bat files.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: no3h19c17
Date: November 28, 2006 at 20:08:20 Pacific
Subject: Batch Programming Append |
Reply: (edit)The process flow is necessary due to the purpose and execution of the batch program. To better explain, file1.bat is lauched which creates file2.bat in the background. File1.bat is coded to prompt the user for specific login credentials depending on their input ( domain login or local server login credentials ). Those credentials entered in file1.bat are then used to launch file2.bat. File1.bat closes and the user supplies input to file2.bat that is used in the population of the config.ini files. The login credentials that are used when launching file2.bat are vital to the correct execution of the file2.bat commands. After file2.bat completes, it deletes itself and the config.ini files since the content of the files is dynamic and only good on a per run basis. The scripts will be implemented in a mixed environment where users will be using either domain and local login credentials, hence the need to be able to dynamically pass different sets of login credentials to file2.bat. Granted, I could have file1.bat and file2.bat statically created and instruct the users to only launch file1bat, but this is not "idiot proof" enough to prevent users from executing file2.bat by itself. Which would ofcourse use their current login credentials in the execution of file2.bat's commands, causing failure in most cases. I agree that a more robust scripting/programming language would be more suited for the route that this task has taken. Initially this project was started as a basic batch script with just a few lines of code but as new functionality is needed, the project has exploded into a few hundred lines of code. This project will likely be converted into a more robust language but frankly at this point I'm just darned curious as to whether or not it is even be possible to accomplish this via batch scripting or if it is in fact a limitation.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: FishMonger
Date: November 28, 2006 at 23:28:15 Pacific
Subject: Batch Programming Append |
Reply: (edit)Doing this as a learning experience is fine, but when you're ready, you may want to look at using Perl. A single simple Perl script can easily do everything you need with less effort, and if you wish, it can be written to use a GUI instead of text interface.
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Mechanix2Go
Date: November 29, 2006 at 02:43:18 Pacific
Subject: Batch Programming Append |
Reply: (edit)I think multiple >> on a single line will prove to be an exercise in futility. I would take the approach of ONE bat with appropriate branching. ::== my.bat @echo off setLocal EnableDelayedExpansion :: MAIN get input set /p user=username? set /p pass=password? set /p backupfolder? :: BRANCH go to needed section goto :labelX :label1 pushd %backupfolder% > config.ini echo %user% >> config.ini echo %password% >> config.ini echo blah=Y goto :eof :label2 pushd %backupfolder% > config.ini echo %user% >> config.ini echo %password% >> config.ini echo blah=N goto :eof ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: no3h19c17
Date: November 29, 2006 at 07:07:14 Pacific
Subject: Batch Programming Append |
Reply: (edit)Agreed, it would certainly appear that this is a "limitation" of batch scripting. Up until the fourth version of this script, it was contained to one file but given new functionality the enduser ended up being prompted for either their domain or local login password twice through out the script. I am familiar with perl along with various other languages and I agree that perl ,in particular, is certainly more suited for such a task. Thanks for the input everyone as this would have nagged me in the back of my mind for quite sometime. Once again, thanks for all the input.
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: no3h19c17
Date: December 3, 2006 at 10:03:55 Pacific
Subject: Batch Programming Append |
Reply: (edit)As an update, I figured out a way to accomplish appending the append characters in batch programming. Granted another language would be more suitable, my obsessive nature didn't allow me to let this concept go. By adding ^ just before each > character that needed to be sent to file2.bat allows these characters to be treated as normal text, hence ignoring their normal batch programing functions, which was causing the difficulties. Basic example to show proof of concept. @echo @echo Hello World ^>^> config.ini >> file2.bat The above code will now allow @echo Hello World >> config.ini to be appended to file2.bat. Thanks once again for everyone's input on this topic. I can now rest easy and put this out of my mind. :-) - no3h19c17
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: Mechanix2Go
Date: December 4, 2006 at 00:16:19 Pacific
Subject: Batch Programming Append |
Reply: (edit)Pretty slick, Slick. ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
Name: Mechanix2Go
Date: December 13, 2006 at 03:32:17 Pacific
Subject: Batch Programming Append |
Reply: (edit)Hi tony, After years of flogging the issue of echoing special chars I was pretty much convinced it couldn't be done. So it was a good catch to see the solution. ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
Name: tonysathre
Date: December 13, 2006 at 04:23:36 Pacific
Subject: Batch Programming Append |
Reply: (edit)When I was trying to figure out his problem with the script I couldn't get those extra >> characters to echo. Now I know. "Computer security." — Oxymoron
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|