我正在编写一个批处理脚本,使用USMT将计算机从XP升级到7。由于USMT有一个扫描状态组件,需要在操作系统升级之前运行,而加载状态计算机必须在操作系统升级后运行,所以我试图使用if语句检查操作系统是什么,然后运行正确的命令。我是批处理文件的新手,但从我一直读到的所有东西来看,我似乎正在正确地编写它,但我显然是在搞砸了。我得到了一个"Windows是意想不到的在这个时候的错误。“我还知道,由于我包含的暂停命令,变量被正确地设置了。我还尝试使用IF %WINVERSION% == %XP% goto XPTRUE/WIN7TRUE,并将括号中的所有内容都封装在:XPTRUE/WIN7TRUE下面,但这会产生相同的错误。
::Don't have commands print...only outputs are printed
@echo off
:: Set constants
SET XP=Microsoft Windows XP [Version 5.1.2600]
SET WIN7=Microsoft Windows [Version 6.1.7601]
SET XPUSMTLOCATION=C:\Program Files\USMT\Binaries\v4\x86
SET 7USMTLOCATION=C:\Program Files (x86)\USMT\Binaries\v4\amd64
SET BACKUPLOACTION=\\[SERVER IP]\z$\UserAccountBackUps\Backups
SET LOCALBACKUPLOCATION=C:\Backup\USMT
SET NASBACKUPLOCATION=S:\UserAccountBackUps\Backups
@PAUSE
::Get the current version of Windows batch file is running on and store it in WINVERSION
FOR /f "delims=" %%A IN ('ver') DO @SET WINVERSION=%%A
echo %WINVERSION%
PAUSE
::Get the MAC address of the computer and store it in MACA
FOR /F %%A IN ('getmac') DO @SET MACA=%%A
echo The MAC Address is: %MACA%
:: Tell user about script
echo This is a script designed to migrate computers with one network card from Windows XP to Windows 7 using USMT, this script should not be used with computers that have multiple network cards
echo Xp is %XP%
echo 7 is %WIN7%
::Check to see if the current version is XP
PAUSE
IF %WINVERSION% == %XP% (
echo This is windows XP
::Change directory to the location of USMT files
cd %XPUSMTLOCATION%
::Run scanstate to create backup
scanstate.exe C:\Backup /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigApp.xml" /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigDocs.xml" /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigUser.xml" /o /v:2
::Change directory to the location of where the USMT backup is
cd %LOCALBACKUPLOCATION%
::Rename the backup to the MAC Address
rename USMT.MIG %MACA%.MIG
::Map the NAS to a drive because xcopy can not take IP addresses
echo Mapping NAS to drive
::NAS is mapped to drive S, if S is used for something else change s below to different letter
net use s: \\[SERVER IP]\z$
echo Prepairing to copy backup to NAS
::Use xcopy to transfer backup file the /v ensures the files are identical
::This must be done this way because if USMT tries to backup directly to the NAS it tries to overwrite all existing files
xcopy %LOCALBACKUPLOCATION%\%MACA%.MIG %NASBACKUPLOCATION% /v
echo The copy has completed, run this batch file again after OS Upgrade
)
IF %WINVERSION% == %WIN7% (
echo This is Windows 7
PAUSE
)当我在我的Windows 7计算机上运行这个程序时,我得到了以下信息:

我在XP计算机上得到了相同的输出,只不过它告诉我当前的版本是xp。我们将非常感谢您的帮助。
发布于 2014-03-08 00:05:13
以下一行:
FOR /f "delims=" %%A IN ('ver') DO @SET WINVERSION=%%A在WINVERSION变量中存储一个字符串,该字符串包含由空格分隔的几个单词,例如:
SET WINVERSION=Microsoft Windows [Version 6.2.9200]这样,下面这一行:
IF %WINVERSION% == %XP% (扩大到:
IF Microsoft Windows [Version 6.2.9200] == Microsoft Windows XP [Version 5.1.2600] (当然,这会导致语法错误!类型:IF /?以获取更多详细信息。
比较两个可能包含空格的字符串的方法是将它们用引号括起来:
IF "%WINVERSION%" == "%XP%" (https://stackoverflow.com/questions/22262048
复制相似问题