Monday, July 23, 2018

PowerShell script to copy files based on shortcuts

From the source directory, find all shortcuts, and copy the shortcut contents to a destination directory

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language. Wikipedia

# Get all shortcuts
$shortcuts = gci "$srcDir\*.lnk"

cgi = Get-ChildItem  Microsoft
Gets the items and child items in one or more specified locations.

# skip existing dirs
if (Test-Path "$destPath") {

Test-Path Microsoft
Determines whether all elements of a path exist.

# copy
copy-item -Path "$srcPath" -Destination "$destRecreatePath" -Force -Recurse -Container -Exclude $exclude

copy-item Microsoft
Copies an item from one location to another.
But alas, without any indication of progress.

So, from some help on Stack Overflow
# xcopy prompts for is this a file/dir, no progress
# robocopy asks for admin perms on ntfs/audit attribs
# copy copies with progress %
# /z   : Copies networked files in restartable mode.
cmd /c copy /z $srcFile $destFile

Some screenshots of the full script in action






And the full source is on GitHub


End of document. Thanks for reading.

Monday, July 2, 2018

A proper wrapper for console.log with correct line number?

While writing a simple JavaScript 'class', I wanted to toggle console.logs on for development and off for production.  Normally I would create a method to wrap console.log with a debug flag, such as:

function Awesome() {
   this.debug = true;

   this.log = function(msg) {
       if (this.debug) {
           console.log(msg);    // line 6
       }
   };
   
   this.log('test');            // outputs line 6
   
   this.doStuff = function() {
       this.log('doing stuff'); // outputs line 6
   };
}

But the line number logged is always from within the log wrapper, this.log(),
which is not overly useful.
After some searching, I ran across this StackOverflow thread
Basically, instead of using a logging wrapper, use a function alias.

function Awesome() {
   this.debug = true;
   
   if (this.debug && window.console && console.log && console.warn && console.error) {
       this.console = {
           'log': window.console.log,
           'warn': window.console.warn,
           'error': window.console.error
       };
   } else {
       this.console = {
           'log': function(){},
           'warn': function(){},
           'error': function(){}
       };
   }
   
   this.console.log('test');            // outputs line 18
   
   this.doStuff = function() {
       this.console.log('doing stuff'); // outputs line 21
   };
}    

So now Awesome is awesome with logging showing the original line number,
which is helpful for debugging.

Of course, this.console could be renamed to anything, such as this.log, this.out, etc.
And you could add the log to the window namespace so it could be used by other functions, such as window.debug or window.log, etc

End of document. Thanks for reading.