Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell : the power of REPL, -WhatIf and ScriptBlocks as Parameter when moving files

One of the things I like most about PowerShell is how you can use the interactive commandline, commandbuffer and REPL (Read - Evaluate - Print - Loop) to quickly construct a solution, especialy with the -WhatIf parameter.  

See this IRC chat (www.PowerShellLive.com).

[21:54] <frb-work> zz_mow_zz: generalhan wants to take each file in a ls -r -inc [a-z]* and move it to a dir based on the first letter of the filename

[21:56] <generalhan> so all the files that started with A would go to \path\to\folder\A's .... and all the files starting with B would go to \path\to\folder\B's
[21:57] <zz_mow_zz> like this ?
[21:57] <zz_mow_zz> dir | Move-Item -WhatIf -Destination {'C:\dir\' + $_.name[0] + '\' + $_.name}

Took me one minute using this technique to work out an example, and a couple minutes later the task was done

[22:04] <generalhan> holy smokes !!! perfect !!!
[22:05] <generalhan> ls -rec -include [a-z]*.wav -exclude No-Number* | Move-Item -Destination {'c:\' + $_.name[0] +'\' + $_.name} -WhatIf

The last part of my History :

21 dir
22 dir | Move-Item -WhatIf -Destination {$_.name[0]}
23 dir | Move-Item -WhatIf -Destination {$_.name[0] + $_.name}
24 dir | Move-Item -WhatIf -Destination {'C:\dir\' + $_.name[0] + '\' + $_.name}

wow I love this shell ;-)

For more information about the method used using a scriptblock as paramter a see the following entry on my former blog, that gives more detailed information about this :

/\/\o\/\/ PowerShelled: PowerShell : Advanced renaming of files .

 

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, May 08, 2007 3:33 PM by admin

Comments

# Hey, PowerShellGuy ! : I have to move files based on date now too !

The Chat did gone on while I made Last entry PowerShell : the power of REPL, -WhatIf and ScriptBlocks

Tuesday, May 08, 2007 4:58 PM by The PowerShell Guy
Anonymous comments are disabled