user2881052
user2881052

Reputation: 13

Command script to Set Filenames to Variables

I'm trying to find a way to copy two *.log files into one file. My intent was to use For /R to set each filename to a variable. Not as easy as I thought. The log files will have different random names each week that I want to combine into one file called Kip.log.

I only got as far as:

FOR /R D:\Temp %F in (*.log) do echo %~nxF

I don't know how to get further.

Upvotes: 1

Views: 152

Answers (4)

foxidrive
foxidrive

Reputation: 41234

Assuming the two log files are in d:\temp then this will suffice.

@echo off
pushd "d:\temp"
copy *.log kip.txt
ren kip.txt kip.log

Upvotes: 3

David Candy
David Candy

Reputation: 743

If you can specify just the files you want using wildcards then this will work

copy *.log kip.log

or

copy a*.log+b*.log kip.log

Type copy /? for Help

Upvotes: 2

Magoo
Magoo

Reputation: 80023

@ECHO OFF
SETLOCAL
(
 FOR /r D:\temp %%F IN (
  zd*.*
  ) DO TYPE "%%~fF"&echo========%%~fF============
)>KIP.LOG
GOTO :EOF

You don't say why you need to set two variables; for the concatenation task, it's not necessary.

the &echo========%%~fF============ above simply adds a separator with the filename found. It's not required and can be omitted if you wish.

If you "need" the filenames in variables simply to delete them, then

del /s d:\temp*.log

would make short work of that task.

Upvotes: 0

Monacraft
Monacraft

Reputation: 6630

To set the variables (and then append) do this:

pushd D:\Test
dir /b *.log >> files.tmp
setlocal enabledelayedexpansion
set count=0
for /f %%a in (files.tmp) do (
set /a count+=1
set "file!1!=%%~a"
)
:: Now you have 2 variables (or more) all file[number]
:: Below is to append
copy "%file1%"+"%file2%" Kip.log

Hope that helped.

Upvotes: 0

Related Questions