2016-11-01 8 views
0

IF%1のWindows CMDファイルがあります。しかし、%1に括弧が含まれている場合、バッチファイルは括弧をコマンドを開くか、またはその両方を閉じると解釈します。%1パラメータにかっこまたは括弧が含まれていると、CMDファイルに失敗する

たとえば、 "File Copy(3).txt"という名前のパラメータをIFまたはFORに安全に設定するにはどうすればよいですか?

何が起こっているのかをいつまでも把握することができました。時には問題がなく、時には気にしないこともありました。私はこれをちょっとだけ肉付けし、成功か失敗かを判断するのではなく、エラーチェックをして、私の髪を引き出しながら削除したコールを元に戻す必要があります。それが役に立ったら、私は自分のものをコピーしています。

「%〜1」がダムであることを知っています。引用符をはずして、もう一度引用符で囲みます。それは助けにはなりません。

SETLOCAL EnableDelayedExpansionをCMDファイルの先頭に含めても役に立ちません。

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 
+0

応答してください。どこかの構文エラーである必要があります。ターゲットとするバッチファイルが正常に動作します。今回。しかし、私はそれにネストされたIF文も試していないので、この問題を更新することができます。 –

+0

決して失敗しません。私が自分自身を公然と恥ずかしくするまで私自身の誤りを見つけることはできません。変数の前後に引用符が必要でした。他に誰も同じような問題があったのはなぜだろうかと思った。今私は知っている! –

答えて

0

は失敗することはありません。私が自分自身を公然と恥ずかしくするまで私自身の誤りを見つけることはできません。 _fnam変数の前後に引用符が必要でした。誰も括弧で似たような問題を抱えていたのはなぜだろうか。今私は知っている!

コードをクリーンアップし、カットアンドペースト残渣を修正して、意図したとおりに動作しています。ラベルへの内部呼び出しで書き直すのが大好きですが、この時点では私は十分に十分に残っていると思います。

関連する問題