关于批处理-番外篇

set Source=z%1
:: 添加一个临时字符 z
if %Source:~1,1%z%Source:~-1%==”z” set Source=z%Source:~2,-1%
::
检查变量参数1的第一个和最后一个字符是否为 “” ,是的话就去掉
set Source=%Source:~1%
::
去掉临时字符 z
if “%Source%”==”” goto :HelpInformation
if “%Source%”==”/ goto :HelpInformation
if /i “%Source%”==”/help” goto :HelpInformation
if not exist “%Source%” echo
目标文件不存在 & exit /b 2
::
检查参数1 [1]

if /i “%2″==”/Bin”         set ConvertOption=ConvertToBin
if /i “%3″==”/Bin”         set ConvertOption=ConvertToBin
if /i “%2″==”/Block”       set ConvertOption=ConvertToBin & set BinaryFormat=BinBlock
if /i “%3″==”/Block”       set ConvertOption=ConvertToBin & set BinaryFormat=BinBlock
if /i “%2″==”/ProgressOff” set ShowProgress=ShowProgressOff
if /i “%3″==”/ProgressOff” set ShowProgress=ShowProgressOff
:: 检查参数23 [2]

for %%i in (“%Source%”) do (
    set SourceDrivePathName=%%~dpni
    set FileSize=%%~zi
)
:: 获得源文件的 驱动器 +路径+标题名 ,以及文件大小。 d – drive, p – path, n – name, z – size
if “%FileSize%”==”0” echo
目标文件为空 & exit /b 3
del %TempFile% >nul 2>nul
fsutil file createnew %TempFile% %FileSize% >nul
::
创建一个大小与源文件一样的空白文件,该文件里的所有字节均为0x00

goto :%ConvertOption%

:ConvertToHex
:: 十六进制转换
setlocal EnableDelayedExpansion
set OutputFile=”%SourceDrivePathName%.Hex.txt”
echo
文件 %Source% 的十六进制格式:>%OutputFile%
echo. & echo
正在转换为十六进制

for /f “skip=1 tokens=1,3” %%i in (‘fc /b %TempFile% “%Source%”‘) do (
    set index=0x%%i
    set index=!index:~0,-1!
    set /a offset = !index! – !num!

    for /l %%n in (1,1,!offset!) do (
        set /p=00>%OutputFile%
        set /a num += 1
    )
    set /p=%%j>%OutputFile%
    set /a num += 1
    call :%ShowProgress% !num!
)
:: 通过与大小相同内容空白的文件进行二进制对比,获得源文件的十六进制码 [3]
endlocal
goto :ExitSuccess

:ConvertToBin
setlocal EnableDelayedExpansion
set OutputFile=”%SourceDrivePathName%.%BinaryFormat%.txt”
echo 文件 %Source% 的二进制格式:>%OutputFile%
echo. & echo
正在转换为二进制

for /f “skip=1 tokens=1,3” %%i in (‘fc /b %TempFile% “%Source%”‘) do (
    set index=0x%%i
    set index=!index:~0,-1!
    set /a offset = !index! – !num!

    for /l %%n in (1,1,!offset!) do (
        call :HexTo%BinaryFormat% 0
        call :HexTo%BinaryFormat% 0
        set /a num += 1
    )
    set HexData=%%j
    call :HexTo%BinaryFormat% !HexData:~0,1!
    call :HexTo%BinaryFormat% !HexData:~1,1!
    set /a num += 1
    call :%ShowProgress% !num!
)
endlocal
goto :ExitSuccess

:HexToBin
:: 函数: HexToBin 用途: 01的形式显示
if %1==0 set /p=0000>%OutputFile%
if %1==1 set /p=0001>%OutputFile%
if %1==2 set /p=0010>%OutputFile%
if %1==3 set /p=0011>%OutputFile%
if %1==4 set /p=0100>%OutputFile%
if %1==5 set /p=0101>%OutputFile%
if %1==6 set /p=0110>%OutputFile%
if %1==7 set /p=0111>%OutputFile%
if %1==8 set /p=1000>%OutputFile%
if %1==9 set /p=1001>%OutputFile%
if %1==A set /p=1010>%OutputFile%
if %1==B set /p=1011>%OutputFile%
if %1==C set /p=1100>%OutputFile%
if %1==D set /p=1101>%OutputFile%
if %1==E set /p=1110>%OutputFile%
if %1==F set /p=1111>%OutputFile%
goto :EOF

:HexToBinBlock
:: 函数: HexToBinBlock 用途: 以 或■的形式显示[3]
if %1==0 set /p=
    >%OutputFile%
if %1==1 set /p=
   ■>%OutputFile%
if %1==2 set /p=
  ■ >%OutputFile%
