eTechtips

Tech Tips and Insights

  • DOS: Capture return values from commands

    When I am writing build automation scripts, there are times that I have to insure that the previous command completed
    with no errors. I have found that ERRORLEVEL gets filled in with the return value from a command.

    dir %1
    
    if (%ERRORLEVEL%) == (0) echo Found File: %1
    if (%ERRORLEVEL%) == (1) echo Missing File: %1
    

    So if you save the previous in a file and run the command with one (1) argument, it will display the output of the dir command
    but also print either “Found file: ” or “Missing File: “.

    0 Comments
  • 3DSMax: Remove background image

    So in 3DSMax it is sometimes useful to insert a background image usually when you are modelling
    something and using the background image as a guide. There is no easy way to remove it. You
    can reassign another image or set it to not display the background image, but this can lead to
    problems if the file is moved or removed as it is still linked to your max file.

    MaxScript to the rescue! You can use the MaxScript Listener(F11) to type in the following to
    clear the background filename:

    backgroundimagefilename=""
    

    This will clear the entry for the filename. I have noticed that you do need to exit 3DSMax to
    fully make the change complete.

    0 Comments
  • Python: error when using the input() function

    I was writing a basic guessing game and when I tried to take user input I was
    getting the following:

    Code:

    guess = input()
    d
    

    Error:

    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 1, in 
    NameError: name 'd' is not defined
    

    The problem is actually with the version of Python that I was using. If you
    are using Python 2.x, you need to use the raw_input() function. And with 3.x
    you can use the input() function.

    0 Comments
  • Python: if/else Statements

    There comes a time when you are writing a program and you need to branch based on input to a program.
    This is the format of the if/else statement in Python.

    if <some true/false statement> :
      <some statements that run if the if statement is true>
    else:
      <some other statements that run if the if statement are false>  
    

    There may be times when you have more than one value set to test against. In that case you can write a many if/else
    statements but you can chain the if/else statements with an elif. So the chain becomes if/elif/else.

    if x < 5:
      <some statements that run if the if statement is true>
    elif x > 5 and x < 10:
      <some statements that run if the elif statement is true>
    else:
      <if none of the if checks are true, run the statements in the else case.>
    
    0 Comments
  • Python: Allow for empty if else blocks

    I was toying with a prime number finder and was creating a set of if/else statements for a horribly failed attempt
    and wanted to skip to the next number on any of the if statements that were true. I didn’t want to put an empty print
    statement as that would disrupt the output I was expecting from the program. I found the pass statement.
    Here is an example from my not very successful attempt at finding primes:

    if inp > 2 and inp % 2 == 0:
      pass
    elif inp > 3 and inp % 3 == 0:
      pass
    elif inp > 7 and inp % 7 == 0:
      pass
    elif inp > 9 and inp % 9 == 0:
      pass
    else:
      print inp, " is a prime"
    

    Yes, this does not accurately find the primes, but it would skip any number that is divisible by 2,3,5,7, or 9. And
    anything that does not match this will be listed as a prime, this is incorrect and would be made to add new elif
    statements as each prime was found.

    0 Comments
  • C: Check if string is a number

    I was recently testing some basic input and wrote this code to test if the input was a number or not.

    #include 
    
    int main()
    {
      char name[10];
    
      scanf("%s",&name);
      if (checkifNumber(name))
      {
         printf("Is a numbern"); 
      }
      else
      {
         printf("Invalid numbern");
      }
    }
    
    int checkifNumber(char *inp)
    {
      int i=0;
      int isanumber = 1;
    
      while(inp[i] != '' && i < 10)
      {
        if (inp[i] >= '0' && inp[i] <= '9')
        {
        }
        else
        {
          isanumber =0;
        }
        i++;
      }
      return isanumber;
    }
    

    One thing that could be improved is to potentially use a global variable to define the length of the
    input string as it is used in two different places in the program and could potentially lead to an
    array overrun/underrun.

    0 Comments
  • Dos: Adding commands to the PATH

    When you install a new piece of software that does not add itself to the Windows PATH variable, you may find your self
    needing it to be run from a command prompt. You could type the full command path each time you run the executable, but
    it may be easier to add the directory to the PATH variable so that it is always available.

    To add it for the current command prompt:

    set PATH=%PATH%;"C:Program FilesSometool"

    This will add the variable to the current command prompt environment but will go away once you close the window and will not
    be available to other prompts unless you type it in again.

    To add it to the Windows Environment, you can use the My Computer->Properties menu to select the “Advanced System Settings”
    in Windows 7 to then select the “Environment Variables…”. Now the choice you have is to add it to the System variables or
    the User variables for.

    If you choose the System varables you will be adding it to the system as a whole and any other user who logs in will have
    the updated path. If you add it to the User variables, then it is only available to your user.

    Whichever you choose, select the Path entry and click the “Edit…” button. This will pop up a dialog that will let
    you add to the “Variable Value” entry. Now you will need to decide whether you want your new path at the beginning, end,
    or somewhere in the middle. You might add it to the beginning if it is being used to override an existing command. You
    may add to the end if there is no conflict with other installs. And finally, you may need to choose the middle if there
    are some commands you need to override but others that need to be left alone.

    Entries are separated by the semi-colon “;” character in DOS.

    0 Comments
  • Perl: Format date strings

    The other day I had a requirement to fill in a date value while creating an entity in ClearQuest. The field that was required was a date and was formatted as “mm/dd/yyyy hh:MM::SS AM|PM”. I used the localtime function to return the date and used sprintf to format the date to the proper output.
    The reason I used sprintf was that for hours, months and days less 10 the output was 1 digit instead of two and the input was not allowed. The %02d states that the digits used will be a minumum of 2 in this case.

    Here is the sample program I wrote to test the expected output:

     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    
    # formatted just by concatenating output
    my $datestring = $mon . "/" . $mday . "/" . $year . "  " . $hour . ":" . $min.
    ":" . $sec  ;
    
    # formatted using sprintf to ensure proper digit counts are output
    my $datestring2 = sprintf("%02d/%02d/%04d %d:%02d:%02d %s",$mon,$mday,1900 +$yea
    r,$hour,$min,$sec, $hour >= 12? "PM" : "AM");
    
     print $datestring;
      print $datestring2;
    
    
    0 Comments
  • Using Rsync to backup a directory

    Recently I was moving data between an old server and new web server. I had some 250 GB of data and thought about just using Secure Copy (scp). When I started the process, I walked away
    thinking “No Problem”, but after a few files, I got an error due to file permissions and I did not want to figure out what files had not been copied yet or where to try to restart.
    So I used rsync to mirror the drive. I was able to set it up and due to my slow network speed, it still took several days, but I was certain that I had gotten all the data.

    rsync -azuv -e ssh  user@originserver:public_html/* public_html/
    

    The options I used were for :

    Option Description
    -a Archive Mode
    -z Compress File Data
    -u Update Mode
    -v Verbose
    -e specify command to run

    This took some time to run, but I did not have to babysit the command and it duplicated the data from my old server to the new one.

    0 Comments
  • XML: Characters that require replacement

    I was generating some html based e-mails and due to some changes I made in the text, my mail stopped being sent. I found that it was not liking some of the characters that were being placed in my generated emails. I tried some of the standard replacemnt methods, but found that the XML file was the issue. The Greater than(>) and less than (<) and the Amersand(&) and the apostrophe(') and the quote(") all have special meaning to XML and can cause these issues. Once I replaced the following characters in my email variable, my e-mails began to flow again. Here is a table of the characters and their XML representations that can be used for replacement:

    Name Character XML replacement
    Ampersand & &
    Apostrophe ' &apos;
    Greater Than >r &gt;
    Less than < &lt;
    Quote " &quot;

    This is by no means the complete list of replacements. This is only that characters that were needed by the calls to Java Mail that my program was using.

    0 Comments