We help IT Professionals succeed at work.

COPY file from the SAME DIRECTORY LOCATION as the SQL Script File

FreightTrain
FreightTrain asked
on
691 Views
Last Modified: 2010-04-21
I am using this copy function and want to make it part of a script deployment.  Is there a way to reference the current directory the script is in as the path.  For instance, I could place this copy script anywhere on a machine and then the FROM line of the COPY script would look like:  FROM '.../TestFile.txt', where the .../ represents the current file location of the script...I just don't want to have to manage changing this, but allow it to be copied to any folder and ran from any location...

COPY pbs."TestFile_Data"
FROM 'C:/MyFolder/TestFile.txt'
WITH DELIMITER E'\t'
CSV HEADER
QUOTE AS E'\t'
NULL AS '';
Comment
Watch Question

CERTIFIED EXPERT

Commented:
with postgresql group privileges you can issue a psql command in a script.
psql -c "COPY pbs.\"TestFile_Data\"
FROM 'C:/MyFolder/TestFile.txt'
WITH DELIMITER E'\t'
CSV HEADER
QUOTE AS E'\t'
NULL AS '';" your_database_name

Author

Commented:
earthman2,

You have been a tremendous help in many of my questions lately...Thank YOU!

What I am looking for is a way to run the script without knowing the drive letter or even the parent folder that the data file is in.  I want to have a Import.sql file that has the COPY command in it and it be in the same folder as the data file...

I am a ASP.NET guy (unfortunately), so if I were to do something like this in ASP.NET I would access the server.mappath('TestFile.txt') and it would return me that file as long as it is in the same location as my web page code is that calls it...

Hope this makes sense about what I am looking for...
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.