Commande DOS .bat pour faire des assemblages de fichiers

Résolu
duple -  
barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,

Je dispose du code suivant pour récupérer 2 ou plusieurs fichiers selon la date du jour au format nom_fichier_aaaammjj.txt. La commande principale est comme ceci:
copy fichier1.txt+fichier2.txt destination_fichier_assembl.txt. J'utilise le code ci-dessous pour repondre à la demande, le problème est que cette commande ne marche pas, et je ne sais pas où, j'ai mis l'instruction pause pour voir ce qui affiche mais la encore l'instruction ne marche pas non plus. Pouvez vous me dire où se trouve le problème et comment le résoudre merci ?

Voici mon code :

@echo off

set path = \\fs245\f_public\prod\proj\
set name_file_extract = hourly_extract_ldt_proj_
set /a J=1%DATE:~0,2% - 100
set /a M=1%DATE:~3,2% - 100
set /a A=%DATE:~6,4%
set /a N=((1461 * (%A% + 4800 + (%M% - 14) / 12)) / 4 + (367 * (%M% - 2 - 12 * ((%M% - 14) / 12))) / 12 - (3 * ((%A% + 4900 + (%M% - 14) / 12) / 100)) / 4 + %J% - 32075) %% 7

if %N%==0 set JOUR=Lundi
if %N%==1 set JOUR=Mardi
if %N%==2 set JOUR=Mercredi
if %N%==3 set JOUR=Jeudi
if %N%==4 set JOUR=Vendredi
if %N%==5 set JOUR=Samedi
if %N%==6 set JOUR=Dimanche

SET today=%A%%M%%J%

echo. & echo Nous sommes %JOUR% ... & echo.

echo. & echo Date du jour %J% ... & echo.

if JOUR==Lundi goto Ope1
if JOUR==Mercredi goto Ope3
if JOUR==Jeudi goto Ope4
if JOUR==Vendredi goto Ope5
if JOUR==Samedi goto Ope6

:Ope1
If Not %J% EQU 1 Set /A DJour7=%J%-7
IF %Djour7% LSS 10 set Djour7=0%Djour7%
If Not %J% EQU 1 Set /A DJour6=%J%-6
IF %Djour6% LSS 10 set Djour6=0%Djour6%
If Not %J% EQU 1 Set /A DJour5=%J%-5
IF %Djour5% LSS 10 set Djour5=0%Djour5%
If Not %J% EQU 1 Set /A DJour4=%J%-4
IF %Djour4% LSS 10 set Djour4=0%Djour4%
If Not %J% EQU 1 Set /A DJour3=%J%-3
IF %Djour3% LSS 10 set Djour3=0%Djour3%
If Not %J% EQU 1 Set /A DJour2=%J%-2
IF %Djour2% LSS 10 set Djour2=0%Djour2%
copy %path%%name_file_extract%%Djour7%.txt+%path%%name_file_extract%%Djour6%.txt+%path%%name_file_extract%%Djour5%.txt+%path%%name_file_extract%%Djour4%.txt+%path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt %path%test.txt

:Ope3
If Not %J% EQU 1 Set /A DJour2=%J%-2
IF %Djour2% LSS 10 set Djour2=0%Djour2%
If Not %J% EQU 1 Set /A DJour1=%J%-1
IF %Djour1% LSS 10 set Djour1=0%Djour1%
copy %path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt

:Ope4
If Not %J% EQU 1 Set /A DJour3=%J%-3
IF %Djour3% LSS 10 set Djour3=0%Djour3%
If Not %J% EQU 1 Set /A DJour2=%J%-2
IF %Djour2% LSS 10 set Djour2=0%Djour2%
If Not %J% EQU 1 Set /A DJour1=%J%-1
IF %Djour1% LSS 10 set Djour1=0%Djour1%
copy %path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt

:Ope5
If Not %J% EQU 1 Set /A DJour4=%J%-4
IF %Djour4% LSS 10 set Djour4=0%Djour4%
If Not %J% EQU 1 Set /A DJour3=%J%-3
IF %Djour3% LSS 10 set Djour3=0%Djour3%
If Not %J% EQU 1 Set /A DJour2=%J%-2
IF %Djour2% LSS 10 set Djour2=0%Djour2%
If Not %J% EQU 1 Set /A DJour1=%J%-1
IF %Djour1% LSS 10 set Djour1=0%Djour1%
copy %path%%name_file_extract%%Djour4%.txt+%path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt

