Sådan flettes flere filer fra flere mapper ved hjælp af Windows Batch

Indlæg af Hanne Mølgaard Plasc

Problem



Koden nedenfor virker ved at slå sammen flere filer fra forskellige mapper for at oprette en enkelt outputfil. Den forventede udgang er korrekt, men måden argumenterne sendes til \%FileName\% er ikke korrekte.


Prøvelog fra CMD:



C:VPTEST>type "C:VPAPC832I810 - Copy.txt"  1>>"C:VPMACS\_IN\_PROCESSWI832MCS.dat"
The system cannot find the file specified.

C:VPTEST>move "C:VPAPC832I810 - Copy.txt" "C:VPMACS\_IN\_ARCHIVE810 - Copy.txt"
The system cannot find the file specified.



Kode for batch-fil:


@echo on
::This ensures the parameters are resolved prior to the internal variable
SetLocal EnableDelayedExpansion
SetLocal ENABLEEXTENSIONS

rem Get current date and time as local time.
for /f "delims=" \%\%a in ('wmic OS Get localdatetime ^| \%SystemRoot\%System32Find.exe "."') do set dt=\%\%a

rem Reformat the date and time strong to wanted format.
set "YYYY=\%dt:~0,4\%"
set "MM=\%dt:~4,2\%"
set "DD=\%dt:~6,2\%"
set "HH=\%dt:~8,2\%"
set "Min=\%dt:~10,2\%"
set "Sec=\%dt:~12,2\%"
set "TimeStamp=\%YYYY\%-\%MM\%-\%DD\%\_\%HH\%-\%Min\%-\%Sec\%"

rem Define name of the list file containing current date and time in name.
set "APC810\_ListFile=APC810\_FLIST\_\%TimeStamp\%.lst"
set "APC832\_ListFile=APC832\_FLIST\_\%TimeStamp\%.lst"
set "APC846\_ListFile=APC846\_FLIST\_\%TimeStamp\%.lst"
set "APC855\_ListFile=APC855\_FLIST\_\%TimeStamp\%.lst"
set "APC856\_ListFile=APC856\_FLIST\_\%TimeStamp\%.lst"

rem Create file list
cd /D "C:VPAPC810I"
dir *.txt /A:-D /B /O:D >"C:VPTEST\%APC810\_ListFile\%"

cd /D "C:VPAPC832I"
dir *.txt /A:-D /B /O:D >"C:VPTEST\%APC832\_ListFile\%"

rem  ====================== APC 810 ======================
rem Process each file in the list file.
cd /D "C:VPTEST"
for /F "delims=" \%\%F in ( \%APC810\_ListFile\% ) do call :Process810 "\%\%F"


rem  ====================== APC 832 ======================
rem Process each file in the list file.
cd /D "C:VPTEST"
for /F "delims=" \%\%F in ( \%APC832\_ListFile\% ) do call :Process832 "\%\%F"

rem Exit batch file.
endlocal
goto :EOF


:Process810
set "FileName=\%~1"
type "C:VPAPC810I\%FileName\%" >>"C:VPMACS\_IN\_PROCESSI810MCS.dat"
move "C:VPAPC810I\%FileName\%" "C:VPMACS\_IN\_ARCHIVE\%FileName\%"

:Process832
set "FileName=\%~1"
type "C:VPAPC832I\%FileName\%" >>"C:VPMACS\_IN\_PROCESSWI832MCS.dat"
move "C:VPAPC832I\%FileName\%" "C:VPMACS\_IN\_ARCHIVE\%FileName\%"

Bedste reference


Jeg beklager, men dit indlæg angiver ikke problemet, du angiver, at dit output er korrekt, men det er ikke korrekt. (sikkert hvis du sender ukorrekte argumenter, vil resultaterne også være forkerte)


Her er en meget hurtig omskrivning, den er uprøvet og kan indeholde fejl.


@Echo Off

For /F "EOL=L" \%\%a In ('WMIC OS Get LocalDateTime') Do For \%\%b In (\%\%a
) Do Set "dt=\%\%~nb"
Set "Stamp=\%dt:~,4\%-\%dt:~4,2\%-\%dt:~6,2\%\_\%dt:~8,2\%-\%dt:~10,2\%-\%dt:~12,2\%"

For \%\%a In (10 32 46 55 56) Do (
    Dir/B/A-D/OD "C:VPAPC8\%\%aI*.txt">"C:VPTESTAPC8\%\%a\_FLIST\_\%Stamp\%.lst"
    For /F "UseBackQ Delims=" \%\%b In ("C:VPTESTAPC8\%\%a\_FLIST\_\%Stamp\%.lst"
    ) Do (Type "\%\%b">>"C:VPMACS\_IN\_PROCESSI8\%\%aMCS.dat"
        Move "\%\%b" "C:VPMACS\_IN\_ARCHIVE"))


... Prøv det og se om det hjælper .