微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在单个变量批处理文件中分配多个值?如果没有,有没有办法以更有效的方式运行它?

如何解决是否可以在单个变量批处理文件中分配多个值?如果没有,有没有办法以更有效的方式运行它?

我这里有一个 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
  )

我更改了以下内容

set /a output+=2 ==> set /a out=%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

解决方法

计划任务将帮助您正确安排所有时间,但看到您已编写代码,我不妨尝试提供帮助。

我不会为您完成所有代码,但我会向您展示想法,您可以完成它:

@echo off
set /p result=<time.txt
timeout /t 60
(set /a result+=60)>time.txt
(call :code%result%)>nul 2>&1
if errorlevel 1 goto :end

:code43200
start "" "AutoHotkey.exe" "D:\UNSORTED\STARTCALLER4.ahk"
do the other things here for code 43200
exit /b

:code172800
start "" "AutoHotkey.exe" "D:\UNSORTED\STARTCALLER4.ahk"
echo do other things here for code 172800
exit /b

:code172860
start "" "AutoHotkey.exe" "D:\SCRIPT\TIMER\PART1.ahk"
echo do other things here for code 172800
exit /b

:end
echo do the end label stuff here.

所以只是一个基本的解释。我仅使用 time.txt 并从文件重定向来读取 set /p 文件的第一行。然后,我只需将 set /a 命令的输出重定向到文件,而不必使用另一行来 echo 结果。

现在我们调用标签,其中 :code%result% 将转换为文件中的结果。然而,存在文件中的结果与任何代码不匹配的问题,这意味着脚本会出错。然而,这有利于这个目的,所以现在我只需检查失败命令的 errorlevel,然后它是 errorlevel 1,它将转到 :end。如果标签匹配,它将转到它,完成工作并退出该标签。

编辑

请尽量不要改变方法:

@echo off
:count
set /p output=<time.txt

:start
timeout /t 2
echo %output%
set /a output+=2
:cut
(echo %output%)>time.txt

(call :code%output%)>nul 2>&1
if errorlevel 1 goto end
goto :start
: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

end:
if %output%  equ 8 (
    set output=0
    goto cut
  ) else (
    goto start
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。