以下.bat
脚本中是否有任何内容可能会删除/损坏任何 Windows 10 系统文件并阻止它们启动,从而导致启动循环?重新启动后,执行后.bat
,Windows 将无法启动并显示Critical Service Failed
:
- 我
.bat
以管理员身份运行了,先从闪存驱动器运行,然后从运行C:
,然后使用 Cmd 终端打开了文件名为不是在正在处理的闪存驱动器上,这很可疑:
我整理了这个脚本,结合了这里和其他地方找到的几个命令,以便处理takeown /f "H:\PVR" /r icacls "H:\PVR" /reset /t for /r "H:\PVR" %%i in (*.ts *.tsv) do move "%%i" "H:\PVR" cd /d "H:\PVR" ren *.tsv *.ts for /f "delims=" %%d in ('dir /ad /b') do @rd /q /s "%%d" setlocal EnableDelayedExpansion for %%a in (*.ts *.tsv) do ( set name=%%~Na set num=00!random! set newName=!!name:~-17! ren "%%a" "!newName!%%~Xa" )
.ts
来自我的 DVBT-T2 接收器的闪存驱动器上的录音,其目的是:- 取得许可
H:\PVR
及其所有内容 - 将
.tsv
录音移出子文件夹,删除子文件夹 - 将
.tsv
扩展名重命名为.ts
- 删除文件名的前 17 个字符,因为我不喜欢接收器的前缀格式
- 取得许可
我该如何解决这个问题?
答案1
...然后打开了一个 CMD 窗口,我看到正在处理不在我的闪存驱动器中的文件名,这很可疑......
去
H:
:cd /d H:\
如果
cd /d H:\
返回非0
,操作员将执行 之后的下一个命令||
,使批处理脚本转到文件 [ ] 的末尾goto :eOf
,结束执行而不对其他文件夹/文件执行任何操作:cd /d H:\ || goto :eOf
如果最后一条命令成功返回,它将执行以下
takeown
命令:icacls
H:
%__AppDir__%takeown.exe /f H:\PVR\. /r %__AppDir__%icacls.exe H:\PVR\. /reset /t
为了
takeown
和icals
命令,使用暂停来查看屏幕::: # One space before ^ and no space and/or another character after ^ : %__AppDir__%takeown.exe /f .\PVR\. /r | <nul ^ %__AppDir__%Findstr.exe "(or.*):" & %__AppDir__%timeout.exe /t -1 %__AppDir__%icacls.exe .\PVR\. /reset /t | <nul ^ %__AppDir__%Find.exe "files; Fai" & %__AppDir__%timeout.exe /t -1
要删除超时:
%__AppDir__%takeown.exe /f .\PVR\. /r && %__AppDir__%icacls.exe .\PVR\. /reset /t
Sort
以反向模式列出 [ ] 文件夹/树 [/r
] 以获取目录树中的最后一个子文件夹/目录,并在可能的情况下移动文件并删除当前目录:for /f usebacktokens^=* %i in (`dir /b /s /ad-h H:\PVR\.^|%__AppDir__%Sort.exe /r
$ Dir /b /s /ad-h D:\PVR|%__AppDir__%Sort.exe /r H:\PVR\tmp H:\PVR\Dir 003\tmp H:\PVR\Dir 003 H:\PVR\Dir 002\tmp H:\PVR\Dir 002 H:\PVR\Dir 001\tmp H:\PVR\Dir 001 H:\PVR\Dir 000\tmp H:\PVR\Dir 000
对于循环中的每个文件夹,移动
*.ts?
文件,应用于*.ts
和*.tsv
其中的文件\PVR\.
;如果没有发生错误[文件/文件夹正在使用等],则删除文件夹,否则重命名文件:(.\PVR\*.tsv
→*.ts
)move "%%~fi\*.ts?" H:\PVR\ && rd "%%~fi" && rename H:\PVR\*.tsv *.ts move "%%~fi\*.ts?" H:\PVR\ if return 0 do: rd "%%~fi" && if return 0 do: rename H:\PVR\*.tsv *.ts
我认为没有其他
*.ts
文件夹中的文件,您需要删除此文件夹 [@rd /q /s "%%d"
];如果不是这种情况,请将最后一行替换为:for /f usebacktokens^=* %%i in (`Dir /b /s /ad-h H:\PVR^|%__AppDir__%Sort.exe /r `)do ( move "%%~fi\*.tsv" D:\PVR\. && move "%%~fi\*.ts" H:\PVR\. ) && ( rmDir /s /q "%%~fi" && rename D:\PVR\*.tsv *.ts )
要在同一行和运行时使用新定义的变量来获取值,请调用
cmd /v /c !Newly defined variable!
;这是通过将名称的长度限制为最大 17 个字符来重命名文件的方法!_new:~-17!
(与最后 17 个字符相同)set "_new=%%~ni" && in the same line use: cmd.exe /v:on /c rename "%%~fi" "!_new:~-17!%%~xi"
- 我个人倾向于使用,
where
因为它可以轻松返回文件的完整路径;与不同的是dir /b
,它不显示小写字符串:for /f useback^tokens^=* %%i in (`2^>nul %__AppDir__%where.exe "H:\PVR":"*.ts*" `)do set "_new=%%~ni" && cmd.exe /v:on /c rename "%%~fi" "!_new:~-17!%%~xi"
- 我补充说
useback
(缩写为usebackq
)以避免意外行为,如果文件名/路径中可能存在任何特殊字符,即使没有发布字符串/布局组成文件名
- 我个人倾向于使用,
处理移动/重命名文件:
@echo off
cd /d H:\.. || goto :eof
setlocal Enabledelayedexpansion
%__AppDir__%takeown.exe /f .\PVR\. /r | <nul ^
%__AppDir__%Findstr.exe "(or.*):" & %__AppDir__%timeout.exe /t -1
%__AppDir__%icacls.exe .\PVR\. /reset /t | <nul ^
%__AppDir__%Find.exe "files;.Fa" & %__AppDir__%timeout.exe /t -01
for /f usebacktokens^=* %%i in (`Dir /b /s /ad-h H:\PVR^|%__AppDir__%Sort.exe /r
`)do move "%%~fi\*.ts?" H:\PVR\ && rd "%%~fi" && rename H:\PVR\*.tsv *.ts
for /f useback^tokens^=* %%i in (`2^>nul %__AppDir__%where.exe "H:\PVR":"*.ts*"
`)do set "_new=%%~ni" && cmd.exe /v:on /c rename "%%~fi" "!_new:~-17!%%~xi"
endlocal & %__AppDir__%timeout.exe /t -1 | echo\iS dONE! & goto :eof || exit /b
- 输出:
H:\PVR\tmp\F0HZCERYIEYKY--U0O69.ts H:\PVR\tmp\4-W5N0-6QDOWYAS-YYW-.ts H:\PVR\tmp\-PD2MDWHFIL5VP0DWCN8.tsv H:\PVR\tmp\SZAAC--91GQCH7OD9-0P.tsv 4 file(s) moved. Removed: "H:\PVR\tmpi" Moved: tmp\*.ts? H:\PVR\Dir 000\tmp\I9aKTe38uRqcmkh2B7GF.ts H:\PVR\Dir 000\tmp\BBPcfN7R9jg1ESsgHsnA.ts H:\PVR\Dir 000\tmp\FRAEKeG1HnMZTmBpY6CB.tsv H:\PVR\Dir 000\tmp\EHuI3OiwSRGZWg7fsxsT.tsv 4 file(s) moved. Removed: "H:\PVR\Dir 000\tmpi" Moved: tmp\*.ts? H:\PVR\Dir 001\tmp\HZH4jcvj2GvQzf7og5h3.ts H:\PVR\Dir 001\tmp\EHRR1jKp3owr4lIsZZre.ts H:\PVR\Dir 001\tmp\1c0sB7o881uCGiYZUnvh.tsv H:\PVR\Dir 001\tmp\PnDUHnrCAGeUB3RRnwF5.tsv 4 file(s) moved. Removed: "H:\PVR\Dir 001\tmpi" Moved: tmp\*.ts? H:\PVR\Dir 002\tmp\nqRuQhA9WrXvPk7i2ywt.ts H:\PVR\Dir 002\tmp\7b4hGoKaA9HxiSXtbpAT.ts H:\PVR\Dir 002\tmp\Y6PbfweT6aSgDiDDCVdU.tsv H:\PVR\Dir 002\tmp\qvnIDtkY7uKtfcgStHzq.tsv 4 file(s) moved. Removed: "H:\PVR\Dir 002\tmpi" Moved: tmp\*.ts? H:\PVR\Dir 003\tmp\wnAVPzdos4RWjrJTt9cr.ts H:\PVR\Dir 003\tmp\EXDlfbAqCyfB23l7ZZHz.ts H:\PVR\Dir 003\tmp\lgAduikvz4vlF1PWA892.tsv H:\PVR\Dir 003\tmp\JcBmygrENpU4kEOOIFqP.tsv 4 file(s) moved. Removed: "H:\PVR\Dir 003\tmpi" Moved: tmp\*.ts? H:\PVR\Dir 000\SvRxVxF6ZDr7a8R5F9z5.ts H:\PVR\Dir 000\1tZDpWwpCVNqrxwr993C.ts H:\PVR\Dir 000\BXKSG8q0vo3bXCgaNr7K.tsv H:\PVR\Dir 000\qtPe80WQRRrms1Iga1i3.tsv 4 file(s) moved. Removed: "H:\PVR\Dir 000i" Moved: Dir 000\*.ts? H:\PVR\Dir 001\NXGbTP1AlPxJGRPWaUJx.ts H:\PVR\Dir 001\Q2Eq97QpY7XQ7reWjUMi.ts H:\PVR\Dir 001\UEAkSw76WV9j0494Lpvv.tsv H:\PVR\Dir 001\IFBoWCY483HQE94fpCHU.tsv 4 file(s) moved. Removed: "H:\PVR\Dir 001i" Moved: Dir 001\*.ts? H:\PVR\Dir 002\4nSfuQtqqOF6rPrs9TKb.ts H:\PVR\Dir 002\iNEIDXVdvKkN7ab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5.ts
H:\PVR\DpWwpCVNqrxwr993C.ts
H:\PVR\SG8q0vo3bXCgaNr7K.ts
H:\PVR\e80WQRRrms1Iga1i3.ts
H:\PVR\bTP1AlPxJGRPWaUJx.ts
H:\PVR\q97QpY7XQ7reWjUMi.ts
H:\PVR\kSw76WV9j0494Lpvv.ts
H:\PVR\oWCY483HQE94fpCHU.ts
H:\PVR\fuQtqqOF6rPrs9TKb.ts
H:\PVR\IDXVdvKkN7abkFZJj.ts
H:\PVR\GThdkefADZpMKWq30.ts
H:\PVR\cbLS8QZngJSDfM9k4.ts
H:\PVR\W84pUm5TEGCWTueFw.ts
H:\PVR\3dLHI5QzdrkGZJJju.ts
H:\PVR\Qu7bcvmQepdh8iVzp.ts
is done!
为了重现我对上述代码的测试场景,请获取一个免费的闪存驱动器,并替换相应的驱动器号:
@echo off
cd /d H:\.. || goto :eof
setlocal Enabledelayedexpansion
:: # create folder for sample/test...
:: # H:\
:: # \---PVR
:: # +---tmp
:: # +---Dir 000
:: # | \---tmp
:: # +---Dir 001
:: # | \---tmp
:: # +---Dir 002
:: # | \---tmp
:: # \---Dir 003
:: # | \---tmp
:: # make folders ".\PVR\tmp\." and ".\PVR\Dir 000\tmp\."
mkdir ".\PVR\tmp\." ".\PVR\Dir 000\tmp\."
:: # make folders ".\PVR\Dir 001\tmp\." and ".\PVR\Dir 002\tmp\." and ".\PVR\Dir 003\tmp\."
mkdir ".\PVR\Dir 001\tmp\." ".\PVR\Dir 002\tmp\." ".\PVR\Dir 003\tmp\."
:: # Dir /b /s /ad-h /o-d outputs:
:: # H:\PVR
:: # H:\PVR\tmp
:: # H:\PVR\Dir 000
:: # H:\PVR\Dir 001
:: # H:\PVR\Dir 002
:: # H:\PVR\Dir 003
:: # H:\PVR\Dir 000\tmp
:: # H:\PVR\Dir 001\tmp
:: # H:\PVR\Dir 002\tmp
:: # H:\PVR\Dir 003\tmp
:: # Show folder the tree/dir created above and list folders
%__AppDir__%tree.com /a /f .
%__AppDir__%Timeout.exe -1 | dir /b /s /ad-h /o-d
echo\UD5I51RPV68L-6MKLEHV.ts >".\PVR\UD5I51RPV68L-6MKLEHV.ts"
echo\5F5-IE2WZ5MGZ0NOYAC8.ts >".\PVR\5F5-IE2WZ5MGZ0NOYAC8.ts"
echo\UY6MYVS0GIQKLSZ99-PI.tsv >".\PVR\UY6MYVS0GIQKLSZ99-PI.tsv"
echo\QA01WMI4-W-0DH2C9V9O.tsv >".\PVR\QA01WMI4-W-0DH2C9V9O.tsv"
echo\F0HZCERYIEYKY--U0O69.ts >".\PVR\tmp\F0HZCERYIEYKY--U0O69.ts"
echo\4-W5N0-6QDOWYAS-YYW-.ts >".\PVR\tmp\4-W5N0-6QDOWYAS-YYW-.ts"
echo\-PD2MDWHFIL5VP0DWCN8.tsv >".\PVR\tmp\-PD2MDWHFIL5VP0DWCN8.tsv"
echo\SZAAC--91GQCH7OD9-0P.tsv >".\PVR\tmp\SZAAC--91GQCH7OD9-0P.tsv"
echo\SvRxVxF6ZDr7a8R5F9z5.ts >".\PVR\Dir 000\SvRxVxF6ZDr7a8R5F9z5.ts"
echo\1tZDpWwpCVNqrxwr993C.ts >".\PVR\Dir 000\1tZDpWwpCVNqrxwr993C.ts"
echo\BXKSG8q0vo3bXCgaNr7K.tsv >".\PVR\Dir 000\BXKSG8q0vo3bXCgaNr7K.tsv"
echo\qtPe80WQRRrms1Iga1i3.tsv >".\PVR\Dir 000\qtPe80WQRRrms1Iga1i3.tsv"
echo\I9aKTe38uRqcmkh2B7GF.ts >".\PVR\Dir 000\tmp\I9aKTe38uRqcmkh2B7GF.ts"
echo\BBPcfN7R9jg1ESsgHsnA.ts >".\PVR\Dir 000\tmp\BBPcfN7R9jg1ESsgHsnA.ts"
echo\FRAEKeG1HnMZTmBpY6CB.tsv >".\PVR\Dir 000\tmp\FRAEKeG1HnMZTmBpY6CB.tsv"
echo\EHuI3OiwSRGZWg7fsxsT.tsv >".\PVR\Dir 000\tmp\EHuI3OiwSRGZWg7fsxsT.tsv"
echo\NXGbTP1AlPxJGRPWaUJx.ts >".\PVR\Dir 001\NXGbTP1AlPxJGRPWaUJx.ts"
echo\Q2Eq97QpY7XQ7reWjUMi.ts >".\PVR\Dir 001\Q2Eq97QpY7XQ7reWjUMi.ts"
echo\UEAkSw76WV9j0494Lpvv.tsv >".\PVR\Dir 001\UEAkSw76WV9j0494Lpvv.tsv"
echo\IFBoWCY483HQE94fpCHU.tsv >".\PVR\Dir 001\IFBoWCY483HQE94fpCHU.tsv"
echo\HZH4jcvj2GvQzf7og5h3.ts >".\PVR\Dir 001\tmp\HZH4jcvj2GvQzf7og5h3.ts"
echo\EHRR1jKp3owr4lIsZZre.ts >".\PVR\Dir 001\tmp\EHRR1jKp3owr4lIsZZre.ts"
echo\1c0sB7o881uCGiYZUnvh.ts >".\PVR\Dir 001\tmp\1c0sB7o881uCGiYZUnvh.tsv"
echo\PnDUHnrCAGeUB3RRnwF5.tsv >".\PVR\Dir 001\tmp\PnDUHnrCAGeUB3RRnwF5.tsv"
echo\4nSfuQtqqOF6rPrs9TKb.tv >".\PVR\Dir 002\4nSfuQtqqOF6rPrs9TKb.ts"
echo\iNEIDXVdvKkN7abkFZJj.tv >".\PVR\Dir 002\iNEIDXVdvKkN7abkFZJj.ts"
echo\ygWGThdkefADZpMKWq30.tsv >".\PVR\Dir 002\ygWGThdkefADZpMKWq30.tsv"
echo\GLLcbLS8QZngJSDfM9k4.tsv >".\PVR\Dir 002\GLLcbLS8QZngJSDfM9k4.tsv"
echo\nqRuQhA9WrXvPk7i2ywt.ts >".\PVR\Dir 002\tmp\nqRuQhA9WrXvPk7i2ywt.ts"
echo\7b4hGoKaA9HxiSXtbpAT.ts >".\PVR\Dir 002\tmp\7b4hGoKaA9HxiSXtbpAT.ts"
echo\Y6PbfweT6aSgDiDDCVdU.tsv >".\PVR\Dir 002\tmp\Y6PbfweT6aSgDiDDCVdU.tsv"
echo\qvnIDtkY7uKtfcgStHzq.tsv >".\PVR\Dir 002\tmp\qvnIDtkY7uKtfcgStHzq.tsv"
echo\i1wW84pUm5TEGCWTueFw.ts >".\PVR\Dir 003\i1wW84pUm5TEGCWTueFw.ts"
echo\N7s3dLHI5QzdrkGZJJju.ts >".\PVR\Dir 003\N7s3dLHI5QzdrkGZJJju.ts"
echo\T1lQu7bcvmQepdh8iVzp.tsv >".\PVR\Dir 003\T1lQu7bcvmQepdh8iVzp.tsv"
echo\izNYHYHzFf4moJZPcOJv.tsv >".\PVR\Dir 003\izNYHYHzFf4moJZPcOJv.tsv"
echo\wnAVPzdos4RWjrJTt9cr.ts >".\PVR\Dir 003\tmp\wnAVPzdos4RWjrJTt9cr.ts"
echo\EXDlfbAqCyfB23l7ZZHz.ts >".\PVR\Dir 003\tmp\EXDlfbAqCyfB23l7ZZHz.ts"
echo\lgAduikvz4vlF1PWA892.tsv >".\PVR\Dir 003\tmp\lgAduikvz4vlF1PWA892.tsv"
echo\JcBmygrENpU4kEOOIFqP.tsv >".\PVR\Dir 003\tmp\JcBmygrENpU4kEOOIFqP.tsv"
:: # put a copy from outputs [tree]+[dir] into the clipboard and exit
(dir /b /s /ad-h /o-d & %__AppDir__%tree.com /a /f . ) | %__AppDir__%clip.exe
endlocal & goto :eof