【发布时间】:2021-07-30 11:25:58
【问题描述】:
我这里有 2 个批处理文件,其行为类似于计时器
第一个批处理文件
@echo off
for /f "Delims=" %%a in (time.txt) do (
set TEXT=%%a
)
timeout 60
set /a "RESULT"=%TEXT%+60
echo %RESULT% > time.txt
REM UNSORTED FILES
IF %RESULT%==43200 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
IF %RESULT%==86400 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
IF %RESULT%==129600 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
IF %RESULT%==172800 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
REM FOLDERMOVER
IF %RESULT%==43200 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
IF %RESULT%==86400 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
IF %RESULT%==129600 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
IF %RESULT%==172800 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
REM ADOBE FILES
IF %RESULT%==172800 (
D:
cd "D:\Desktop Portal\ADOBE OUTPUTS\"
AutoHotkey.exe "D:\Desktop Portal\ADOBE OUTPUTS\ADOBEMOVERCALLER.ahk"
)
REM IMPORT
IF %RESULT%==172800 (
D:
cd "D:\IMPORT\"
AutoHotkey.exe "D:\IMPORT\IMPORTMOVERCALLER.ahk"
)
IF not %RESULT%==172860 (
D:
cd "D:\SCRIPT\TIMER\"
AutoHotkey.exe "D:\SCRIPT\TIMER\PART2.ahk"
) ELSE (
goto end
)
exit
:end
set a=0
goto count
第二个批处理文件
@echo off
for /f "Delims=" %%a in (time.txt) do (
set TEXT=%%a
)
timeout 60
set /a "RESULT"=%TEXT%+60
echo %RESULT% > time.txt
REM UNSORTED FILES
IF %RESULT%==43200 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
IF %RESULT%==86400 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
IF %RESULT%==129600 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
IF %RESULT%==172800 (
D:
cd "D:\UNSORTED\"
AutoHotkey.exe "D:\UNSORTED\STARTCALLER4.ahk"
)
REM FOLDERMOVER
IF %RESULT%==43200 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
IF %RESULT%==86400 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
IF %RESULT%==129600 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
IF %RESULT%==172800 (
C:
cd "\Desktop\"
AutoHotkey.exe "\Desktop\FOLDERMOVERCALLER.ahk"
)
REM ADOBE FILES
IF %RESULT%==172800 (
D:
cd "D:\Desktop Portal\ADOBE OUTPUTS\"
AutoHotkey.exe "D:\Desktop Portal\ADOBE OUTPUTS\ADOBEMOVERCALLER.ahk"
)
REM IMPORT
IF %RESULT%==172800 (
D:
cd "D:\IMPORT\"
AutoHotkey.exe "D:\IMPORT\IMPORTMOVERCALLER.ahk"
)
IF not %RESULT%==172860 (
D:
cd "D:\SCRIPT\TIMER\"
AutoHotkey.exe "D:\SCRIPT\TIMER\PART1.ahk"
) ELSE (
goto end
)
exit
:end
set a=0
goto count
它们的行为都类似于计时器。 (PS 上面的 "set /a "RESULT"=%TEXT%+60" 用于添加 60 秒/1 分钟。它们以 ahk. 脚本相互启动,因此我可以隐藏它们启动它们,而无需提示控制台。
.ahk 脚本里面的代码是这样的
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
run, "D:\SCRIPT\TIMER\PART1 or PART2 .bat"
Exit
批处理文件正在运行,但我正在考虑是否有更有效的方法来执行这些脚本并且不需要太多“如果”。我正在考虑在一个变量中分配多个值,但我什至不确定这是否可能。我也以其他方式开放。
编辑:
这个版本的早期代码现在效率更高了吗?
@echo off
:count
set /p output=<time.txt
:start
timeout /t 2
echo %output%
:cut
set /a out=%output%+2
(echo %out%)> time.txt
(call :code%output%)>nul 2>&1
if errorlevel 1 goto code8
:code2
REM DESKTOP
echo start "" "AutoHotkey.exe" "C:\Users\Eliaz\Desktop\DESKTOPCALLER.ahk"
exit /b
:code4
REM IMPORT
echo start "" "AutoHotkey.exe" "D:\IMPORT\IMPORTCALLER.ahk"
exit /b
:code6
REM UNSORTED
echo start "" "AutoHotkey.exe" "D:\UNSORTED\UNSORTEDCALLER.ahk"
exit /b
:code8
if %out% geq 8 (
set output=0
goto cut
) else (
goto start
)
我更改了以下内容:
设置 /a 输出+=2 ==> 设置 /a 输出=%output%+2
因为它只是在我应用set /a output+=2时关闭
我还将:end 标签更改为:code8,使标签:code8 更易于调用。
这种变化是否正常,现在运行效率更高了吗?
编辑
Example: Which one is correct
这个:
:code2
start "" "D:\SCRIPT\INSTALLER\TIMER\tst - Copy.bat"
exit /b
start "" "D:\SCRIPT\INSTALLER\TIMER\tst - Copy2.bat"
exit /b
或者这个:
:code2
start "" "D:\SCRIPT\INSTALLER\TIMER\tst - Copy.bat"
start "" "D:\SCRIPT\INSTALLER\TIMER\tst - Copy2.bat"
exit /b
【问题讨论】:
-
为什么不将计划任务用于重复性任务? (请参阅
schtasks /create /?了解他们能够做什么;如果您愿意,还有一个GUI。 -
@Stephan - 谢谢,我会期待使用计划任务
标签: batch-file variables autohotkey