There are a bunch of files, and the name is like this: [abcd]2008.01.01. To test. txt, intend to remove the closure, change to this appearance: 2008.01.01.txt. In Linux should be very simple, a big deal to write a script is done, but I want to do it under Windows. Always wanted to study PowerShell, this time to use this tool to achieve my purpose.
Behold the toss for two days, incredibly still not done, but PowerShell a little primer some. The PowerShell is really powerful, and it's a lot different from the usual script. The fundamental point is that all of its cmdlet outputs are objects, and this concept may not be easy to understand at first, but it will find its flexibility when you have a thorough understanding. Unlike other scripts where the command output is a string, the command after the pipe can only handle the previously exported string, the PowerShell cmdlet outputs objects, and the pipeline can work with the rich information of the object. For example, dir (in fact, is the Get-childitem command), output is a bunch of files or directory objects, the file is System.IO.FileInfo object, the directory is System.IO.DirectoryInfo objects, these are standard. NET objects, so you can use the properties and methods of these objects and have an advantage if you are familiar with. net/c# programming. For example, if you want to list read-only files for the current directory, you can do this:
Dir|where-object{$_. IsReadOnly}
Listing the directory is this:
Dir|where-object{$_. Psiscontainer}
If you want to know how many files are in the directory
Get-childitem|where-object{$_. psiscontainer}|foreach-object{"$_.fullname:" + $_.getfiles (). Length}
As I said, the output of PowerShell is all objects, and get-member can know what each item of output is and what attributes and methods the object has.
In other words, PowerShell also has a puzzling place, with other Microsoft products seem different style, PowerShell and a bit like *nix things, a lot of dazzling abbreviations and Windows SDK long function name in sharp contrast, like LS, CP these * Nix under the command incredibly can also use, of course, just alias, let people shoddy merchandizing tongue is almost every built-in command has alias, and some more than one alias, such as Get-childitem alias GCI, ls, dir Three, and? is the alias of Where-object,% is the alias of Foreach-object, these I am afraid will let the beginner quite puzzled. Look at the previous example, take the number of files in each subdirectory of the current directory, and if you change to a simple way, this is the following:
Ls|? {$_. Psiscontainer}|%{$_.fullname: "+ $_.getfiles (). Length}
Does it look like it's a spell with Perl? Look at the example below, and there's the taste of awk, which is the number of files for all subdirectories (there may be a simpler way, of course, just for example):
Ls|? {$_. psiscontainer}|%{$count =0}{$count +=$_.getfiles (). length}{"Total Files:" + $count}
After this example, I seem to have some understanding of PowerShell, a bit of admiration for Microsoft, PowerShell is indeed a powerful scripting language.
Well, gossip less, the first request has not been resolved. At first I used the Rename-item, who knows the other side brackets not too cold, toss a half-day also did not fix, always reported such a mistake:
PS f:/temp> rename-item ' f:/temp/' [ten] ' F:/TEMP/10 '
Rename-item: The object under the specified path f:/temp/' [10 '] does not exist.
Location: 1 Characters: 12
+ rename-item <<<< ' f:/temp/' [ten] ' F:/TEMP/10 '
Until a certain moment online search can use Move-item, only finally, all kinds of octyl will not, in short this is a rename-item bug, do not support square brackets, can only use Move-item. Pour ~
The final result is
LS *.avi|%{if ($_.fullname-match ' (. *)/[.*/] (/d/d/d/d/./d/d/./d/d). *avi ') {Mv-literalpath $_.fullna
Me $ ($matches [1]+ $matches [2]). avi "}}
Or that sentence, practice is the best way to learn knowledge, especially when you encounter bugs. It seems that my next interview will also be able to write "proficient PowerShell" ah, roar.