本帖最后由 flashercs 于 2024-12-7 00:23 编辑
先测试,后投产,防止丢文件.我觉得分割后应该删除源文件的,否则500G变成了1TB,太占用磁盘.但 删除文件很危险.
Q1- @echo off
- set "list=.\清单.txt"
- set "dir0=C:\D\Test\ps1\要处理的文件夹"
-
- for /f "usebackq tokens=1* delims=+" %%A in ("%list%") do set "?%%~A=%%~B"
- for /f "delims=" %%A in ('dir /a-d-h /b /s "%dir0%"') do (
- if defined ?%%~nA (
- setlocal enabledelayedexpansion
- for %%B in ("!?%%~nA!") do (
- endlocal
- ren "%%~fA" "%%~B%%~xA"
- )
- )
- )
- for /f "delims=" %%A in ('dir /ad-h /b /s "%dir0%"^|sort /r') do (
- if defined ?%%~nxA (
- setlocal enabledelayedexpansion
- for %%B in ("!?%%~nxA!") do (
- endlocal
- ren "%%~fA" "%%~B"
- )
- )
- )
-
- pause
复制代码 Q2- @echo off
- set "file=文件夹名"
- set "dir0=C:\D\Test\ps1\要处理的文件夹"
-
- for /f "delims=" %%A in ('dir /ad-h /b /s "%dir0%"^|sort /r') do (
- if /i "%%~nxA"=="%file%" (
- move "%%~fA\*" "%%~dpA"
- )
- )
-
- pause
复制代码 Q3 分割- <#*,:
- @echo off
- cd /d "%~dp0"
- set "batchfile=%~f0"
- Powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
- pause
- exit /b
- #>
- $srcDir = "." # 要处理的文件夹路径 - 文件夹
- $filePattern = "*.db" # 要分割的文件类型
- $fragSizeRange = "40-50" # 文件分割容量范围,单位:MB
- $dstdir = ".\拆分后的目标路径" # 指定拆分后的文件存放路径
- $format = '{0}+{1:000000}{2}' # 拆分后的文件名格式
-
- $didst = [IO.Directory]::CreateDirectory($dstdir)
- $dirPath = $didst.FullName
- $buff = New-Object 'byte[]' -ArgumentList 80kb
-
- function Get-RandomSize {
- [double]$lo, [double]$hi = $fragSizeRange -split '-'
- $lo = $lo * 1mb
- $hi = $hi * 1mb
- if ($lo -eq $hi) {
- $m = $lo
- } elseif ($lo -gt $hi) {
- $m = Get-Random -Minimum $hi -Maximum $lo
- } else {
- $m = Get-Random -Minimum $lo -Maximum $hi
- }
- $m
- }
- Get-ChildItem -Path ("$srcDir\*" -replace '[\[\]]', '`$&') -Filter $filePattern -Recurse | Where-Object { $_ -is [IO.FileInfo] -and $_.FullName -ne $env:batchfile } | ForEach-Object {
- try {
- $_.FullName
- # $dirPath = [IO.Path]::GetDirectoryName($_.FullName)
- $ctr = 1
- $fragSize = Get-RandomSize
- $fragName = $format -f $_.BaseName, $ctr, $_.Extension
- $fragPath = [IO.Path]::Combine($dirPath, $fragName)
- if ($_.Length -le $fragSize) {
- $_.MoveTo($fragPath)
- return
- }
- $stream1 = New-Object System.IO.FileStream -ArgumentList ($_.FullName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read, 80kb)
- $stream2 = New-Object System.IO.FileStream -ArgumentList ($fragPath, [IO.FileMode]::Create, [IO.FileAccess]::Write, [IO.FileShare]::None, 80kb)
- $totalSize = 0
- while (($readCount = $stream1.Read($buff, 0, $buff.Length)) -gt 0) {
- $stream2.Write($buff, 0, $readCount)
- $totalSize += $readCount
- if ($totalSize -ge $fragSize) {
- $stream2.Close()
- if ($stream1.Position -ge $stream1.Length) {
- break
- }
- $totalSize = 0
- ++$ctr
- $fragSize = Get-RandomSize
- $fragName = $format -f $_.BaseName, $ctr, $_.Extension
- $fragPath = [IO.Path]::Combine($dirPath, $fragName)
- $stream2 = New-Object System.IO.FileStream -ArgumentList ($fragPath, [IO.FileMode]::Create, [IO.FileAccess]::Write, [IO.FileShare]::None, 80kb)
- }
- }
- $stream1.Close()
- $stream2.Close()
- # 删除原文件
- # $_ | Remove-Item
- } finally {
- if ($stream1) { $stream1.Close(); $stream1 = $null; }
- if ($stream2) { $stream2.Close(); $stream2 = $null; }
- }
- trap {}
- }
复制代码 Q3 合并- @echo off
- @REM 要处理的文件夹
- set "dir0=c:\d\test\分割"
- @REM 要合并的文件类型
- set "pattern=*+*.db"
- @REM 指定合并后的文件存放路径
- set "dir1=c:\d\test\合并"
-
- md "%dir1%" 2>nul
- for /r "%dir0%" %%A in ("%pattern%") do (
- for /f "delims=+" %%B in ("%%~nA") do (
- if not defined ?%%~dpA%%~B (
- set "?%%~dpA%%~B=1"
- copy /b "%%~dpA%%~B+*%%~xA" "%dir1%\%%~B%%~xA"
- )
- )
- )
-
- pause
复制代码
|