abs786123
abs786123

Reputation: 609

Accept two files types in Windows command line

@echo off
setlocal enabledelayedexpansion

set _SRCDIR=C:\Users\hyea\Desktop\Testing\Text Files
set _DSTDIR=C:\Users\hyea\Desktop\Testing\Text Files\TargetFolder

for /f "tokens=*" %%f in ('dir /b /a-d "!_SRCDIR!\*.COS"') do (
type "%_SRCDIR%\header.txt" > "!_DSTDIR!\%%f"
type "!_SRCDIR!\%%f" >> "!_DSTDIR!\%%f"
)

I have the above code which concatenates the header file with the contents of the text file which have an extension of .COS.

Just wondering how would I go about adding another file extension to that of .RCS, so both .COS and .RCS are picked up.

How would the code look like?

Upvotes: 1

Views: 90

Answers (1)

aschipfl
aschipfl

Reputation: 34979

The dir command accepts multiple path/file name/pattern parameters, so simply add the second file type to the command line like this:

@echo off
setlocal enabledelayedexpansion

set "_SRCDIR=C:\Users\hyea\Desktop\Testing\Text Files"
set "_DSTDIR=C:\Users\hyea\Desktop\Testing\Text Files\TargetFolder"

for /f "tokens=*" %%f in ('dir /b /a-d "!_SRCDIR!\*.COS" "!_SRCDIR!\*.RCS"') do (
type "%_SRCDIR%\header.txt" > "!_DSTDIR!\%%f"
type "!_SRCDIR!\%%f" >> "!_DSTDIR!\%%f"
)

In addition, I improved the set command lines and put quotation marks around the whole assignment expressions, which avoids trouble with potential special characters, but does not include the "" in the values of the variables.

Upvotes: 2

Related Questions