@echo off
rem writen by pcl_test
rem bat/ps1通用
rem 测试前先备份源文件或文件夹
type "%~f0"|powershell -noprofile -sta "-"
>nul ping /n 6 0&exit
#>
$global:files=@();
$ListFile={
$global:files=@();
$button2.Enabled = $false;
$button2.Text = '获取文件中';
if($radioButton1.Checked){
$global:files=FileDialog;
}else{
$global:files=FolderDialog;
}
$textBox2.Text=$files.Count;
if($files.Count){
$textBox3.Text=$files -join "`r`n";
$textBox3.Enabled = $true;
}else{
$textBox3.Text='测试前先备份源文件或文件夹';
$textBox3.Enabled = $false;
}
$button2.Text = '选择文件或文件夹';
$button2.Enabled = $true;
}
$EditFile={
switch($comboBox1.Text)
{
'UTF-8'{$outenc=[Text.Encoding]::UTF8;}
'Unicode(LittleEndian)'{$outenc=[Text.Encoding]::Unicode;}
'Unicode(BigEndian)'{$outenc=[Text.Encoding]::BigEndianUnicode;}
Default{$outenc=[Text.Encoding]::Default;}
}
if($files.Count){
$button1.Enabled = $false;
$button2.Enabled = $false;
$button1.Text = '处理中';
for($i=0;$i -lt $files.Count;$i++)
{
$tmp=ClearExtra $files[$i];
OutFile $files[$i] $tmp $outenc;
if([int]$textBox2.Text -gt 0){$textBox2.Text=[int]$textBox2.Text-1;}
[System.Windows.Forms.Application]::DoEvents();
write-host $files[$i];
}
$button1.Text = '执行';
$button1.Enabled = $true;
$button2.Enabled = $true;
[void][System.Windows.Forms.MessageBox]::Show('Done','提示');
}
}
function CheckExt
{
$ext=@();
if(($textBox1.Text.Trim() -eq '') -or ($textBox1.Text.Trim() -notmatch '^\*\.[a-z\d]+(;\*\.[a-z\d]+)*$')){
[void][System.Windows.Forms.MessageBox]::Show('指定扩展名不能为空或非法');
}else{
$ext=($textBox1.Text -replace '\s*','').split(';',[StringSplitOptions]::RemoveEmptyEntries);
}
return ,$ext;
}
function FolderDialog
{
$filelist=@();
$ext=CheckExt;
if($ext.Count){
$fbd = New-Object System.Windows.Forms.FolderBrowserDialog;
$fbd.RootFolder = 'MyComputer';
$fbd.ShowNewFolderButton = $false;
$Show = $fbd.ShowDialog();
If ($Show -eq 'OK'){
$fd=$fbd.SelectedPath;
dir $fd -r|?{($_ -is [System.IO.FileInfo]) -and ($ext -contains ('*'+$_.Extension))}|%{
$filelist+=(,$_.FullName);
}
}
}
return ,$filelist;
}
function FileDialog
{
$filelist=@();
$ext=CheckExt;
if($ext.Count){
$ofd = New-Object Windows.Forms.OpenFileDialog;
$ofd.InitialDi