提问人:TomasRa 提问时间:4/13/2023 最后编辑:TomasRa 更新时间:7/2/2023 访问量:87
在更新文本文件后立即将所有字符转换为 ASCII 的批处理
Batch for convesion of all characters to ASCII immediately after text file is updated
问:
我想请您帮助在 Windows 11 64 位 PC 上运行的脚本。 我需要从脚本中执行 6 个后续操作。
- 检查 Test.txt 文件是否存在。如果找到,请继续。如果未找到,请停止错误消息。
- 执行“iconv -f UTF8 -t ASCII Test.txt > Test_ascii.txt”
- 将 Test.txt 文件的实际保存时间保存到变量中。
- 永久检查 Test.txt 是否已更新(错误,例如:等待 100 毫秒(最多 1 秒))
- 如果 Test.txt 的保存时间不同,则转到“2”。
- 如果 Test.txt 的节省时间相同,请转到“3”。
CTRL+C 执行 EXIT from script。
test.txt 是包含 20-120 个字符的文本文件。
我试图用下面的脚本开始解决这个任务,但不幸的是它不起作用。 我成功测试了外部功能,但是当添加内部功能时,它就停止了。似乎我也无法在 Windows bat-script 中按 $ 声明变量。
@ECHO OFF
:CheckForFile
IF EXIST "Test.txt" (
$timeMod = forfiles /M Test.txt /C "cmd /c echo @ftime
If NOT $timeMod == (forfiles /M Test.txt /C "cmd /c echo @ftime)
{
iconv -f UTF8 -t ASCII Test.txt > Test_ascii.txt
}
start-sleep -Milliseconds 100
goto :CheckForFile )
echo The file Test.txt not found.
pause
exit
我需要知道如何声明变量并填充@time值。 也许它还存在一些更好的方法,如何立即检查该文本文件是否已更新,而不是检查和比较其@time。我试图搜索“Win32目录更改通知API”,不幸的是没有找到真正的功能解决方案。 非常感谢您的帮助。 此致 托马斯
答:
0赞
TomasRa
6/25/2023
#1
我找到了最终的全功能解决方案。
@ECHO OFF
ECHO Script for replacing diacritical characters when input file was updated
setlocal EnableDelayedExpansion
IF EXIST "test.txt" (
set "file=test.txt"
set "hash="
:loop1
set "newhash="
for /f %%a in ('certutil -hashfile "!file!" SHA256 ^| find /v ":"') do set "newhash=%%a"
if not defined hash set "hash=!newhash!"
if "!newhash!" neq "!hash!" (
set "hash=!newhash!"
REM Continue only if file was updated Code for file update:
del Test_ascii.txt.txt
FOR /f "tokens=1,* delims=" %%b in (test.txt) do (
set "line=%%b"
set "line=!line:á=a!"
set "line=!line:č=c!"
set "line=!line:ď=d!"
set "line=!line:é=e!"
set "line=!line:ě=e!"
set "line=!line:í=i!"
set "line=!line:ň=n!"
set "line=!line:ó=o!"
set "line=!line:ř=r!"
set "line=!line:š=s!"
set "line=!line:ť=t!"
set "line=!line:ú=u!"
set "line=!line:ů=u!"
set "line=!line:ý=y!"
set "line=!line:Ž=Z!"
set "line=!line:ž=z!"
set "line=!line:Á=A!"
set "line=!line:Č=C!"
set "line=!line:Ď=D!"
set "line=!line:É=E!"
set "line=!line:Ě=E!"
set "line=!line:Í=I!"
set "line=!line:Ň=N!"
set "line=!line:Ó=O!"
set "line=!line:Ř=R!"
set "line=!line:Š=S!"
set "line=!line:Ť=T!"
set "line=!line:Ú=U!"
set "line=!line:Ů=U!"
set "line=!line:Ý=Y!"
echo !line!>>Test_ascii.txt
)
)
REM End of update script
timeout /t 1 > NUL
goto loop1 )
ECHO The source file test.txt was not found.
pause
exit
我认为这个独特的解决方案可以帮助需要在文本文件中进行任何字符转换的人。
注意力:在 Windows 记事本 (UTF-8) 和 Windows cmd 行为(可能是 CP852,不确定)中,我对非 ASCII 字符的不同编码只有一个大问题。我通过使用 HexaEditor 更正此文件文本中的非 ASCII 字符(例如,十六进制中的 =)解决了这个问题。.bat
á
e1
评论