如果%1参数包括括号或括号,则CMD文件将失败

时间:2016-11-01 15:33:03

标签: cmd parameters parentheses

我有一个IFs%1的Windows CMD文件。但是,如果%1包含括号,则批处理文件会将括号解释为打开命令或关闭命令或两者都关闭。

我如何安全地IF或FOR参数命名,例如"文件复制(3).txt"?

我花了很长时间才弄清楚发生了什么,为什么有时候没有问题,有时它只是吓坏了。我需要更多地对此进行充实,对成功或失败进行错误检查而不是假设,并且在拔出我的头发时恢复我消除的CALL。但是,如果它有所帮助,我会复制我的内容。

我知道"%~1"愚蠢我将剥离引号并将它们再次放在它周围。这没有帮助。

在CMD文件的顶部包含SETLOCAL EnableDelayedExpansion也无济于事。

我不了解IF如何运作?

代码如下:

@cls                  & REM blank gratuitous error message when run from server
@echo off
SETLOCAL EnableExtensions

::-----------------------------------------------------------------------------
:: ONLY THE NEXT TWO LINES MAY BE CUSTOMIZED BY THE USER
set _CAT=DigitalCAT
set _User=Timothy McGowan
::-----------------------------------------------------------------------------

::-----------------------------------------------------------------------------
:: Set up two more variables
set _Server=\\[path blanked; nonpublic server]
set _Path=%_Server%\%_CAT%\%_User%
::-----------------------------------------------------------------------------

::-----------------------------------------------------------------------------
::----------                                                        top of loop
:Loop
::----------
If "%~1"=="" (goto Done)

:: Get chunks of name from first parameter on command line.
:: _fnam is just file name without path or quotes.
:: _tnam is just file name without path, quotes, or extension.
:: _yr is first four characters of file name, which is supposed to be a year.
:: _mo holds the 6th and 7th characters of the file name and should be
::     among the numbers 01 through 12.
:: Better error checking would determine whether a space or mark of punctuation
::     separates the first four characters.  A file name such as
::     20160126... could get filed in December rather than January.
::     So IF the fifth character is a digit, use that and next; else,
::        use sixth and seventh.
   set _fnam=%~nx1
   set _yr=%_fnam:~0,4%
   set _mox=%_fnam:~5,2%
   if %_mox%==01 (set _mo=01 - January)   & goto MonthSet
   if %_mox%==02 (set _mo=02 - February)  & goto MonthSet
   if %_mox%==03 (set _mo=03 - March)     & goto MonthSet
   if %_mox%==04 (set _mo=04 - April)     & goto MonthSet
   if %_mox%==05 (set _mo=05 - May)       & goto MonthSet
   if %_mox%==06 (set _mo=06 - June)      & goto MonthSet
   if %_mox%==07 (set _mo=07 - July)      & goto MonthSet
   if %_mox%==08 (set _mo=08 - August)    & goto MonthSet
   if %_mox%==09 (set _mo=09 - September) & goto MonthSet
   if %_mox%==10 (set _mo=10 - October)   & goto MonthSet
   if %_mox%==11 (set _mo=11 - November)  & goto MonthSet
   if %_mox%==12 (set _mo=12 - December)  & goto MonthSet
   Goto BadMonth

::----------
:MonthSet            & REM Skip "Goto Badmonth" command, the catchall rerouter.
::----------
:: Find out whether first four digits of file name are a year,
:: and whether that year is a folder's name within user's folder.
:: If not, create folder in NewYear procedure.

   if NOT exist "%_Path%\%_yr%\" (
      echo.
      echo %_yr% is not yet a folder in your storage space.
      echo Is this a new year, or did you typo the year in the file name?
      echo The file's name needs to be in [year-month-day Judge] format.
      echo For instance, "2016-10-22 Perkkio" works, but "20161022 Perkkio" does not.
      echo Your file is named %_fnam%.
      echo.
      echo Either...
      echo Press A to Add %_yr%
      echo or...
      echo Press C to Cancel so you can rename the file.
      choice /N /C CA
      if errorlevel 2 (
         MD "%_Path%\%_yr%"
         echo %date% %time%:  Created %_yr% folder>>"%_Path%\%_yr%\Log.txt"
         goto YearAdded
      )
      if errorlevel 1 (
         set _errFlag=1
         echo.>>"%_Path%\ErrorMessages.txt"
         echo %date% %time%:  ERROR!  FOLLOWING FILE NOT COPIED:>>"%_Path%\ErrorMessages.txt"
         echo %_fnam%>>"%_Path%\ErrorMessages.txt"
         goto End
      )
   )

::----------
:YearAdded
::----------
:: Folder Year should now exist on server.
:: If necessary, quietly create new Month folder in Year folder.
   if NOT exist "%_Path%\%_yr%\%_mo%" (
      MD "%_Path%\%_yr%\%_mo%"
      echo %date% %time%:  Created %_yr%\%_mo% folder>>"%_Path%\%_yr%\Log.txt"

   )

::----------
:: If file already uploaded, handle; else, just copy.
   if exist "%_Path%\%_yr%\%_mo%\%_fnam%" (
      echo.
      echo %_Path%\%_yr%\%_mo%\%_fnam% aleady exists.
      echo.
      echo You may...
      echo    Press R to replace the copy you already uploaded,
      echo.
      echo    Press S to skip this file,
      echo Or...
      echo    Press Q to quit.
      choice /N /C QSR
         if errorlevel 3 (
            copy /y "%~1" "%_Path%\%_yr%\%_mo%"
            echo %date% %time%:  %_fnam% replaced in %_yr%\%_mo%>>"%_Path%\%_yr%\Log.txt"
            SHIFT
            GOTO LOOP
         )
         if errorlevel 2 (
            echo %date% %time%:  %_fnam% not replaced in %_yr%\%_mo%>>"%_Path%\%_yr%\Log.txt"
            SHIFT
            GOTO LOOP
         )
         if errorlevel 1 (
            set _errFlag=1
            echo.>>"%_Path%\ErrorMessages.txt"
            echo %date% %time%:  ERROR!  FOLLOWING FILE NOT COPIED:>>"%_Path%\ErrorMessages.txt"
            echo %_fnam%>>"%_Path%\ErrorMessages.txt"
            goto End
         )

   ) ELSE (
      copy "%~1" "%_Path%\%_yr%\%_mo%"
      echo %date% %time%:  %_fnam% copied to %_yr%\%_mo%>>"%_Path%\%_yr%\Log.txt"
      SHIFT
      GOTO LOOP
   )

::                                                               bottom of loop
::-----------------------------------------------------------------------------


::-----------------------------------------------------------------------------
::----------
:BadMonth
::----------
   echo.
   echo ERROR!
   echo "%_mox%" is not a valid month.
   echo The file's name needs to be in [year-month-day Judge] format.
   echo For instance, "2016-10-22 Perkkio" works, but "20161022 Perkkio" does not.
   echo.
   echo Your file is named:
   echo %_fnam%
   echo.
   echo Please rename the file and use this utility again.
goto VeryEnd
::-----------------------------------------------------------------------------


::-----------------------------------------------------------------------------
::----------
:Done
::----------
   echo.
   echo No more files found to copy.
goto End
::-----------------------------------------------------------------------------


::-----------------------------------------------------------------------------
::----------                              Allow user to read results on screen.
:End
::----------
if .%_errFlag%.==.1. (
   type "%_Path%\ErrorMessages.txt"
   echo.
   echo NOTE FILES NOT COPIED ABOVE.
)
echo.
pause

::----------
:VeryEnd
::----------
:: cls

1 个答案:

答案 0 :(得分:0)

永远不会失败。在我公开尴尬之前,我无法找到自己的错误。在_fnam变量周围需要一些引号。想知道为什么其他人没有与括号有类似的问题。现在我知道了!

清理了该代码并修复了一些剪切和粘贴的残留物,并且它按预期工作。我希望通过内部调用标签来重写,但我认为在这一点上我会留下足够的好处。