Skip to content

Instantly share code, notes, and snippets.

@itskaia
Created September 4, 2019 18:18
Show Gist options
  • Save itskaia/3e2924494ac7831858cf9cddb2d94b1c to your computer and use it in GitHub Desktop.
Save itskaia/3e2924494ac7831858cf9cddb2d94b1c to your computer and use it in GitHub Desktop.
September 2019 - Week 1 - Scripting Mini-Project

September / Week 1 Mini-Project

Guide / Help Notes / Resource Notes


Script Requirements...

# Write a script that searches for all image files starting with 'Screen Shot' and ending with .png.
# Have the script move all such files into a new directory in your Documents directory
# Assign the new destination folder a standardized name that includes an ISO 8601 date/time stamp.
# Have the script zip/compress the screen shot image folder.

Snippets to work with...

path = 'folder/to/zip' # Replace this with your folder path
zipped = 'whatever.zip' # Replace this with your zip name
zip_succeeded = system("zip -r #{zipped} #{path}") # true if succeeded


Discussion about file paths...

Me: What's the practice for determining how precise you need to be when specifying a directory variable in a script for moving items?
Roger:
GOOD QUESTION!
By default every path will be RELATIVE to the directory you ran the script FROM
UNLESS you call a method which returns an absolute path (like Dir.pwd)
  • Where to run the script from

  • What privileges does the script need (sudo?)

  • I think of it that there are 3 kinds of paths (I'm only talking strings right now)

    • relative path (for example, 'assets/some-file.txt'). Relative to where script was ran from
    • absolute path (for example, /etc/foo/bar.txt). Starts with a /, explains exactly where file is
    • script-relative path (for example, "#{File.dirname(FILE)}/foo/bar.txt"). This will look for a file RELATIVE TO wherever some-script.rb lives
    • Script-relative paths always look like: "#{File.dirname(__FILE__)}/#{relative_path}" where relative_path is the path relative to the script you are running
  • So it depends what you are acccessing.

    • relative paths - for user files
    • absolute paths - for system and global files (logs and stuff)
    • script-relative paths - for files that you provide along with your script (default config files, template files, etc. Best to treat script-relative files as read-only)
    • Relative and absolute are important to understand. Script-relative is more like a technicality
    • Relative - no slash at beginning. Absolute - slash at beginning Every language has their own way of specifying script-relative paths (relative and absolute are given)
  • __FILE__ is the path to the currently running script. File.dirname(path) takes a file path and strips off the filename at the end. File.dirname(__FILE__) therefore gives you a path to the directory that the currently running script lives in.

@itskaia
Copy link
Author

itskaia commented Sep 4, 2019

(Roger in this case is my professional mentor who's working with me on my learning j o u r n e y )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment