Ampersand似乎正在拆分我的变量 - 批处理文件

时间:2016-02-24 05:23:19

标签: batch-file cmd

我有以下代码可以很好地运作

set "findest=\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\%PrimaryApplicantLast%, %PrimaryApplicantlFirst%"
.....
@echo %DATE% %time:~0,-6% [%findest%] >> log.txt

当我运行相同的命令时,但%findest%稍微长一点且包含&符号,它似乎拆分了%findest%变量,或者只是在&amp ;.之后。

问题代码 -

SET "findest=\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\%PrimaryApplicantLast%, %PrimaryApplicantlFirst% & %SecondaryApplicantLast%, %SecondaryApplicantFirst%"
....
@echo %DATE% %time:~0,-6% [%findest%] >> log.txt

我的代码中还有很多内容,但是写入我的日志文件会引发错误,错误是%SecondaryApplicantLast%isn' ta有效命令,让我相信我的代码已被拆分。

错误 -

Press any key to continue . . .
Wed 24/02/2016 13:21 [\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\EXAMPLE, test

'EXAMPLE2' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .

干杯

编辑 -

@echo
setlocal enableDelayedExpansion
echo.


echo. Client Folder Creator! 
echo.
echo. Follow the instructions to create appropriate client folders. 
echo --------------------------------------------------------------------------------

:choice
Set /P c=".Are there multiple applicants[Y/N]?"


:Primary
echo.
echo "Primary Applicant" 
echo.
set /P "PrimaryApplicantlFirst=Enter First Name: "
set /P "PrimaryApplicantLast=Enter Last Name: "
echo.

call :toUpper PrimaryApplicantLast


if /I "%c%" EQU "N" goto :CreateFolders
if /I "%c%" EQU "Y" goto :Secondary


:Secondary
echo "Secondary Applicant" 
echo.
set /P "SecondaryApplicantFirst=Enter First Name: "
set /P "SecondaryApplicantLast=Enter Last Name: "
echo.

call :toUpper SecondaryApplicantLast


goto :CreateFoldersMulti


:CreateFoldersMulti
SET "findest=\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\%PrimaryApplicantLast%, %PrimaryApplicantlFirst% ^& %SecondaryApplicantLast%, %SecondaryApplicantFirst%"
robocopy Z:\ProjectIT\BAT\test "%findest%" /e /NFL /NDL /NJH /NJS
SET blanknum=1
SET "finsubdest=-BLANK-%PrimaryApplicantLast% & %SecondaryApplicantLast%."

IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 
IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 
IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 
IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 

robocopy Z:\ProjectIT\BAT\Construction "%findest%\%blanknum%%finsubdest%" /e /NFL /NDL /NJH /NJS


echo Folder has been created for "%PrimaryApplicantLast% & %SecondaryApplicantLast%"
echo.
@echo "%findest%"
echo.


pause


@echo %DATE% %time:~0,-6% [%findest%] >> log.txt


%SystemRoot%\explorer.exe "%findest%"

pause

goto :eof


:CreateFolders
set "source=Z:\ProjectIT\BAT\test"
set "findest=\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\%PrimaryApplicantLast%, %PrimaryApplicantlFirst%"
robocopy "%source%" "%findest%" /e /NFL /NDL /NJH /NJS
SET blanknum=1
set finsubdest=-BLANK-%PrimaryApplicantLast%

IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 
IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 
IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 
IF EXIST "%findest%\%blanknum%%finsubdest%" ( 
    SET /A blanknum=blanknum+1     
) 

robocopy Z:\ProjectIT\BAT\Construction "%findest%\%blanknum%%finsubdest%" /e /NFL /NDL /NJH /NJS


echo Folder has been created for "%PrimaryApplicantLast%"
echo.
echo %findest%
echo.


pause


@echo %DATE% %time:~0,-6% [%findest%] >> log.txt



%SystemRoot%\explorer.exe "%findest%"

goto :eof
:---------------------------------------------------------------------


:toUpper str -- converts lowercase character to uppercase
if not defined %~1 EXIT /b
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
            "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
            "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
            "ö=Ö" "ü=Ü") do (
    call set %~1=%%%~1:%%~a%%
)
EXIT /b

1 个答案:

答案 0 :(得分:1)

使用插入符 ^ & 之前:^&

如果明智地使用插入符号,将关闭以下字符的特殊含义(& 是“连接命令”)

@ECHO OFF
SETLOCAL
SET "PrimaryApplicantLast=PAL"
:: Examine the next SET very carefully. It's copied directly from your original code...
SET "PrimaryApplicantlFirst=PAF"
SET "SecondaryApplicantLast=SAL"
SET "SecondaryApplicantFirst=SAF"
SET "findest=\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\%PrimaryApplicantLast%, %PrimaryApplicantlFirst% ^& %SecondaryApplicantLast%, %SecondaryApplicantFirst%"
SET fi
@echo %DATE% %time:~0,-6% [%findest%]

GOTO :EOF

对我而言:

findest=\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\PAL, PAF ^& SAL, SAF
24/02/2016 13:45 [\\CFFS\CF_Networked_Files\!!CLIENT DOCUMENTS\PAL, PAF & SAL, SAF]