开发者

Loop through a set of files in one folder

开发者 https://www.devze.com 2023-04-03 22:27 出处:网络
I have a requirement to loop through the particular files in a folder and merge those into one file and want it to achieve using a Batch file. My files look like below.

I have a requirement to loop through the particular files in a folder and merge those into one file and want it to achieve using a Batch file. My files look like below.

ILCY_NEW_20110908123008
ILCY_NEW_20110908123009
ILCY_NEW_20110908123010

meanining, ILCY_NEW_timestamp. The folder will have other files as well but I only need ones with today's timestamp. So I have written the following code to loo开发者_如何学Cp through all the files and combining the names into variable CL.

set tt=%yyyy%%mm%%dd%
for %%f in (ILCY_NEW_%tt%* . *) do set cl=%cl%+%%f 
set cl=%cl:~1%
echo %cl%
copy %cl% ILCY_NEW_CQ.csv

But, only the last file gets selected with this and it only gets copied into ILCY_NEW_CQ.csv ignoring all previous files, even though they have today's timestamp in the name. Can anyone help me here please?


I can't get your date logic to work on my machine, but assuming that your for loop is correctly getting the right filenames, you can do something like this:

set tt=%yyyy%%mm%%dd%
for %%f in (ILCY_NEW_%tt%*.*) do type "%%f" >> ILCY_NEW_CQ.csv

The '>>' will append to the file (as opposed to '>', which would overwrite it). See this page for reference.


Copy command can handle wildcards by itself:

set tt=%yyyy%%mm%%dd%    
copy ILCY_NEW_%tt%*.* ILCY_NEW_CQ.csv


If you need the time stamp for today automatically, use this little for loop with serg's copy command:

FOR /F "USEBACKQ tokens=2-4 delims=/ " %%A IN (`date /t`) DO (
 SET tt=%%C%%A%%B & copy /y "ILCY_NEW_%tt%*.*" "ILCY_NEW_CQ.csv"
)
0

精彩评论

暂无评论...
验证码 换一张
取 消