该过程无法访问该文件,因为在打开启动时打开最大化的窗口时,另一个过程错误使用了该文件



我最近制作了代码以打开包含我的作业信息的Excel电子表格。电子表格本身完全包含在一个名为"作业"的文件夹中。不幸的是,我到了一个地步,我希望电子表格可以最大程度地打开。我添加了一个应该使它起作用的代码,但它确实为命令提示符

The process cannot access the file because it is being used by another process

这是我当前正在使用的所有代码:

@echo off
SET "stime=10:00:00.00"
SET "etime=16:00:00.00"
tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
if "%ERRORLEVEL%"=="0" (
set "running=true"
) else (
set "running=false"
)
:start
IF %time% GEQ %stime% (
    IF %time% LEQ %etime% (
        IF %running%==false (
            for %%i in (G:Homework*) do %%i
            GOTO start
        ) Else (
            GOTO start
            )
    ) ELSE (
        GOTO start
        )
) ELSE (
    GOTO start
    )

也许是这样的?

@echo off
SET "stime=10:00:00.00"
SET "etime=16:00:00.00"
:start
tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
if "%ERRORLEVEL%"=="0" (
   set "running=true"
) else (
   set "running=false"
)
IF %time% GEQ %stime% IF %time% LEQ %etime% IF %running%==false (
   for %%i in (G:Homework*) do start /MAX %%i
)
GOTO start

我发现了我搞砸了的地方。该过程尚未检查以查看ILE是否已经打开。一些快速的更改,我已经完成了,因此该过程正确检查并在必要时打开文件。

新代码:

@echo off
SET "stime=10:00:00.00"
SET "etime=16:00:00.00"
:start
tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
if "%ERRORLEVEL%"=="0" (
set "running=true"
) else (
set "running=false"
)
IF %time% GEQ %stime% (
    IF %time% LEQ %etime% (
        IF %running%==false (
            for %%i in (G:Homework*) do start /MAX %%i
            GOTO start
        ) Else (
            GOTO start
            )
    ) ELSE (
        GOTO start
        )
) ELSE (
    GOTO start
    )

尽管这起作用并不理想,我想知道是否有更好的方法使该代码起作用。

最新更新