ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Script to input two variables and use them to perform multiple tasks

(1/3) > >>

questorfla:
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:
Are you familiar with AutoHotkey at all?  The vast majority of what you want can be done easily in AHK.

Shades:
This little batch script should do most, if not all of your requests.




Hope it helps  :)

questorfla:
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:
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

Navigation

[0] Message Index

[#] Next page

Go to full version