if %1==3 set /p=
  ■■>%OutputFile%
if %1==4 set /p=
 ■  >%OutputFile%
if %1==5 set /p=
 ■ ■>%OutputFile%
if %1==6 set /p=
 ■■ >%OutputFile%
if %1==7 set /p=
 ■■■>%OutputFile%
if %1==8 set /p=
■   >%OutputFile%
if %1==9 set /p=
■  ■>%OutputFile%
if %1==A set /p=
■ ■ >%OutputFile%
if %1==B set /p=
■ ■■>%OutputFile%
if %1==C set /p=
■■  >%OutputFile%
if %1==D set /p=
■■ ■>%OutputFile%
if %1==E set /p=
■■■ >%OutputFile%
if %1==F set /p=
■■■■>%OutputFile%
goto :EOF

:ShowProgressOn
:: 函数: ShowProgressOn 用途: 显示转换进度
set /a mod = %1 %% 50
if %mod% equ 0 (
    set /a percent = %1 * 100 / %FileSize%
    set /p=%BackSpace%%BackSpace%%BackSpace%    set /p=!percent!%%)
goto :EOF

:ShowProgressOff
:: 函数: ShowProgressOff 用途: 不显示转换进度
goto :EOF

:HelpInformation
:: 该批处理的帮助信息
echo
将文件转换为十六进制或二进制的格式。
echo.
echo DataFormatConvert source [/Hex ^| /Bin ^| /Block] [/ProgressOn ^| /ProgressOff]
echo.
echo   source      
指定要转换的文件。
echo   /Hex        
表示转换为十六进制格式(默认)
echo   /Bin        
表示转换为二进制格式。
echo   /Block      
表示转换为二进制格式并以方块的形式显示。
echo   /ProgressOn
 显示转换进度(默认)
echo   /ProgressOff
关闭显示进度。
exit /b 1

set /p choice=LaunchOptions:
if not “%choice%”==”” goto :CustomLaunch

:BasicLaunch
call :LaunchItem1
call :LaunchItem2
call :LaunchItem3
call :LaunchItem4
call :LaunchItem5
goto :EOF

:CustomLaunch
for /l %%i in (0 1 9) do (
    set “choice_=!choice:~%%i,1!”
    if “!choice_!”==”” echo All Launched! & goto :EOF
    if !choice_! gtr 9 echo Unknown Define & goto :EOF
    if !choice_! lss 0 echo Unknown Define & goto :EOF
    call :LaunchItem!choice_!
)

:LaunchItem1
start “OE”           “C:Program FilesOutlook Expressmsimn.exe”
call :IsItemLaunchedSuccessful OE
goto :EOF
:LaunchItem2
start “Skype”        “D:Program FilesSkypePhoneSkype.exe” /nosplash /minimized
call :IsItemLaunchedSuccessful Skype
goto :EOF
:LaunchItem3
Start “QQ”           “D:Program FilesTencentQQQQ.exe”
call :IsItemLaunchedSuccessful QQ
goto :EOF
:LaunchItem4
start “BaiduHi” /min “D:Program FilesbaiduBaidu HiBaiduHi.exe”
call :IsItemLaunchedSuccessful BaiduHi
goto :EOF
:LaunchItem5
start “Thunder” /min “D:Program FilesThunder NetworkThunderThunder.exe”
call :IsItemLaunchedSuccessful Thunder
goto :EOF
:LaunchItem6
start “Tudou”   /min “D:Program FilesTudouiTudouiTudou.exe”
call :IsItemLaunchedSuccessful Tudou
goto :EOF
:LaunchItem7
start “MSN”     /min “C:Program FilesMSN Messengermsnmsgr.exe”
call :IsItemLaunchedSuccessful MSN
goto :EOF

:IsItemLaunchedSuccessful
set /p =Launching %1if ERRORLEVEL 0 (
    for /l %%i in (1,1,4) do (
        ping -n %ShortDelay% 127.1>nul
        set /p =.    )
)
for /l %%i in (1,1,32) do set /p =%BackSpace%echo %1 Launched     
title %1 Launched
ping -n %ShortDelay% 127.1>nul
goto :EOF
:::::::::::::::::::::::::::::::: 看得出来,这是一个科技含量很低的批处理,仅仅是在 start 每个程序之间将其挂起一段时间来避免众多程序同时启动所带来的负荷。当然,为了使它能够发挥作用,您得亲自配置它所要启动的程序,以及程序所在的路径才行。

 

文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览113438 人正在系统学习中 相关资源:抖音最近很火的直播,挤地铁源码软件教程_抖音虚拟人物直播代码…

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2012年1月11日
下一篇 2012年1月12日

相关推荐