2016-11-01 2 views
0

J'ai un fichier CMD Windows qui IFs% 1. Mais si% 1 inclut une parenthèse, le fichier de commandes interprète la parenthèse comme ouvrant une commande ou la fermant, ou les deux.Le fichier CMD échoue si le paramètre% 1 inclut des parenthèses ou des parenthèses

Comment sécuriser IF ou FOR un paramètre nommé, par exemple, "File Copy (3) .txt"?

Il m'a fallu une éternité pour comprendre ce qui se passait, pourquoi parfois il n'y avait pas de problème et parfois ça flippait. J'ai besoin d'étoffer ça un peu plus, de vérifier les erreurs pour le succès ou l'échec plutôt que de supposer, et de restaurer les APPELS que j'ai éliminés en retirant mes cheveux. Si ça aide, je copie dans ce que j'ai.

JE SAIS que "% ~ 1" est bête; Je suis en train d'enlever les guillemets et de les remettre à nouveau. Cela n'a pas aidé.

L'inclusion de SETLOCAL EnableDelayedExpansion en haut du fichier CMD n'aide pas non plus. Qu'est-ce que je ne comprends pas sur le fonctionnement d'IF?

Code suit:

@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

Veuillez répondre. Doit être une erreur de syntaxe là quelque part. Un fichier batch ciblé fonctionne correctement. Cette fois. Mais je n'ai pas essayé non plus les instructions IF imbriquées, donc je peux renouveler cette affaire. –

+0

N'échoue jamais. Je ne peux pas trouver ma propre erreur avant de m'embarrasser en public. Besoin de quelques guillemets autour des variables. Je me demandais pourquoi personne d'autre n'avait un problème similaire. Maintenant je sais! –

Répondre

0

échoue jamais. Je ne peux pas trouver ma propre erreur avant de m'embarrasser en public. Besoin de quelques guillemets autour des variables _fnam. Je me demandais pourquoi personne d'autre n'avait un problème similaire avec des parenthèses. Maintenant je sais!

Nettoyé ce code et fixé quelques résidus de coupe-pâte, et cela fonctionne comme prévu. J'adorerais réécrire avec des appels internes aux étiquettes, mais je pense que pour le moment je vais partir assez bien seul.