Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Windows批处理从多个文件夹合并多个文件_Windows_Batch File - Fatal编程技术网

如何使用Windows批处理从多个文件夹合并多个文件

如何使用Windows批处理从多个文件夹合并多个文件,windows,batch-file,Windows,Batch File,下面的代码通过合并来自不同目录的多个文件来创建单个输出文件。预期的输出正确,但参数传递到%FileName%的方式不正确 CMD中的示例日志: 批处理文件的代码: @echo on ::This ensures the parameters are resolved prior to the internal variable SetLocal EnableDelayedExpansion SetLocal ENABLEEXTENSIONS rem Get current date and t

下面的代码通过合并来自不同目录的多个文件来创建单个输出文件。预期的输出正确,但参数传递到
%FileName%
的方式不正确

CMD中的示例日志:

批处理文件的代码:

@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%\System32\Find.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:\VP\APC\810I"
dir *.txt /A:-D /B /O:D >"C:\VP\TEST\%APC810_ListFile%"

cd /D "C:\VP\APC\832I"
dir *.txt /A:-D /B /O:D >"C:\VP\TEST\%APC832_ListFile%"

rem  ====================== APC 810 ======================
rem Process each file in the list file.
cd /D "C:\VP\TEST"
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:\VP\TEST"
for /F "delims=" %%F in ( %APC832_ListFile% ) do call :Process832 "%%F"

rem Exit batch file.
endlocal
goto :EOF


:Process810
set "FileName=%~1"
type "C:\VP\APC\810I\%FileName%" >>"C:\VP\MACS_IN_PROCESS\I810MCS.dat"
move "C:\VP\APC\810I\%FileName%" "C:\VP\MACS_IN_ARCHIVE\%FileName%"

:Process832
set "FileName=%~1"
type "C:\VP\APC\832I\%FileName%" >>"C:\VP\MACS_IN_PROCESS\WI832MCS.dat"
move "C:\VP\APC\832I\%FileName%" "C:\VP\MACS_IN_ARCHIVE\%FileName%"

很抱歉,您的帖子没有指明问题所在,您声明输出正确,但传递的参数不正确。(当然,如果你传递的参数不正确,结果也会不正确)

这是一个非常快速的重写,它未经测试,可能包含错误

@Echo关闭
对于('WMIC OS Get LocalDateTime')中的/F“EOL=L”%%a,请在(%%a)中为%%b执行操作
)设置“dt=%%~nb”
设置“Stamp=%dt:~,4%-%dt:~4,2%-%dt:~6,2%\u%dt:~8,2%-%dt:~10,2%-%dt:~12,2%”
对于%%a In(10 32 46 55 56)Do(
Dir/B/A-D/OD“C:\VP\APC\8%%aI\*.txt”>“C:\VP\TEST\APC8%%A\U FLIST\uuu%Stamp%.lst”
对于/F“UseBackQ Delims=“%%b In”(“C:\VP\TEST\APC8%%a\u FLIST\u%Stamp%.lst”
)Do(键入“%%b”>>“C:\VP\MACS\u IN\u PROCESS\I8%%aMCS.dat”
移动“%%b”C:\VP\MACS\u在\u存档中”))

…试试看是否有帮助。

您需要精确地指定您想要什么和实际得到什么!否则,这个问题对其他用户是无用的,因此可能会被关闭。。。
@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%\System32\Find.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:\VP\APC\810I"
dir *.txt /A:-D /B /O:D >"C:\VP\TEST\%APC810_ListFile%"

cd /D "C:\VP\APC\832I"
dir *.txt /A:-D /B /O:D >"C:\VP\TEST\%APC832_ListFile%"

rem  ====================== APC 810 ======================
rem Process each file in the list file.
cd /D "C:\VP\TEST"
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:\VP\TEST"
for /F "delims=" %%F in ( %APC832_ListFile% ) do call :Process832 "%%F"

rem Exit batch file.
endlocal
goto :EOF


:Process810
set "FileName=%~1"
type "C:\VP\APC\810I\%FileName%" >>"C:\VP\MACS_IN_PROCESS\I810MCS.dat"
move "C:\VP\APC\810I\%FileName%" "C:\VP\MACS_IN_ARCHIVE\%FileName%"

:Process832
set "FileName=%~1"
type "C:\VP\APC\832I\%FileName%" >>"C:\VP\MACS_IN_PROCESS\WI832MCS.dat"
move "C:\VP\APC\832I\%FileName%" "C:\VP\MACS_IN_ARCHIVE\%FileName%"