:Ope6
If Not %J% EQU 1 Set /A DJour5=%J%-5
IF %Djour5% LSS 10 set Djour5=0%Djour5%
If Not %J% EQU 1 Set /A DJour4=%J%-4
IF %Djour4% LSS 10 set Djour4=0%Djour4%
If Not %J% EQU 1 Set /A DJour3=%J%-3
IF %Djour3% LSS 10 set Djour3=0%Djour3%
If Not %J% EQU 1 Set /A DJour2=%J%-2
IF %Djour2% LSS 10 set Djour2=0%Djour2%
If Not %J% EQU 1 Set /A DJour1=%J%-1
IF %Djour1% LSS 10 set Djour1=0%Djour1%
copy %path%%name_file_extract%%Djour5%.txt+%path%%name_file_extract%%Djour4%.txt+%path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt

pause

5 réponses

  1. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929
     
    Testé et approuvé :

    @echo off
    Setlocal enableextensions enabledelayedexpansion

    set path=\\fs245\f_public\prod\proj\
    set name_file_extract=hourly_extract_ldt_proj_

    set /a J=1%DATE:~0,2%-100
    set /a M=1%DATE:~3,2%-100
    set /a A=%DATE:~6,4%

    set /a N=((1461*(%A%+4800+(%M%-14)/12))/4+(367*(%M%-2-12*((%M%-14)/12)))/12-(3*((%A%+4900+(%M%-14)/12)/100))/4+%J%-32075)%%7

    set fichier=test.txt

    (pushd %path%) || (goto :eof)

    if %N%==0 (set JOUR=Lundi & call :boucle 7 2)
    if %N%==1 (set JOUR=Mardi)
    if %N%==2 (set JOUR=Mercredi & call :boucle 2 1)
    if %N%==3 (set JOUR=Jeudi & call :boucle 3 1)
    if %N%==4 (set JOUR=Vendredi & call :boucle 4 1)
    if %N%==5 (set JOUR=Samedi & call :boucle 5 1)
    if %N%==6 (set JOUR=Dimanche)

    echo. & echo Nous sommes %JOUR% ...
    echo. & echo Date du jour %J% ...
    echo.

    pause
    popd
    Endlocal
    exit

    :boucle
    echo. > %fichier%
    if !M! LSS 10 (set M=0!M!)

    for /L %%A in (%1,-1,%2) do (
    if Not %J% EQU 1 (set /A DJour%%A=%J%-%%A)
    if !Djour%%A! LSS 10 (set Djour%%A=0!Djour%%A!)
    echo %name_file_extract%%A%!M!!Djour%%A!.txt
    type %name_file_extract%%A%!M!!Djour%%A!.txt >> %fichier%
    (echo. & echo.) >> %fichier%
    )
    goto :eof

    rem toujours laisser une ligne vide à la fin



    “L'intelligence artificielle se définit comme le contraire de la bêtise naturelle.”
    2
    1. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929
       
      Alors ???
      0
      1. duple > barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention  
         
        Salut barnabe0057, j'étais un peu pris hier, et n'avais pas trop eu le temps de vérifier. Là j'ai eu le temps de voir et de tester et impec !!! (Y) . C'est parfait, le code est vraiment alléger, quoi que je me perd un peu sur les syntaxes, vu que je suis assez débutant en .bat. Mais le code est top et d'après mon test le résultat correspond bien à ma demande, merci beaucoup !!!
        0
    2. duple
       
      Comment faire pour mettre ce sujet en tant que résolu ?
      0
      1. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929 > duple
         
        Slt tu n'es pas inscrit donc je ne crois pas que tu puisses le passer en résolu, je vais le faire pour toi.

        Tant mieux si le script te convient, si tu as des questions ou des explications à demander, n'hésite pas.
        0
  2. duple
     
    En faisant un echo par étape suivi de pause j'ai trouvé le problème. Le fait de mettre des espaces sur la declaration SET des variables a pétté le programme. Bref, cependant, j'ai encore un petit souci quand je fais :

    IF %M% LSS 10 set M=0%M%
    SET today=%A%%M%%J%
    echo. & echo mOIS %M% ... & echo.
    echo. & echo today %today% ... & echo.

    Cela m'affiche un espace entre le moi et la date du jour (format_resultat=aaaamm dd), d'où peux provenir ce problème ???
    0
    1. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929
       
      Bonjour,

      Tu as peut-être laissé un espace à la fin de cette ligne :
      IF %M% LSS 10 set M=0%M%


      Ton code a besoin d'être simplifié, je vais t'aider.
      0
      1. duple > barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention  
         
        Merci de ta réponse, tu verras la mienne juste en bas. J'ai oublié de cliquer ici en le postant
        0
  3. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929
     
    En simplifiant un peu ça donne ça :

    @echo off
    Setlocal enableextensions enabledelayedexpansion

    set path = \\fs245\f_public\prod\proj\
    set name_file_extract = hourly_extract_ldt_proj_
    set /a J=1%DATE:~0,2% - 100
    set /a M=1%DATE:~3,2% - 100
    set /a A=%DATE:~6,4%
    set /a N=((1461 * (%A% + 4800 + (%M% - 14) / 12)) / 4 + (367 * (%M% - 2 - 12 * ((%M% - 14) / 12))) / 12 - (3 * ((%A% + 4900 + (%M% - 14) / 12) / 100)) / 4 + %J% - 32075) %% 7

    if %N%==0 (set JOUR=Lundi)
    if %N%==1 (set JOUR=Mardi)
    if %N%==2 (set JOUR=Mercredi)
    if %N%==3 (set JOUR=Jeudi)
    if %N%==4 (set JOUR=Vendredi)
    if %N%==5 (set JOUR=Samedi)
    if %N%==6 (set JOUR=Dimanche)

    SET today=%A%%M%%J%

    echo. & echo Nous sommes %JOUR% ... & echo.

    echo. & echo Date du jour %J% ... & echo.

    if JOUR==Lundi (call :Ope1)
    if JOUR==Mercredi (call :Ope3)
    if JOUR==Jeudi (call :Ope4)
    if JOUR==Vendredi (call :Ope5)
    if JOUR==Samedi (call :Ope6)

    Endlocal
    exit

    :Ope1
    call :boucle 2 7
    copy %path%%name_file_extract%%Djour7%.txt+%path%%name_file_extract%%Djour6%.txt+%path%%name_file_extract%%Djour5%.txt+%path%%name_file_extract%%Djour4%.txt+%path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt %path%test.txt
    goto :eof

    :Ope3
    call :boucle 1 2
    copy %path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt
    goto :eof

    :Ope4
    call :boucle 1 3
    copy %path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt
    goto :eof

    :Ope5
    call :boucle 1 4
    copy %path%%name_file_extract%%Djour4%.txt+%path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt
    goto :eof

    :Ope6
    call :boucle 1 5
    copy %path%%name_file_extract%%Djour5%.txt+%path%%name_file_extract%%Djour4%.txt+%path%%name_file_extract%%Djour3%.txt+%path%%name_file_extract%%Djour2%.txt+%path%%name_file_extract%%Djour1%.txt %path%test.txt
    goto :eof

    :boucle
    for /L %%A in (%1,1,%2) do (
    If Not %J% EQU 1 (set /A DJour%%A=%J%-%%A)
    IF !Djour%%A! LSS 10 (set Djour%%A=0!Djour%%A!)
    )
    goto :eof

    rem toujours laisser une ligne vide à la fin



    “L'intelligence artificielle se définit comme le contraire de la bêtise naturelle.”
    0
  4. duple
     
    barnabe0057> Avec ceci çà marche :
    IF %M% LSS 10 set M=0%M%

    SET today=%A%%M%%J%
    SET today=%today: =%
    echo. & echo mOIS %M% ... & echo.
    echo. & echo today %today% ... & echo

    >>> Il fallait rajouter SET today=%today: =% pour éliminer les espaces dans la variable.

    Du coup mon code avec rectification ressemble à ce qui suit, mais comme tu le dis j'aimerai beaucoup l'alléger mon code, j'ai pensé à combiner des conditions if qui vont ensuite paramétrer un boucle for, seulement je débute en programmation batch, et là je ne sais pas comment construire une boucle for avec dans mon cas plusieurs instructions dedans end DOS. De l'aide serait avec plaisir :)

    Voici mon code pour l'instant:

    @echo off

    set path = \\fs245\f_public\prod\proj\
    set name_file_extract = hourly_extract_ldt_proj_

    set /a J=1%DATE:~0,2% - 100
    set /a M=1%DATE:~3,2% - 100
    set /a A=%DATE:~6,4%
    set /a N=((1461 * (%A% + 4800 + (%M% - 14) / 12)) / 4 + (367 * (%M% - 2 - 12 * ((%M% - 14) / 12))) / 12 - (3 * ((%A% + 4900 + (%M% - 14) / 12) / 100)) / 4 + %J% - 32075) %% 7

    if %N%==0 set JOUR=Lundi
    if %N%==1 set JOUR=Mardi
    if %N%==2 set JOUR=Mercredi
    if %N%==3 set JOUR=Jeudi
    if %N%==4 set JOUR=Vendredi
    if %N%==5 set JOUR=Samedi
    if %N%==6 set JOUR=Dimanche

    echo. & echo Nous sommes %JOUR% ... & echo.

    echo. & echo Date du jour %J% ... & echo.

    IF %M% LSS 10 set M=0%M%

    SET today=%A%%M%%J%
    SET today=%today: =%

    if JOUR==Lundi goto Ope1
    if JOUR==Mercredi goto Ope3
    if JOUR==Jeudi goto Ope4
    if JOUR==Vendredi goto Ope5
    if JOUR==Samedi goto Ope6

    pause

    :Ope1
    If Not %J% EQU 1 Set /A DJour7=%J%-7
    IF %Djour7% LSS 10 set Djour7=0%Djour7%
    If Not %J% EQU 1 Set /A DJour6=%J%-6
    IF %Djour6% LSS 10 set Djour6=0%Djour6%
    If Not %J% EQU 1 Set /A DJour5=%J%-5
    IF %Djour5% LSS 10 set Djour5=0%Djour5%
    If Not %J% EQU 1 Set /A DJour4=%J%-4
    IF %Djour4% LSS 10 set Djour4=0%Djour4%
    If Not %J% EQU 1 Set /A DJour3=%J%-3
    IF %Djour3% LSS 10 set Djour3=0%Djour3%
    If Not %J% EQU 1 Set /A DJour2=%J%-2
    IF %Djour2% LSS 10 set Djour2=0%Djour2%
    set fichier1=%path%%name_file_extract%%Djour7%.txt
    set fichier1=%fichier1: =%
    set fichier2=%path%%name_file_extract%%Djour6%.txt
    set fichier2=%fichier2: =%
    set fichier3=%path%%name_file_extract%%Djour5%.txt
    set fichier3=%fichier3: =%
    set fichier4=%path%%name_file_extract%%Djour4%.txt
    set fichier4=%fichier4: =%
    set fichier5=%path%%name_file_extract%%Djour3%.txt
    set fichier5=%fichier5: =%
    set fichier6=%path%%name_file_extract%%Djour2%.txt
    set fichier6=%fichier6: =%
    set fichierDest=%path%%name_file_extract%assemblement_%JOUR%_%today%.txt
    copy %fichier1%+%fichier2%+%fichier3%+%fichier4%+%fichier5%+ %fichierDest%

    :Ope3
    If Not %J% EQU 1 Set /A DJour2=%J%-2
    IF %Djour2% LSS 10 set Djour2=0%Djour2%
    If Not %J% EQU 1 Set /A DJour1=%J%-1
    IF %Djour1% LSS 10 set Djour1=0%Djour1%
    set fichier1=%path%%name_file_extract%%Djour2%.txt
    set fichier1=%fichier1: =%
    set fichier2=%path%%name_file_extract%%Djour1%.txt
    set fichier2=%fichier2: =%
    set fichierDest=%path%%name_file_extract%assemblement_%JOUR%_%today%.txt
    copy %fichier1%+%fichier2% %fichierDest%

    :Ope4
    If Not %J% EQU 1 Set /A DJour3=%J%-3
    IF %Djour3% LSS 10 set Djour3=0%Djour3%
    If Not %J% EQU 1 Set /A DJour2=%J%-2
    IF %Djour2% LSS 10 set Djour2=0%Djour2%
    If Not %J% EQU 1 Set /A DJour1=%J%-1
    IF %Djour1% LSS 10 set Djour1=0%Djour1%
    set fichier1=%path%%name_file_extract%%Djour3%.txt
    set fichier1=%fichier1: =%
    set fichier2=%path%%name_file_extract%%Djour2%.txt
    set fichier2=%fichier2: =%
    set fichier3=%path%%name_file_extract%%Djour1%.txt
    set fichier3=%fichier3: =%
    set fichierDest=%path%%name_file_extract%assemblement_%JOUR%_%today%.txt
    copy %fichier1%+%fichier2%+%fichier3% %fichierDest%

    :Ope5
    If Not %J% EQU 1 Set /A DJour4=%J%-4
    IF %Djour4% LSS 10 set Djour4=0%Djour4%
    If Not %J% EQU 1 Set /A DJour3=%J%-3
    IF %Djour3% LSS 10 set Djour3=0%Djour3%
    If Not %J% EQU 1 Set /A DJour2=%J%-2
    IF %Djour2% LSS 10 set Djour2=0%Djour2%
    If Not %J% EQU 1 Set /A DJour1=%J%-1
    IF %Djour1% LSS 10 set Djour1=0%Djour1%
    set fichier1=%path%%name_file_extract%%Djour4%.txt
    set fichier1=%fichier1: =%
    set fichier2=%path%%name_file_extract%%Djour3%.txt
    set fichier2=%fichier2: =%
    set fichier3=%path%%name_file_extract%%Djour2%.txt
    set fichier3=%fichier3: =%
    set fichier4=%path%%name_file_extract%%Djour1%.txt
    set fichier4=%fichier4: =%
    set fichierDest=%path%%name_file_extract%assemblement_%JOUR%_%today%.txt
    copy %fichier1%+%fichier2%+%fichier3%+%fichier4% %fichierDest%

    :Ope6
    If Not %J% EQU 1 Set /A DJour5=%J%-5
    IF %Djour5% LSS 10 set Djour5=0%Djour5%
    If Not %J% EQU 1 Set /A DJour4=%J%-4
    IF %Djour4% LSS 10 set Djour4=0%Djour4%
    If Not %J% EQU 1 Set /A DJour3=%J%-3
    IF %Djour3% LSS 10 set Djour3=0%Djour3%
    If Not %J% EQU 1 Set /A DJour2=%J%-2
    IF %Djour2% LSS 10 set Djour2=0%Djour2%
    If Not %J% EQU 1 Set /A DJour1=%J%-1
    IF %Djour1% LSS 10 set Djour1=0%Djour1%
    set fichier1=%path%%name_file_extract%%Djour5%.txt
    set fichier1=%fichier1: =%
    set fichier2=%path%%name_file_extract%%Djour4%.txt
    set fichier2=%fichier2: =%
    set fichier3=%path%%name_file_extract%%Djour3%.txt
    set fichier3=%fichier3: =%
    set fichier4=%path%%name_file_extract%%Djour2%.txt
    set fichier4=%fichier4: =%
    set fichier5=%path%%name_file_extract%%Djour1%.txt
    set fichier5=%fichier5: =%
    set fichierDest=%path%%name_file_extract%assemblement_%JOUR%_%today%.txt
    copy %fichier1%+%fichier2%+%fichier3%+%fichier4%+%fichier5% %fichierDest%

    pause
    0
    1. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929
       
      Quand tu postes du code, il faut utiliser les balises de coloration syntaxique.

      Je suis occupé cet après-midi, je regarderai tout ça en fin d'après-midi.
      0
      1. duple > barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention  
         
        Ok, j'en prends note, merci !
        0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. barnabe0057 Messages postés 14329 Date d'inscription   Statut Contributeur Dernière intervention   4 929
     
    J'ai pas eu le temps de tester mais globalement ça donnerait ça :

    @echo off
    Setlocal enableextensions enabledelayedexpansion

    set path=\\fs245\f_public\prod\proj\
    set name_file_extract=hourly_extract_ldt_proj_
    set /a J=1%DATE:~0,2%-100
    set /a M=1%DATE:~3,2%-100
    set /a A=%DATE:~6,4%
    set /a N=((1461*(%A%+4800+(%M%-14)/12))/4+(367*(%M%-2-12*((%M%-14)/12)))/12-(3*((%A%+4900+(%M%-14)/12)/100))/4+%J%-32075)%%7
    set fichier=test.txt

    pushd %path%

    if %N%==0 (set JOUR=Lundi & call :boucle 7 2)
    if %N%==1 (set JOUR=Mardi)
    if %N%==2 (set JOUR=Mercredi & call :boucle 2 1)
    if %N%==3 (set JOUR=Jeudi & call :boucle 3 1)
    if %N%==4 (set JOUR=Vendredi & call :boucle 4 1)
    if %N%==5 (set JOUR=Samedi & call :boucle 5 1)
    if %N%==6 (set JOUR=Dimanche)

    SET today=%A%%M%%J%

    echo. & echo Nous sommes %JOUR% ... & echo.
    echo. & echo Date du jour %J% ... & echo.

    pause
    popd
    Endlocal
    exit

    :boucle
    echo ### > %fichier%

    for /L %%A in (%1,-1,%2) do (
    If Not %J% EQU 1 (set /A DJour%%A=%J%-%%A)
    IF !Djour%%A! LSS 10 (set Djour%%A=0!Djour%%A!)
    type %name_file_extract%!Djour%%A!.txt >> %fichier%
    )

    goto :eof

    rem toujours laisser une ligne vide à la fin



    “L'intelligence artificielle se définit comme le contraire de la bêtise naturelle.”
    0