The source of this article: http://www.cnblogs.com/wy123/p/6129498.html
Due to work needs, processing a batch of documents, this would like to write C # to deal with, and later think this is the bounden duty of PowerShell,
Simply on-line various search, all kinds of demo, a variety of changes, spent half a day, and finally put together to achieve the effect.
PowerShell is not familiar with itself, there may be some detours, the implementation of the more rotten, in short, a variety of small functions pieced together, and finally pieced together to achieve the effect.
The following is the original demo, relatively naïve, right when it is the notes of their own records
1, File information statistics
Below is a folder with six text files, the folder below the file name to a log file
The file information under this folder is counted, there is a file with a specified path, the file is generated according to the time.
The following is the generated statistics file information
If you want to count the full path of the file, use FullName directly, $fileName = $file. FullName can get the full path of the file.
If you want to facilitate the contents of all the files and subfolders under the folder under a certain path, you can add the-recurse parameter, that is: $files = Get-childitem "F:\TestPowerShell\" -recurse
This is the result of the record.
If you want to count a certain type of file, you need to add the-include *.txt parameter to specify the suffix name, namely: $files = Get-childitem "F:\TestPowerShell\"-recurse-include *.txt
2, file operation (Copy)
If the folder and subfolders are copied to another path, create a new folder here, named after the date and time
After running the script, copy the files and subfolders under the specified folder to another directory.
If you want to copy files under one path (or multiple paths) under all (including subfolders) to a folder
Easy to copy files to the destination folder
foreach ($file inch$fileList) {#判断是否是文件, if it is a file, the Copy Value destination folderif($file - is [System.IO.FileInfo]) {#Copy文件 Copy-Item $file. Pspath $filepath \ $destinationDir #记录copy的日志信息 $file. FullName|Out-File$filepath \ $logfilename-Append}}
effect (to show the effect, I have the name of the file in the subfolder to prevent overwriting by the renamed file)
The recorded statistics are as follows
Finally, attach a completed script for the above operation
Cls$filepath="C:" $name=(Get-date). ToString ("Yyyymmddhhmmss") $logfilename=$name+'. txt'#创建日志文件New-Item-ItemTypeFile -Path $filepath \ $logfilename-force# Destination folder name $destinationdir=$name # Create a destination folderif(! (Test-Path $filepath \ $destinationDir)) {New-Item $filepath \ $destinationDir-type directory} #输出F: \ file name for all files $filelist=Get-ChildItem F:\TestPowerShell-Recurseforeach ($file inch$fileList) {#判断是否是文件, if it is a file, the Copy Value destination folderif($file - is [System.IO.FileInfo]) {#Copy文件 Copy-Item $file. Pspath $filepath \ $destinationDir #记录copy的日志信息 $file. FullName|Out-File$filepath \ $logfilename-Append}}
3, File content operation
The following is the content of the text file, if you want to read the contents of the specific file, you can get-content $file. Pspath This command to read the contents of the text file
The following is the effect of the read out
Here is the complete processing script for the above operation
Cls$files=Get-ChildItem "F:\TestPowerShell"-Recurse-Include*. Txtforeach ($file inch$files) #遍历文件 {$linenumber= 0#获取当前文件的内容 $content=Get-Content $file. Pspath #打印出来文件名称 Write-Host "******************the Write-Host $fileWrite-Host "******************the #遍历文件的每一行信息 foreach ($lineinch$content) {#打印出来文件的每一行信息 Write-Host $line #这里当然可以继续处理, such as continue to do other processing for the current line of information}}
So far, the file can be counted, can be moved, can read out the specific content,
Many of PowerShell's system functions are similar to T-SQL or C #, and can be used to enrich the content of the file with system functions.
The downward work is not said, can do a variety of processing to meet the demand.
Summarize:
The features of PowerShell are certainly not just these, and the above is just the tip of the iceberg of PowerShell, bucket, only with its most basic functions to meet its needs,
Because there is not much to PowerShell, there may be a lot of system commands to achieve the above functions, the above should be more simple and more convenient implementation, here more than a few lines of code, is a familiar process.
Wild Path Origin PowerShell file Operation utility