Solved

Windows copy two files into one file and rename file

Posted on 2014-01-02
12
411 Views
Last Modified: 2014-01-04
On my windows server I have two files, File1.csv and File2.csv.  I need to concatenate the two files, or append File2.csv to File1.csv, into a third file, File3.csv; either way, as long as the contents of File1.csv is first in the File3_CURRENTDATE.csv

I will need to have this run via a .bat file that will perform the task once a week.  I also need to know how to name the resultant file with the currentdate appended to the name before the .csv suffix.  For example

File1.csv File2.csv >> File3_20140102.csv

Thanks so much for any help you can provide.
0
Comment
Question by:newtoperlpgm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39752639
The easiest you can do is to use copy command, like this:
copy File1.csv + File2.csv File3_20140102.csv

Open in new window

If your files are binary, then use this:
copy /B File1.csv + File2.csv File3_20140102.csv

Open in new window

0
 

Author Comment

by:newtoperlpgm
ID: 39752652
Thank you but I need to automate the process to add the current date to the filename File3.
0
 
LVL 18

Accepted Solution

by:
Jeremy Weisinger earned 500 total points
ID: 39752657
I haven't tested this but I believe it should work.

If your CSV files have headers:
@ECHO OFF
set loop1=1
for /f "tokens=1-4 delims=/ " %%a in ("%date%") do (set fileDate=%%d%%c%%b)
set outfile=file3%filedate%.csv
for %%a in ("file1.csv","file2.csv") do (
  if defined loop1 (
    COPY /y "%%a" %outfile% >nul
    set "loop1="
  ) else (
    FOR /f "skip=1 delims=" %%I IN (%%a) DO >> %outfile% ECHO %%I
  )
) 

Open in new window


If they don't then:
@ECHO OFF
set loop1=1
for /f "tokens=1-4 delims=/ " %%a in ("%date%") do (set fileDate=%%d%%c%%b)
set outfile=file3%filedate%.csv
for %%a in ("file1.csv","file2.csv") do (
  if defined loop1 (
    COPY /y "%%a" %outfile% >nul
    set "loop1="
  ) else (
    type %%a >> %outfile%
  )
) 

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39752679
In powershell you can run this:
start -> run -> powershell
then type the following and save the file as "combine.ps1"
------------------------
Set-ExecutionPolicy unrestricted
$combinedfile="c:\resultfile_" + (get-date).toshortdatestring() + ".csv"
add-content -value (get-content c:\csvfile1.csv) -path $combinedfile.csv
add-content -value (get-content c:\csvfile2.csv) -path $combinedfile.csv
-------------------------

Run the combine.ps1 file as scheduled task if you want to automatically run on schedule.

Input files are c:\csvfile1.csv and c:\csvfile2.csv
Output file: c:\resultfile_02/01/2014.csv

( The date is added based on the day you execute the .ps1 file)

You can refer the following to schedule the above as task in task scheduler.
http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx
0
 

Author Comment

by:newtoperlpgm
ID: 39752713
@Jeremy Weisinger I tested your solution and it worked for me, so in order to run it I will need to add directories in order for it to know where my files that I want to be copied are correct?  Thanks.
0
 

Author Comment

by:newtoperlpgm
ID: 39752721
***update, never mind, I figured that out by changing the format in the (set fileDate=%%d%%b%%c)


Jeremy Weisinger, I also have a question about the date,
if I want to have 20140102 as todays date, how can I change it so that it appears that way.  My date appeared 20140201.
Thank you.
0
 
LVL 3

Expert Comment

by:telczj9
ID: 39752781
change it to fileDate=%%d%%b%%c
0
 
LVL 18

Expert Comment

by:Jeremy Weisinger
ID: 39753132
@newtoperlpgm "Jeremy Weisinger, I also have a question about the date,
if I want to have 20140102 as todays date, how can I change it so that it appears that way.  My date appeared 20140201."

Yes, I usually put the month first so that it's easy to sort by date just by sorting the filename. But it looks like you already figured out how to change the order.

Cheers!
JW
0
 

Author Comment

by:newtoperlpgm
ID: 39755597
I've requested that this question be closed as follows:

Accepted answer: 0 points for newtoperlpgm's comment #a39752713

for the following reason:

solution provided.
0
 
LVL 18

Expert Comment

by:Jeremy Weisinger
ID: 39755598
I believe newtoperlpgm mistakenly choose their own answer as the solution when they stated my answer was correct.
0
 

Author Comment

by:newtoperlpgm
ID: 39756245
oops that is correct Jeremy I inadvertently clicked on the wrong comment to Accept as Solution.  Thanks for noticing.  I will correct.
0
 
LVL 18

Expert Comment

by:Jeremy Weisinger
ID: 39756311
No problem. Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
No More local names in the certificate starting November 2015 4 135
Unix Command -- Challenging  question 7 107
mapAB Challlenge 35 186
Remote login in windows 7 8 74
This article describes how to set permissions to allow a limited-permissions user to start and stop a particular System Service.   It is always best to give users only the permissions that they need to perform their job, so tweaking particular permi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question