在 .bat 程序中操作带有特殊字符的文本文件时遇到问题
Having trouble with operating text file with special charcters in .bat program
问题已重新格式化,现已找到错误原因。
背景资料:
所以我对 .bat 还很陌生,所以任何关于我的代码的一般性建议都会很好。我是 运行 闪存驱动器上的一个简单程序,它将加密我闪存驱动器上另一个文件的内容。这些文件可能包含任何键盘字符,因此我需要能够处理所有这些字符。
错误:
看起来即使我使用 delayedExpansinon 我的程序仍然无法处理有毒字符。我的程序没有循环遍历多行(感谢 michael_heath),但是以下错误日志显示了一个新错误。看来 " 仍然是一个问题。同样在测试中我发现它不适用于 % 或 ! in abet 和 cipher1.
注意解决这个问题我试图将 enabledealyedexpansion 添加到整个 encrypt/dycrpt 功能。这确实消除了我涉及 " 的错误,但我的代码不再处理 % 和 !。
程序
@echo off
setlocal
cd BatchMenu/BatchLibs
set "command=%3"
set "drivePath=%2"
set "fileName=%1"
set "abet=abcdefghijklmnopqrstuvwxyz!@# $%^&*()_-+={[}]/?<,>.\|~`':; 0123456789"
set "cipher1=;~`8p#j4*&9zw+}.{a@%^e?[@0u2< r|5)o!(xk_-[cf'1b3g7h=m>,qil/sntdvy:"
if "%command%" == "d" goto dycrypt
goto encrypt
:encrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
setlocal enabledelayedexpansion
set "line=%%~A"
call :encipher
endlocal
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
del "%drivePath%%fileName%.bak"
goto :eof
:dycrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
setlocal enabledelayedexpansion
set "line=%%~A"
call :decipher
endlocal
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
del "%drivePath%%fileName%.bak"
goto :eof
:decipher
set "morf=%abet%"
set "from=%cipher1%"
goto trans
:encipher
set "from=%abet%"
set "morf=%cipher1%"
:trans
set "enil="
:transl
set "=%from%"
set "=%morf%"
:transc
if /i "!line:~0,1!" == "!:~0,1!" (
set "enil=%enil%%:~0,1%"
goto transnc
)
set "=%:~1%"
set "=%:~1%"
IF defined goto transc
:: No translation - keep
set "enil=%enil%%line:~0,1%"
:transnc
set "line=!line:~1!"
if defined line goto transl
if defined enil >&3 echo !enil!
goto :eof
错误日志
E:\BatchMenu\BatchLibs>set "=rstuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=.{a@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "r" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||."
goto transnc
)
E:\BatchMenu\BatchLibs>set "=stuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "={a@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "s" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||{"
goto transnc
)
E:\BatchMenu\BatchLibs>set "=tuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=a@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "t" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||a"
goto transnc
)
E:\BatchMenu\BatchLibs>set "=uvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "u" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||@"
goto transnc
)
E:\BatchMenu\BatchLibs>set "=vwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "="
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1|||"
E:\BatchMenu\BatchLibs>set "line="797816-901A5E25AE3F84AAC67979FF01AA681E"|Mon, 29 Feb 2016 11:54:54 GMT|application/vnd.openxmlformats-officedocument.presentationml.presentation|application/vnd.openxmlformats-officedocument.presentationml.presentation"
E:\BatchMenu\BatchLibs>if defined line goto transl
E:\BatchMenu\BatchLibs>set "=abcdefghijklmnopqrstuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=;~`8p#j4*&9zw+}.{a@"
The syntax of the command is incorrect.
E:\BatchMenu\BatchLibs>if /i """ == "a" (
*NOTE to fix this I attempted to add enabledealyedexpansion to the entire encrypt/dycrpt functionality. This did eliminate this error but my code does not handle % and ! anymore.
@echo off
setlocal enabledelayedexpansion
cd Batch/BatchLibrariesLocation
set "command=%~3"
set "drivePath=%~2"
set "fileName=%~1"
set "abet=abcdefghijklmnopqrstuvwxyz!@# $%%^&*()_-+={[}]/?<,>.\|~`':; 0123456789"
set "cipher1=;~`8p#j4*&9zw+}.{a@%%^e?[@0u2< r|5)o!(xk_-[cf'1b3g7h=m>,qil/sntdvy:"
if "%command%" == "d" goto dycrypt
goto encrypt
:encrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
set "line=%%A"
call :encipher
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
goto :eof
:dycrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
set "line=%%A"
call :decipher
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak2"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
goto :eof
:decipher
set "morf=!abet!"
set "from=!cipher1!"
goto trans
:encipher
set "from=!abet!"
set "morf=!cipher1!"
:trans
set "enil="
:transl
set "=!from!"
set "=!morf!"
:transc
if /i "!line:~0,1!" == "!:~0,1!" set "enil=!enil!!:~0,1!"& goto transnc
set "=!:~1!"
set "=!:~1!"
IF defined goto transc
:: No translation - keep
set "enil=!enil!!line:~0,1!"
:transnc
set "line=!line:~1!"
if defined line goto transl
if not defined enil goto :eof
>&3 echo !enil!
goto :eof
根据需要用双引号引起来的变量和字符串。
带有 for
循环的选项 usebackq
因此可以双引号路径。
输出从 for
循环回显到文件,现在使用
stream 3 以便可以使用 echo on
和任何 echo
使用流 1 不会被重定向到文件。
3>
用于将流 3 重定向到文件,>&3
使用流 3 句柄的命令。
将源文件扩展名重命名为.bak
以便于测试。
如果需要,请改回 del
文件。
使用enabledelayedexpansion
避免回显字符串
使用有毒字符以避免错误。
修复了已知错误,而不是实际加密等。
以后可以是你的研发来做。
更新了脚本顶部的延迟扩展
允许许多变量使用延迟扩展。
结果显示加解密异常(无错误)。
要加密:
"797816-901A5E25AE3F84AAC67979FF01AA681E"|Mon, 29 Feb 2016 11:54:54 GMT|application/vnd.openxmlformats-officedocument.presentationml.presentation|application/vnd.openxmlformats-officedocument.presentationml.presentation
解密:
"7k7d16-k01a5e25ae3fd4aac67k7kff01aa6d1e"4mon, 2k feb 2016 11:54:54 gmt4application/vndropenxmlformats-officedocumentrpresentationmlrpresentation4application/vndropenxmlformats-officedocumentrpresentationmlrpresentation
问题已重新格式化,现已找到错误原因。
背景资料: 所以我对 .bat 还很陌生,所以任何关于我的代码的一般性建议都会很好。我是 运行 闪存驱动器上的一个简单程序,它将加密我闪存驱动器上另一个文件的内容。这些文件可能包含任何键盘字符,因此我需要能够处理所有这些字符。
错误: 看起来即使我使用 delayedExpansinon 我的程序仍然无法处理有毒字符。我的程序没有循环遍历多行(感谢 michael_heath),但是以下错误日志显示了一个新错误。看来 " 仍然是一个问题。同样在测试中我发现它不适用于 % 或 ! in abet 和 cipher1.
注意解决这个问题我试图将 enabledealyedexpansion 添加到整个 encrypt/dycrpt 功能。这确实消除了我涉及 " 的错误,但我的代码不再处理 % 和 !。
程序
@echo off
setlocal
cd BatchMenu/BatchLibs
set "command=%3"
set "drivePath=%2"
set "fileName=%1"
set "abet=abcdefghijklmnopqrstuvwxyz!@# $%^&*()_-+={[}]/?<,>.\|~`':; 0123456789"
set "cipher1=;~`8p#j4*&9zw+}.{a@%^e?[@0u2< r|5)o!(xk_-[cf'1b3g7h=m>,qil/sntdvy:"
if "%command%" == "d" goto dycrypt
goto encrypt
:encrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
setlocal enabledelayedexpansion
set "line=%%~A"
call :encipher
endlocal
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
del "%drivePath%%fileName%.bak"
goto :eof
:dycrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
setlocal enabledelayedexpansion
set "line=%%~A"
call :decipher
endlocal
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
del "%drivePath%%fileName%.bak"
goto :eof
:decipher
set "morf=%abet%"
set "from=%cipher1%"
goto trans
:encipher
set "from=%abet%"
set "morf=%cipher1%"
:trans
set "enil="
:transl
set "=%from%"
set "=%morf%"
:transc
if /i "!line:~0,1!" == "!:~0,1!" (
set "enil=%enil%%:~0,1%"
goto transnc
)
set "=%:~1%"
set "=%:~1%"
IF defined goto transc
:: No translation - keep
set "enil=%enil%%line:~0,1%"
:transnc
set "line=!line:~1!"
if defined line goto transl
if defined enil >&3 echo !enil!
goto :eof
错误日志
E:\BatchMenu\BatchLibs>set "=rstuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=.{a@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "r" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||."
goto transnc
)
E:\BatchMenu\BatchLibs>set "=stuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "={a@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "s" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||{"
goto transnc
)
E:\BatchMenu\BatchLibs>set "=tuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=a@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "t" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||a"
goto transnc
)
E:\BatchMenu\BatchLibs>set "=uvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=@"
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>if /I "|" == "u" (
set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1||@"
goto transnc
)
E:\BatchMenu\BatchLibs>set "=vwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "="
E:\BatchMenu\BatchLibs>IF defined goto transc
E:\BatchMenu\BatchLibs>set "enil=1|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|`:\@{p.{\;w;.8zwwww6zw;8{\*6a.w aw .4paw.*` ;68 pa4w{.++ax|13101368703482566|805152|805152|1|0|0|13101368703615493|1|4aa+{://`w@.{p{.{`4wwz.p8@/wp~;++{/~z;`9~w;.8/pxp`@ap/8*{+z;y*68*v*8@;z`w6ap6a?\w8p=v*pw&`w6ap6a_*8=_1553882_1&`w@.{p_*8=_75015_1|||"
E:\BatchMenu\BatchLibs>set "line="797816-901A5E25AE3F84AAC67979FF01AA681E"|Mon, 29 Feb 2016 11:54:54 GMT|application/vnd.openxmlformats-officedocument.presentationml.presentation|application/vnd.openxmlformats-officedocument.presentationml.presentation"
E:\BatchMenu\BatchLibs>if defined line goto transl
E:\BatchMenu\BatchLibs>set "=abcdefghijklmnopqrstuvwxyz@# $; 0123456789"
E:\BatchMenu\BatchLibs>set "=;~`8p#j4*&9zw+}.{a@"
The syntax of the command is incorrect.
E:\BatchMenu\BatchLibs>if /i """ == "a" (
*NOTE to fix this I attempted to add enabledealyedexpansion to the entire encrypt/dycrpt functionality. This did eliminate this error but my code does not handle % and ! anymore.
@echo off
setlocal enabledelayedexpansion
cd Batch/BatchLibrariesLocation
set "command=%~3"
set "drivePath=%~2"
set "fileName=%~1"
set "abet=abcdefghijklmnopqrstuvwxyz!@# $%%^&*()_-+={[}]/?<,>.\|~`':; 0123456789"
set "cipher1=;~`8p#j4*&9zw+}.{a@%%^e?[@0u2< r|5)o!(xk_-[cf'1b3g7h=m>,qil/sntdvy:"
if "%command%" == "d" goto dycrypt
goto encrypt
:encrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
set "line=%%A"
call :encipher
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
goto :eof
:dycrypt
(
for /f "usebackq delims=" %%A in ("%drivePath%%fileName%.txt") do (
set "line=%%A"
call :decipher
)
) 3> "%drivePath%%fileName%temp.txt"
if exist "%drivePath%%fileName%.txt" ren "%drivePath%%fileName%.txt" "%fileName%.bak2"
ren "%drivePath%%fileName%temp.txt" "%fileName%.txt"
goto :eof
:decipher
set "morf=!abet!"
set "from=!cipher1!"
goto trans
:encipher
set "from=!abet!"
set "morf=!cipher1!"
:trans
set "enil="
:transl
set "=!from!"
set "=!morf!"
:transc
if /i "!line:~0,1!" == "!:~0,1!" set "enil=!enil!!:~0,1!"& goto transnc
set "=!:~1!"
set "=!:~1!"
IF defined goto transc
:: No translation - keep
set "enil=!enil!!line:~0,1!"
:transnc
set "line=!line:~1!"
if defined line goto transl
if not defined enil goto :eof
>&3 echo !enil!
goto :eof
根据需要用双引号引起来的变量和字符串。
带有 for
循环的选项 usebackq
因此可以双引号路径。
输出从 for
循环回显到文件,现在使用
stream 3 以便可以使用 echo on
和任何 echo
使用流 1 不会被重定向到文件。
3>
用于将流 3 重定向到文件,>&3
使用流 3 句柄的命令。
将源文件扩展名重命名为.bak
以便于测试。
如果需要,请改回 del
文件。
使用enabledelayedexpansion
避免回显字符串
使用有毒字符以避免错误。
修复了已知错误,而不是实际加密等。 以后可以是你的研发来做。
更新了脚本顶部的延迟扩展 允许许多变量使用延迟扩展。
结果显示加解密异常(无错误)。
要加密:
"797816-901A5E25AE3F84AAC67979FF01AA681E"|Mon, 29 Feb 2016 11:54:54 GMT|application/vnd.openxmlformats-officedocument.presentationml.presentation|application/vnd.openxmlformats-officedocument.presentationml.presentation
解密:
"7k7d16-k01a5e25ae3fd4aac67k7kff01aa6d1e"4mon, 2k feb 2016 11:54:54 gmt4application/vndropenxmlformats-officedocumentrpresentationmlrpresentation4application/vndropenxmlformats-officedocumentrpresentationmlrpresentation