Reputation: 77
I have loop, where I'll copy all *.txt files to big.txt in the same directory. In each loop step I want additional copys of different files in different folders with the same first three characters in file name from files in first directory.
Example:
\A\001.txt
\A\003.txt
\A\005.txt
\A\big.txt
\B\001_sth.xml
\B\002_sth.xml - don't copy! no 002 in folder A
\B\003_sth.xml
\B\004_sth.xml - don't copy! no 004 in folder A
\B\005_sth.xml
\B\big.xml
In step 1: copy \A\001.txt to \A\big.txt AND \B\001_sth.xml to \B\big.xml etc
I wrote the batch file to only copy files in directory A, but I have no idea how to find files directory B.
@echo off
if "%1"=="" goto error
for %%x in (%1\A\*.txt) do (
copy %%x %1\A\big.txt
echo %%x
echo ---
)
goto end
:error
echo give me a directory
:end
Upvotes: 2
Views: 5757
Reputation: 34128
Why not just enumerate files in A and then reference files in B?
for %%I in (%1\A\*.txt) do (
copy %1\B\%%~nI_sth.xml %1\B\big.xml
echo %%I
echo ---
)
goto :EOF
(by the way it's not wise to use lower case x for your loop variable because %%~sdfnx all have special meaning)
Upvotes: 3
Reputation: 130819
I don't understand the purpose of your code. Each time you copy to big.txt you over-write the previous copy. I think you want to append instead.
I'm not sure if you want to start with an empty big.txt and big.xml. I'm assuming you do.
Also, your question implies the source text files could have more than 3 characters in the name, even though your example has exactly 3. I'm assuming you want to support more than 3.
If I understand you correctly, I think this will work (untested) Edit - made changes in response to Andriy M comments regarding initial setlocal and avoiding self-copy of big.txt
@echo off
setlocal disableDelayedExpansion
if "%~1"=="" echo Give me a directory&exit /b
set "root=%~1"
if exist "%root%\A\big.txt" del "%root%\A\big.txt"
if exist "%root%\B\big.xml" del "%root%\B\big.xml"
for /f "delims=" %%F in ('dir /b "%root%\A\*.txt"') do (
type "%root%\A\%%~F" >>"%root%\A\big.txt"
set "prefix=%%~nF"
setlocal enableDelayedExpansion
type "!root!\B\!prefix:~0,3!*.xml" >>"!root!\B\big.xml"
endlocal
)
The toggling of the delayed expansion within the loop is only needed if !
appears in any of the paths. Since this is unlikely, you can probably start out with delayed expansion enabled and drop the toggling.
Upvotes: 2
Reputation: 699
These would help;
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
Upvotes: 1