Link to home
Start Free TrialLog in
Avatar of johnormond
johnormondFlag for United States of America

asked on

Concatenation with loop and wildcard

I need a batch file that will concatenate all files for a certain day of the form:
YYMMDDFF.001 or YYMMDDFF.txt (FF= file# <=100 and for each date string, file# is unique)
ex \\dir\03103001.001 + \\dir\03103002.txt \\dir\YYMMDD.txt
I use the following to get the necessary date format:
echo off

FOR /F "TOKENS=1-4 DELIMS=/ " %%i in ('date /t') DO (
 SET MM=%%j
 SET DD=%%k
 SET YYYY=%%l
)
SET YY=%YYYY:~2,2%
SET DATE=%YY%%MM%%DD%
rem echo %DATE%
I just need to know how to concatentate the files with a loop for filename and
a wildcard for extension.  Thanks
Avatar of _nn_
_nn_

First, I'd mention that %DATE% is possibly a not so good choice, since newer Windows systems use it. So, maybe better assume another name, let's say THEDATE.

Then, I would try something along these lines :

@echo off
:: please adapt following line
pushd \\dir
type NUL >%THEDATE%.txt
for /f %%a in ('dir/b %THEDATE%??.*') do call :append %%a
popd
goto end

:append
ren %THEDATE%.txt %THEDATE%.tmp
copy /b %THEDATE%.tmp+%1 %THEDATE%.txt
del /q %THEDATE%.tmp
:end


Note: it would have been possible to build a copy command-line and then execute it. Problem is that from the problem statement, I can see that up to 200 files may have to fit on that command line, which can be too much for the command-line parser (supports up to something like 2000 characters only). So I think that the temporary file idea is a slower, but more reliable solution.
Avatar of johnormond

ASKER

I got your script and I inserted the directory in lines 3 and 5, but the file returned is empty.  Also, I should have said in my original statement that while the value of FF can be up to 99,   it  rarely goes above 5 so the copy line should work... sorry about that.
Hmm, can you please paste your script as it is now ?
here it is... i renamed date to thedate as you suggested...thanks

@echo off
cls
call \\bdc\win\johnscripts\getdate2.bat


pushd \\oa\inter\
type NUL >%THEDATE%.txt
for /f %%a in ('\\oa\inter /b %THEDATE%??.*') do call :append %%a
popd
goto end

:append
ren %THEDATE%.txt %THEDATE%.tmp
copy /b %THEDATE%.tmp+%1 %THEDATE%.txt
del /q %THEDATE%.tmp
:end
ASKER CERTIFIED SOLUTION
Avatar of _nn_
_nn_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!!  That worked perfectly... the copy command-line method was what i was looking for but i guess this way is better, just in case i there are more files to add than anticipated...