2012 Scripting Games Beginner Event 3: Create a File in a Folder

2012 Scripting Games Beginner Event 3: Create a File in a Folder

  • Comments 22
  • Likes

 2012 Scripting Games badge

Summary: Beginner Event 3 of the 2012 Scripting Games requires you to create a file in a folder that may or may not exist.

About this event

Division

Beginner

Date of Event

4/4/2012 12:01 AM

Due Date

4/11/2012 12:01 AM

 

Event scenario

You are a local user, and you want to document the processes that are running on your computer. You decide that you would like to list the name and process ID of each process running on your computer. You want to save this information in a nested folder. Because you use a number of different computers, you are never certain that the folder will exist. The folder is nested off the root directory in the following location:

C:\2012SG\event3

The following image illustrates the folder/file arrangement.

Image of folder

The image that follows illustrates the contents of the Process3.txt file.

Image of command output

Design points

  • If you do not have permission off the root, create the nested folder where you have permissions. Keep in mind that the folder MUST be nested, and it must be named 2012sg\event3. The file must be named Process3.txt.
  • Your code should check to see if the folder exists. If it does not exist, it should be created, and then the Process3.txt file should be created. If it does exist, the Process3.txt file should be created.
  • Your script should not generate errors (that are ignored), even if it does create the Process3.txt.
  • Your script does not have to have command-line parameters, but if you want to include them (with a default path that solves this problem), you will gather additional points.
  • If you choose to add command-line parameters, you should include comment-based Help that describes the parameters, and a sample example of use.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Good luck as you compete in this year’s Scripting Games. We wish you well.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I've put something together that does this fairly well I think but am thrown off a little by the Design Point stating extra points for Command Line Parameters.  Not sure what is meant by this?  Is it safe to assume the systemdrive is always 'C:'?

  • Never assume anything.  blogs.technet.com/.../do-not-assume-anything-in-your-powershell-script.aspx

  • If you do not have permission off the root, create the nested folder where you have permissions.

    For this one does this mean we need the script to check to see if permissions are there and then write to an alternate location, or is this something we should determine before writing the script?

  • Thanks @Chris.  Amended Script without that assumption now.

    @Michael I'd be interested to know the answer to this too.  I'm not sure if the Design Point means the script must check for permissions or that it is acceptable for us to make the script write elsewhere if we don't have access to the root of the system drive.  I figure the former is more likely but don't want to work on how to do that until it gets confirmed.  :)

  • @Mr Killian Yeah, it seems odd to me that the script could be written to be inconsistent.  On one computer your ID could have access to the root drive, on another it may not.  As the person running the script I would not want to be guessing on each system where the file went.  In this scenario I'm not quite sure what to provide for this part.

  • I think a permission check would be a little bit to difficult for Day 3 in the beginner class ;-)

    As i understand it, if there is no access to C:, just created it on drive d: or elsewhere, as long as the structure says ...2012sg\event3

  • @Timo  I understand it the same as you. I'll wait for clarification just to be sure :)

  • @Killian Yes, it is safe to assume (for this scenario) that the system drive is always c:\. The command line parameter portion, could be as simple, as having a parameter that accepts a path for where to create the log, or a name for the log itself. This would allow you to use this script in other scenarios. But if you do this, keep in mind your submission MUST have default values that meet the needs of the scenario.

  • @Chris Seiter ... this is a great point. But for this scenario, your submission can assume that the c:\ drive is the system drive. I do not want the scripts to have to be TOO complicated, or to have to do TOO much error checking.

  • @Michael Moore no. You do not need to check for permissions off the root. If in your testing you do not have permission to the root, then use your MYDOCUMENTS folder. In fact, there is a great Scripting Wife article that talks about how to do this.

  • @Michael Moore, @Mr. Killian I did not want you to have to do the permission check because as @Timo points out I do not want the event to be too hard for beginners. If this were an advanced event, then I would require the permission check, and also require the check for determining where systemroot resides. But this IS a beginner event, and I do NOT want it to be too difficult.

  • @Timo @DawnV I put the line in there about if you do not have permission, simply because I realise that some people may be running with NON-ADMIN rights, and not have permission to the root. Therefore I did not want them to think they could not do the event. It is not my intention to clutter a beginner event with permission checks etc.

  • If the Process3.txt file exists do you want it appended or overwritten?

  • Got so giddy about submitting that I forgot to change the event.  My Beginner 3 is in Advanced 1.

  • @Paul Cunningham if the file exists it should be appended to ... but if you choose to overwrite the file that is fine as well because I did not specify that in the design guidelines.