批处理文件未给出正确的响应

时间:2014-06-09 23:24:11

标签: batch-file

我有一个批处理文件,我需要检查是否存在文本文件。如果检测到文件,则应启动Excel电子表格。如果它没有检测到文件,它应该将一些文件复制到不同的位置。它还需要查看Windows的版本是32位还是64位

这是我的批处理文件(目前无法正常工作)

@echo off
for /f "tokens=*" %%a in ('dir /b /s "%windir%\system32\xcopy.exe"') do set path=%path%;%%~dpa

REM check if windows 64-bit

IF EXIST "%systemdrive%\Program Files (x86)\Auto-BF\installed.txt" (
cd  "%systemdrive%\Program Files (x86)\Auto-BF"
start abf.xlsx
exit
) else (



REM check if windows 32-bit

IF EXIST "%systemdrive%\Program Files\Auto-BF\installed.txt" (
cd  "%systemdrive%\Program Files\Auto-BF"
start abf.xlsx
exit
) else (

REM create Auto-BF Folder
cd "%userprofile%\MarketFeeder Pro 7\profiles\"
md Auto-BF

REM move files_if 64-bit windows
xcopy "c:\Program Files (x86)\Auto-BF\settings.ini" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
xcopy "c:\Program Files (x86)\Auto-BF\customcells.xml" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
xcopy "c:\Program Files (x86)\Auto-BF\ukhorsewin.mfl" "%userprofile%\MarketFeeder Pro 7\mlocator" /h /q /r /y
xcopy "c:\Program Files (x86)\Auto-BF\auto-bf_trial.mft" "%userprofile%\MarketFeeder Pro 7\triggers" /h /q /r /y

REM move files_if 32-bit windows
xcopy "c:\Program Files\Auto-BF\settings.ini" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
xcopy "c:\Program Files\Auto-BF\customcells.xml" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
xcopy "c:\Program Files\Auto-BF\ukhorsewin.mfl" "%userprofile%\MarketFeeder Pro 7\mlocator" /h /q /r /y
xcopy "c:\Program Files\Auto-BF\auto-bf_trial.mft" "%userprofile%\MarketFeeder Pro 7\triggers" /h /q /r /y

ECHO Auto-BF is successfully installed - Program will now launch
PAUSE

cd "%systemdrive%\Program Files\Auto-BF\"
start abf.xlsx
EXIT

1 个答案:

答案 0 :(得分:0)

我建议尝试以下批处理文件:

@echo off
for /f "tokens=*" %%a in ('dir /b /s "%windir%\system32\xcopy.exe"') do set path=%path%;%%~dpa

REM check if Windows 64-bit

IF EXIST "%ProgramFiles(x86)%\Auto-BF\installed.txt" (
   cd /D "%ProgramFiles(x86)%\Auto-BF"
   start abf.xlsx
   goto EndOfBatch
)

REM check if Windows 32-bit

IF EXIST "%ProgramFiles%\Auto-BF\installed.txt" (
   cd /D "%ProgramFiles%\Auto-BF"
   start abf.xlsx
   goto EndOfBatch
)

REM create Auto-BF Folder
cd /D "%userprofile%\MarketFeeder Pro 7\profiles\"
md Auto-BF

IF EXIST "%ProgramFiles(x86)%\Auto-BF\*.*" (
   REM move files_if 64-bit Windows
   xcopy "%ProgramFiles(x86)%\Auto-BF\settings.ini" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
   xcopy "%ProgramFiles(x86)%\Auto-BF\customcells.xml" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
   xcopy "%ProgramFiles(x86)%\Auto-BF\ukhorsewin.mfl" "%userprofile%\MarketFeeder Pro 7\mlocator" /h /q /r /y
   xcopy "%ProgramFiles(x86)%\Auto-BF\auto-bf_trial.mft" "%userprofile%\MarketFeeder Pro 7\triggers" /h /q /r /y
) else (
   REM move files_if 32-bit Windows
   xcopy "%ProgramFiles%\Auto-BF\settings.ini" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
   xcopy "%ProgramFiles%\Auto-BF\customcells.xml" "%userprofile%\MarketFeeder Pro 7\profiles\Auto-BF" /h /q /r /y
   xcopy "%ProgramFiles%\Auto-BF\ukhorsewin.mfl" "%userprofile%\MarketFeeder Pro 7\mlocator" /h /q /r /y
   xcopy "%ProgramFiles%\Auto-BF\auto-bf_trial.mft" "%userprofile%\MarketFeeder Pro 7\triggers" /h /q /r /y
)
ECHO Auto-BF is successfully installed - Program will now launch
PAUSE

cd /D "%ProgramFiles%\Auto-BF\"
start abf.xlsx

:EndOfBatch

它使用环境变量 ProgramFiles(x86) ProgramFiles ,其中第一个在具有32位Windows的计算机上不存在,从而导致替换 ProgramFiles (x86)用空字符串。

如果当前工作目录位于与要更改的目录不同的驱动器上,则在每个 cd 命令中使用选项/D

它解决了批处理文件的主要问题:通过避免嵌套的IF命令,嵌套了IF命令而没有正确使用)

您的IF使用情况:

IF EXIST "%systemdrive%\Program Files (x86)\Auto-BF\installed.txt" (
   rem ...
   exit
) else (
   IF EXIST "%systemdrive%\Program Files\Auto-BF\installed.txt" (
      rem ...
      exit
   ) else (
      rem Where are the closing parentheses for the nested IF commands?

最后我喜欢批处理文件的 1 退出点,因此使用了 goto 命令而不是退出