topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday December 15, 2024, 12:10 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Script to input two variables and use them to perform multiple tasks  (Read 6340 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
I need a script to automate something that has become a very tedious and risk-prone procedure that I have to perform several times a day.   Worse, I am going to have to teach someone else how to do it with a “Zero” margin of error.  I am hoping someone here can offer some ideas on how to accomplish the following:

The script should ask me to input two variables to be used to perform a series of tasks that never varies
The first step would input a name (example "jimmyk") for the first variable $na1
The second would input a description (example:  "jimmywins")  for variable $nb2

(I need to use some variable name that is not likely to already exist in other scripts in case I have to join this script to interact with another script that could already be using them)

Then first procedure would be to check for any errors that would make these variables unusable.  The naming conventions require that neither of these can have any capital letters, spaces, hyphens or other special characters (%, $, #, etc.) and a maximum length of 13 characters.

Next I have to check the existing databases in “c:\mainfolder\databases” to be sure there is not already a database using the name of the 2nd variable.  

Finally, I need to check to see if there is  preexisting subfolder of the first variable containing a folder by the name of the 2nd variable.
(example:  There might be a folder C:\mainfolder\jimmyk\  but it cannot already contain the subfolder "jimmykwins".  

If any of these is conditions exist ( if the "jimmykwins" subfolder already exists in C:\mainfolder\jimmyk" or if the "jimmykwins" database exists in "c:\mainfolder\databases" or if I accidentally made the name too long or included any capital letters or spaces etc.. as mentioned above this would result in an error message and send me back to the start to change the entered data to fix the problem.

If both inputs pass all these tests I need to use those input variables to create a subdirectory named "jimmyk" within a directory "C:\mainfolder"  IF it does not already exist there.

(Example c:\mainfolder\jimmyk").      If it DOES already exist then proceed to the next step.

In the next step it should create a sub-subdirectory using the name of the second variable inside the directory of the first variable.  

(example: "c:\mainfolder\jimmyk\jimmywins”)

There are or will be other sub-subdirectories such as "jimmyloses" or "jimmyties" but never another with the exact same name.  If there is a preexisting subfolder with the name in variable $nb2 I would not want to overwrite it.  This same rule applies to the creation of the new database file later.   If any of these occur, that should be an automatic stop with an error flag.

The next step is to copy the contents of the “c:\mainfolder\master template” directory including all the files and folders contained into the newly created sub-directory
(This populates the directory “c:\mainfolder\jimmyk\jimmywins\" with all the files and folders from the master template.  Currently done using xcopy or cut and paste if I am in GUI mode.)

Next, create an empty  “MySQL” database of the correct type with the name "jimmywins" in the MySQL database folder.
(I currently use phpmyadmin to do this).  If there already exists a database by that name, I would definitely not want to overwrite it or replace it in any way.  This should have been caught back at the beginning and forced me to use some other name.

*Note:  If the automatic database creation is a "deal-breaker"   it  is not an absolute necessity for the script.  I can perform that part after all the rest is done*.*

Next step is to edit a file called "identityfile.cfg" which was copied from the "master template" into the "c:/mainfolder/jimmyk/jimmywins"  directory. I need to replace the single instance of the word "masterdb" with the 2nd variable "jimmywins" for use in identifying the correct database for the main program to use later
(This is now done by editing the name-field in the identityfile.cfg  file with Notepad++)

Finally, the last thing is to  create a hyperlink used to access that database with the format "http://www.mainfolder.com/1st variable/2nd variable"  and writing it to an existing text file "info.txt" that was copied into the newly created subdirectory "c:\mailnfolder\jimmyk\jimmywins" when all the other files were populated there.
 (Also now done with notepad ++)

I could be missing something but I think this is the procedural order I have been manually following to accomplish all the tasks done with those two variables

Currently the time needed to do all this is at least 15 minutes even for me and I have done it hundreds of times.  The chances of a typo is high enough with me but would be higher with someone else who had never done it.  A script to ensure that this procedure was followed properly at every step would be a godsend for sure

I hope everyone at DC had a Merry Christmas and a Happy New Year.  Like most   I.T.   people, I was working through the Holidays but still had time to remember how it used to be.  :)      :D

I think it is time for Santa to make another Donation to DC (In the General fund I guess) as so  many of you have helped me with problems in the past I don't want anyone to feel missed or any less appreciated.  I am not sure what happens to donations to the General fund but I assume they in some way benefit everyone.   :Thmbsup:

Final Note:  I already tried doing this with a program that "follows the mouse" to repeat all my steps pausing for me to enter the needed names at each place but it ended up taking just as long to do because I have to keep entering those variables over and over again.   That is where it is possible to make a typo that screws up everything.   I admit that I am not that good with this but "Ghost Mouse" or anything like it is definitely not an answer. :(

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #1 on: January 04, 2014, 11:38 PM »
Are you familiar with AutoHotkey at all?  The vast majority of what you want can be done easily in AHK.

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,939
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #2 on: January 05, 2014, 03:28 PM »
This little batch script should do most, if not all of your requests.




Hope it helps  :)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #3 on: January 05, 2014, 04:48 PM »
I Just got your file will be testing in a sec.  I appreciate your help.  I have tried various iterations of Perl, Sed, etc. with varying degrees of success.  If your solution works, perhaps you can tell me how to finish another project along the same lines.  I wrote the following script to create a "date-in-the-name"  directory listing from website files on our webserver. ( I Hope I properly formatted the script so it shows  ;)
   
    @echo off
    :: timestamp YYYY-MM-DD_HH-MM-SS
   for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
   set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%
   setlocal
   set currentLevel=0
   set maxLevel=%2
   if not defined maxLevel set maxLevel=2

   :procFolder
   pushd %1 2>nul || exit /b
   if %currentLevel% lss %maxLevel% (
     for /d %%F in (*) do (
       echo %%~fF >>c:\dailylist\Sites-%dt%.txt
       set /a currentLevel+=1
       call :procFolder "%%F"
       set /a currentLevel-=1
     )
   )
    popd
                     <end>


The next step would be to process the final output file (Sites+date.txt) in the "dailylists" folder on the C: drive.  The created file is a full local path directory listing showing each line as  "C:\dir1\dir2\dir3\dir4\dir5\dir6".
I am running the script at the dir4 level in the example above with a limit of 2 in order to omit everything not needed.  In the resulting file I have to perform several other steps to get what I need in the end.

Each line of the output file should be showing an active hyperlink to a website such as "http:\\www."rest-of-line.   

I have been manually using notepad++ to change the starting text from a local path to a web path by converting "C:\dir1\dir2\"    to   "http://".  dir3 is the actual name of the site starting "www.xxxx"  so it needs no changes.
dir4 is not necessary and must be removed on each line no matter what it says
Any line that is only 5 dirs. long ending in dir5, should replaced to show only the name contained in dir5 to serve as a division marker (not absolutely necessary and could be skipped)
In the last step I do a final check converting any left-over "\" to "/"

The final result is the original local directory listing of about 50+ "C:\dir1\dir2\dir3\dir4\dir5\dir6" is converted to a working list of hyperlinks showing "http://dir3/dir5/dir6"  (with those of only 5 dirs. used as divisions if possible)

There may be an easier way to do this but I have not found it. (Probably because this works and I have not had much time to experiment with automation)  Due to the precise structure of the Web-Server this is always the layout and this process always works generating a name+date.txt in about 1 second.  But it takes me another 5 minutes of editing with notepad++ to change all the lines of text to be working hyperlinks .

The generated txt file does not have the capability of clickable hyperlinks and another annoying thing is when I copy them into a word doc, they will not become active until I add a carriage return to the end of each line but so far I have not gotten Perl to work with me on automated editing (due to some of the characters "like the "/" and "\" I think) .

As   :Thmbsup: Skwire  :Thmbsup: mentioned, it might be possible to convert this whole mess into an AHK program but that is another skill (AutoHotKey) that I have downloaded but not had time to experiment with.

Thanks again..  Sorry for the long post but it is the only way to give easy to see examples and I have found they help a lot when trying to convey an unusual method of doing things.

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,939
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #4 on: January 05, 2014, 05:30 PM »
It might help you to visualize the whole procedure you have to go through to get what you need.

Don't be shy of using simple pen and paper to make a drawing using blocks containing text of the condition you want to achieve and where a line for the 'yes' and 'no' answers depart. You will get a clearer picture of the steps you need to take.

As a bonus these steps become small which makes research a lot simpler in any computer language you like to master...or should I say: 'engage with'?  ;)

AHK would by all means be the better choice than batch scripts. Batch scripts are nice for simple stuff to automatize, but you reach the limits quickly. Workarounds for those make the resulting script messy and unclear. AHK or other programming languages have more structure, a lot less limitations and make more sense in the end. Especially after you have to take a look after a year at the code you made  :D

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #5 on: January 05, 2014, 06:45 PM »

...

There may be an easier way to do this but I have not found it. (Probably because this works and I have not had much time to experiment with automation)  Due to the precise structure of the Web-Server this is always the layout and this process always works generating a name+date.txt in about 1 second.  But it takes me another 5 minutes of editing with notepad++ to change all the lines of text to be working hyperlinks .

@questorfla: Perhaps it would help to read one of your previous threads?

Command Line special needs for Batch file - Replace/rename/remove in filenames

 ;)
« Last Edit: January 05, 2014, 08:44 PM by 4wd, Reason: I\'m not on a freakin\' phone now. »

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #6 on: January 05, 2014, 08:52 PM »
See what happens when you add on a year ever time Christmas goes by?  Post?  What Post?   :'(
Since me and Santa have the same birthday I try to hide mine in the rush so  it won't count.  But.....
Where was I?  :huh:

Thanks for the repost I need to keep better tabs on my  other self. :P 
I did not check in much over the Holly Daze but that one looks like I need to read it again for the first time.

Mr. Shade.  I can do as you say with the pen and paper but it will still look the same I am afraid.  I cold post screenshots of Notepad++ while I repeatedly use the replace function but that is the issue now.  I have to repeatedly replace the same sections of a directory listing in order to convert it to a hyperlink listing.  Instead of c:\websitelocation on the server.  I  need http://"website location on the server" with all the slashes reversed  :-\

I think I better haul out my copy of autohotkey and see how it flies.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #7 on: January 05, 2014, 11:23 PM »
Hi Shades. 
I promised to report back on the test run.  Everything was ok until the xcopy command.  For what it is worth, I changed your parameters and it went through,  Not sure which one was the cause,. 
You used the /I/H/Q/R switches which is probably the norm.  I am a bit more "paranoid" (overkill or whatever)  and I usually put half the alphabet.  As in "/s/h/e/r/c/k/y/d/I/f/g"  :)  I know a lot of that is unnecessary but something in my switches must be needed.  The fail seemed to be on copying either subfolders or contents of subfolders since the main files went fine.  Once I got past that the next stop was in the database creation. 
This is just a dummy system so I only left root as the user with no password.  But it still hit an error.  I added a pause statement so I could read it and it was about the system not being able to find the stated file (ie: variable)

Changing the "create"  line for  MySQL to -u root -e "testdb"; works fine.

Also leaving the variable %flaUserNameOK%  for the database name works.   Unfortunately the name of the db needs to be the variable (%flaUserDescriptionOK%).

I thought this would simply be a matter of changing the variable name but for some reason it won't go.  If I leave it creating the Username as you have it, that works although I need to add switches to set the character-set and collation to Latin1_swedish_ci from UTF8.
But it will create the empty database.  I have been reading through the MYSQL manual   (Sorry but that is why this has taken me so long to get back to you)  but everything looks Kosher.  That one variable is the only problem and I can even echo the value of the description variable immediately before the creation of the DB and it is the correct value.  I am obviously missing something since the other variable works fine but I cant use it due to the structure of the server.  The inputted description is what should be used for the database creation.  I think I can handle the addition of the default characterset and default collation after the DBs created if necessary but it looks like it can be done during creation too.

I MUST say that your structured formatting for each process has given me a new way to look at these problems as modules rather than trying to do everything in one chunk.  Much easier to troubleshoot for sure. :Thmbsup:

Any ideas on the variable problem would be appreciated.  The multiple switches I added on the Xcopy command have a lot of unnecessary actions but a couple of them (probably /S/E ) were all I needed to add) to get the contents of the subfolders and files

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #8 on: January 06, 2014, 12:40 AM »
Instead of c:\websitelocation on the server.  I  need http://"website location on the server" with all the slashes reversed  :-\

Which is solved at the above post I mentioned ... no AutoHK required  ;)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #9 on: January 15, 2014, 07:04 AM »
Thanks 4W for the pointer to look back at what had already bee answered.  A little bit of this and touch of that and lo and behold the whole thing works!  That was the middle part of the 3 steps.  It fit like a glove.  I might be better off with AHK but it is more fun doing it the old way.  Like back in the COBOL days :).  Anyway, I might post the whole project now that it is finally complete and tested several times.  Works perfectly! :Thmbsup: (I doubt anyone but me would ever need it!)  :huh:

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Script to input two variables and use them to perform multiple tasks
« Reply #10 on: January 15, 2014, 07:15 AM »
While I m passing out Thanks, I should also thank Mr. Shades for his input on the command line sequences for MySQL database creation.  :Thmbsup: I have run the completed script several times and found no problems in creation.  As soon as I get a spare hour or two for loading one, it should make life a lot simpler for me.  Unfortunately, it probably would not be of much use to anyone else as I am sure no one would need it to do things quite like this.  :huh: It is a Rube Goldberg setup for sure!!  I will also post back the completed sequence.  You may spot something I am missing in the way I managed to get the script to create using the variables I wanted.