本帖最后由 lj670 于 2024-11-23 17:20 编辑
文件夹3内有若干mp3文件,根据“00.txt”文本列出的多个mp3文件名(每首一行,没有后缀名),从文件夹3移动至文件夹4内,如果有没找到的文件则创建一个"缺少.txt"文本并记录,我有一个脚本如下,可以根据文本移动文件,但对缺少的文件不能准确记录,求助各位大佬,谢谢! | | | | | | | | | | | | | | | | | | | | | $txtfile="00.txt"; | | $oldfolder="C:\测试\3"; | | $newfolder="C:\测试\4"; | | | | if (-not (test-path -liter $txtfile)) { | | write-host ('"'+$txtfile+'" not found'); | | exit; | | }; | | if (-not (test-path -liter $oldfolder)) { | | write-host ('"'+$oldfolder+'" not found'); | | exit; | | }; | | if (-not (test-path -liter $newfolder)) { | | [void](md $newfolder); | | }; | | | | $dic = New-Object 'System.Collections.Generic.Dictionary[string,int]'; | | $text = [IO.File]::ReadAllLines($txtfile, [Text.Encoding]::GetEncoding('GB2312')); | | | | for ($i = 0; $i -lt $text.count; $i++) { | | $key = $text[$i].toLower(); | | if (-not $dic.ContainsKey($key)) { | | $dic.Add($key, 0); | | } | | } | | | | | | $filesInFolder = @(dir -liter $oldfolder -Filter "*.mp3" |? { $_ -is [System.IO.FileInfo] }); | | | | | | $missingFiles = @(); | | | | | | foreach ($line in $text) { | | $fileNameToCheck = $line.Trim(); | | $found = $false; | | foreach ($file in $filesInFolder) { | | if ($file.Name -eq $fileNameToCheck) { | | $found = true; | | break; | | } | | } | | if (-not $found) { | | $missingFiles += $fileNameToCheck; | | } | | } | | | | | | if ($missingFiles.Count -gt 0) { | | $missingFiles | Out-File -FilePath "11.txt" | | } | | | | | | foreach ($file in $filesInFolder) { | | if ($dic.ContainsKey($file.Name.ToLower())) { | | write-host $file.FullName; | | $newfile = $newfolder.trimend('\') + '\' + $file.Name; | | move-item -liter $file.FullName $newfile; | | } | | }COPY |
|