如何修复此批处理文件? (打字机效果)

时间:2016-10-13 02:08:18

标签: batch-file error-handling effect

我想让批处理脚本具有打字机样式/效果,同时删除文件并显示回声和内容。我希望它看起来像这样:

:Write1
echo Deleting Prefetches...
del /s /q "C:\Windows\Prefetch\JAVAW*.pf"
echo Prefetches Deleted.
pause && goto :Write2
(Then Write2 would delete stuff, display messages and go to Write3 and so on.

这不是我实现的确切代码,但非常相似。 我有这个代码(长),我想知道是否有人可以错误修补它或只是找出它的错误,因为它重复自己,并没有在暂停后去。这是我到目前为止所做的:(注意,它删除的文件不是系统文件,而是快捷方式和无效的注册表项。干净以便测试。)

@echo off
:: %len_% holds the value of the length of the string input

setlocal enabledelayedexpansion
call :Write "Deleting Prefetches..."
del /s /q "C:\Windows\Prefetch\VAPE*.pf"
del /s /q "C:\Windows\Prefetch\JAVAW*.pf"
echo Prefetches Deleted!
pause && goto :Write2

setlocal enabledelayedexpansion
call :Write2 "Deleting Shellbags..."
reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell /va /f
reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Bags /va /f
reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\BagMRU /va /f
reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RunMRU /VA /F
reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\TypedPaths /VA /F
reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU /va /f
echo Shellbags Deleted!
pause && goto :Write3

setlocal enabledelayedexpansion
call :Write3 "Deleting Temp Files..."
for /d %%i in ("%TEMP%\Rar$EXa0.???") do rd "%TEMP%\%%i"
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
rd /s /q "C:\Users\%USERNAME%\AppData\Local\Temp\Rar$EXa0.*.*"
del /s /q "C:\Users\%USERNAME%\AppData\Local\Temp\Rar$EXa0.*.*"
attrib -H "C:\Users\%USERNAME%\AppData\Local\Temp\etilqs_*.*"
del /s /q "C:\Users\%USERNAME%\AppData\Local\Temp\etilqs_*.*"
echo Temp Files Deleted!
pause && goto :Write4

setlocal enabledelayedexpansion
call :Write4 "Deleting Recent Items..."
del /s /q "?C:\Users\%USERNAME%\Recent\SDHC (E) (2).lnk"
del /s /q "?C:\Users\%USERNAME%\Recent\SDHC (E).lnk"
del /s /q "C:\Users\%USERNAME%\Recent\*.ahk.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\1.7.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\1.8.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.exe.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape.jar.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\.vs.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Common.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Jar Injector.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.sln.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\v14.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\.suo.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Memory.c.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Memory.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\include.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jdk.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\win32.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\classfile_constants.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jawt.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jdwpTransport.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jni.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jvmti.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jvmticmlr.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\bridge.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jawt_md.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\jni_md.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\AccessBridgeCallbacks.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\AccessBridgeCalls.c.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\AccessBridgeCalls.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\AccessBridgePackages.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\DllMain.c.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Jar Injector.vcxproj.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Jar Injector.vcxproj.filters.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Jar Injector.vcxproj.user.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\ReflectiveDLLInjection.h"
del /s /q "C:\Users\%USERNAME%\Recent\ReflectiveLoader.c.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\ReflectiveLoader.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\GetProcAddressR.c.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\GetProcAddressR.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\LoadLibraryR.c.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\LoadLibraryR.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\ReflectiveDLLInjection.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\resource.h.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.rc.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.vcxproj.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.vcxproj.filters.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\Vape Launcher.vcxproj.user.lnk"
del /s /q "C:\Users\%USERNAME%\Recent\WinMain.c.lnk"
echo Recent Items Deleted!
pause && goto :Write5

setlocal enabledelayedexpansion
call :Write5 "Deleting Recycled Items..."
del /s /q "C:\$RECYCLE.BIN\*.lnk"
del /s /q "C:\$RECYCLE.BIN\*.pf"
rd /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
del /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
rd /s /q "C:\$RECYCLE.BIN\Rar$DIa0.*.*"
del /s /q "C:\$RECYCLE.BIN\Rar$DIa0.*.*"
del /s /q "C:\$RECYCLE.BIN\etilqs_*.*"
echo Recycled Items Deleted!
pause && goto :eof

:Write
set string=%1
call :GetLength %string%
set string=%string:"=%
set /a length=%len_%
for /l %%A in (1,1,%length%) do (
cls
echo !string:~0,%%A!
ping localhost -n 1 -w 500 -l 5000 > nul
)

:Write2
set string=%1
call :GetLength %string%
set string=%string:"=%
set /a length=%len_%
for /l %%A in (1,1,%length%) do (
cls
echo !string:~0,%%A!
ping localhost -n 1 -w 500 -l 5000 > nul
)

:Write3
set string=%1
call :GetLength %string%
set string=%string:"=%
set /a length=%len_%
for /l %%A in (1,1,%length%) do (
cls
echo !string:~0,%%A!
ping localhost -n 1 -w 500 -l 5000 > nul
)

:Write4
set string=%1
call :GetLength %string%
set string=%string:"=%
set /a length=%len_%
for /l %%A in (1,1,%length%) do (
cls
echo !string:~0,%%A!
ping localhost -n 1 -w 500 -l 5000 > nul
)

goto :eof

:GetLength
set str=%1
set /a len=1
:loop
set /a len_=%len% - 1
if "!str:~0,%len%!"=="!str:~0,%len_%!" goto done
set /a len+=1
goto loop

:done
set /a len_-=2
goto :eof

1 个答案:

答案 0 :(得分:1)

您不需要调用相同的函数:Write1 :write2 :write3 :write3..,依此类推......

您只需要一个函数TypeWriter即可根据需要调用它,因此,只需调用它就可以节省大量代码,如下代码所示:

@echo off
Title Example of TypeWriter
color 0A
setlocal enabledelayedexpansion
call :TypeWriter "http://stackoverflow.com/questions/40011157/how-do-i-fix-this-batch-file-typewriter-effect"
pause && goto Next
::************************************************
:TypeWriter
set string=%1
call :GetLength %string%
set string=%string:"=%
set /a length=%len_%
for /l %%A in (1,1,%length%) do (
cls
echo !string:~0,%%A!
ping localhost -n 1 -w 500 -l 5000 > nul
)
::************************************************
:GetLength
set str=%1
set /a len=1
::************************************************
:loop
set /a len_=%len% - 1
if "!str:~0,%len%!"=="!str:~0,%len_%!" goto done
set /a len+=1
goto loop
::************************************************
:done
set /a len_-=2
goto :eof
::************************************************
:Next
color 0C
Call :TypeWriter "Deleting Recycled Items..."
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\*.lnk""
echo del /s /q "C:\$RECYCLE.BIN\*.lnk"
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\*.pf""
echo del /s /q "C:\$RECYCLE.BIN\*.pf"
Call :TypeWriter "echo rd /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*""
echo rd /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
echo del /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
Call :TypeWriter "echo rd /s /q "C:\$RECYCLE.BIN\Rar$DIa0.*.*""
echo rd /s /q "C:\$RECYCLE.BIN\Rar$DIa0.*.*"
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\Rar$D.*.*"Ia0"
echo del /s /q "C:\$RECYCLE.BIN\Rar$D.*.*"Ia0
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\etilqs_*.*""
echo del /s /q "C:\$RECYCLE.BIN\etilqs_*.*"
echo Recycled Items Deleted!
pause
::************************************************

编辑于2016年10月13日@22:05

另一个例子:

@echo off
Call :TypeWriter "http://stackoverflow.com/questions/40011157/how-do-i-fix-this-batch-file-typewriter-effect"
pause && goto Next
::*************************************************************
:TypeWriter
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo     strTempText = Mid(strText,x,1^)
echo     WScript.StdOut.Write strTempText
echo     WScript.Sleep intPause
echo Next
)>%tmp%\%~n0.vbs
@cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
echo(
exit /b
::**************************************************************
:Next
color 0C
Call :TypeWriter "Deleting Recycled Items..."
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\*.lnk""
echo del /s /q "C:\$RECYCLE.BIN\*.lnk"
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\*.pf""
echo del /s /q "C:\$RECYCLE.BIN\*.pf"
Call :TypeWriter "echo rd /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*""
echo rd /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
echo del /s /q "C:\$RECYCLE.BIN\Rar$EXa0.*.*"
Call :TypeWriter "echo rd /s /q "C:\$RECYCLE.BIN\Rar$DIa0.*.*""
echo rd /s /q "C:\$RECYCLE.BIN\Rar$DIa0.*.*"
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\Rar$D.*.*"Ia0"
echo del /s /q "C:\$RECYCLE.BIN\Rar$D.*.*"Ia0
Call :TypeWriter "echo del /s /q "C:\$RECYCLE.BIN\etilqs_*.*""
echo del /s /q "C:\$RECYCLE.BIN\etilqs_*.*"
echo Recycled Items Deleted!
pause>nul
::************************************************