Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch files - naming output file extensions

Posted on 2014-03-10
11
Medium Priority
?
475 Views
Last Modified: 2014-03-12
The following is the batch file I am using to create a "CSV" output from two SQL tables:

bcp "select * from livedb.dbo.tbl_RCA_DAILY_ATB_HEADERS" queryout "P:\ACCOUNTING\RCA_DAILY_HEADERS.csv" -T -SPTM-DR01 -w -t^|


bcp "select * from livedb.dbo.tbl_RCA_ATB_DAILY" queryout "P:\ACCOUNTING\RCA_ATB_DAILY_Temp.csv" -T -SPTM-DR01 -w -t^|

copy P:\ACCOUNTING\RCA_DAILY_HEADERS.csv + P:\ACCOUNTING\RCA_ATB_DAILY_Temp.csv  P:\ACCOUNTING\CHI\DailyATB\rca_p1_mt_daily_accts_"%date:~10,4%%date:~4,2%%date:~7,2%.csv

The code works well, except that the name of the output file results in a "txt" extension.  My client require a "pipe" delimited "csv" file.

How can I change the above code to make sure the output works like I need it to?

Thanks

glen
0
Comment
Question by:GPSPOW
[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
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39918352
#1, rename the .txt to get rid of it with a
rename [filename.txt] to [filename.csv] if needed.

Try specifying the delimiter as such:

-t^|

| is a command shell item..it gets interpreted by the shell. ^| may escape it.
0
 

Author Comment

by:GPSPOW
ID: 39918362
How do I write it since the date is a variable?

Thanks

Glen
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39918662
CSV, pipe delimited, text file are all just text files.  All depends soley on the output of your bcp command.  Above should give us a CSV file.

Can you give us an example output file from your bcp line?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 25

Expert Comment

by:SStory
ID: 39919233
Don't you need a -o before the output file name? Also it should already be a CSV file before you copy it.  I agree your current code should work.  Can you show us the output...a few lines from each bop and the final version? Change the data as needed to protect info before posting.  Are you saying the -t^| doesn't work?
0
 
LVL 38

Accepted Solution

by:
BillDL earned 1500 total points
ID: 39919836
Was the double quote intentional or accidental in this portion of your COPY command line?:

rca_p1_mt_daily_accts_"%date:~10,4%
0
 

Author Comment

by:GPSPOW
ID: 39921030
I placed double quotes in the title of the new file:

copy P:\ACCOUNTING\RCA_DAILY_HEADERS.csv + P:\ACCOUNTING\RCA_ATB_DAILY_Temp.csv  "P:\ACCOUNTING\CHI\DailyATB\rca_p1_mt_daily_accts_"%date:~10,4%%date:~4,2%%date:~7,2%".csv"

The batch file works when I run it manually and I get afile with a "csv" extension.  However, when the batch file runs from the scheduled task the file has a "txt" extension.

I like the solution of renaming the file.  Since the date is variable, how do I write this?

Thanks

Glen
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39921078
Is this scheduled, or run as a different user in any way btw?  the %date% bits could easily break as %date% could return different formatting.  if so consider using this way to get your date:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

You can always do rename *.txt *.csv

Steve
0
 
LVL 38

Expert Comment

by:BillDL
ID: 39921452
Thanks for clarifying about the double quote.  I had wondered if it had any bearing on the issue, because you forgot to add another at the end of the new file name and extension, and I see that while you were explaining this you placed an extra one in front of the .csv extension.  Obviously just typos here rather than in the actual batch file.
0
 
LVL 25

Expert Comment

by:SStory
ID: 39921839
>The batch file works when I run it manually and I get afile with a "csv" extension.  >However, when the batch file runs from the scheduled task the file has a "txt" extension.

That is beyond bizarre.  Is it for sure calling the same batch file from the scheduled task?

Rename P:\ACCOUNTING\CHI\DailyATB\rca_p1_mt_daily_accts_"%date:~10,4%%date:~4,2%%date:~7,2%.txt P:\ACCOUNTING\CHI\DailyATB\rca_p1_mt_daily_accts_"%date:~10,4%%date:~4,2%%date:~7,2%.csv

There is a space between two file paths.
I do not know why the batch would work differently though.  Double check the scheduled task to be sure you are calling the same script.
0
 

Author Closing Comment

by:GPSPOW
ID: 39921855
BillDL alerted me to m y syntax (grammar) error.  Once I added the double quotes around the path name\file name segment and the extension segment, I got the desired output.

As for the perplexing situation where the batch file did not run via the scheduler but when I did manually...the network administrator copied the batch file into a alternate server folder to allow the scheduler to run properly.  I was unaware that I was not editing the correct "bat" file.  Once I figured this out, the scheduler works as it should.

Again thanks to  all who gave me a possible solution.

Glen
0
 
LVL 38

Expert Comment

by:BillDL
ID: 39922938
Thank you Glen.  It's sometimes the simplest things that we overlook and scratch our heads over for ages.  We've all done it from time to time.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

715 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