SQLTeam.com | Weblogs | Forums

Manipulating files with BCP


#1

Hello guys! I really need your help!
I need to create a file through BCP (SQL SERVER Utility) but I am not able to create a file under the following conditions:
If the file exists, I need to check if its creation date is greater than 90 days if yes I just need to create a new one and I know that the file will automatically be overwritten. However, if the file has less than 90 days of creation, I can not overwrite it, or even need to add the new information from the last point (Line) in which the file was written. That is, in short, there is the possibility of choosing how I can create a file through BCP, such as creating a file from scratch, overwriting, creating from an existing file without overwriting, just adding new information to the file. Sorry for the writing! Thank you very much who can help me! I've tried everything!


#2

PowerShell is probably the best modern way to do this, but I've never bothered to learn that.

I would use a BATCH file and this:

FOR %F IN (*.TXT) DO ECHO %~tF %F

will give you a list of the date/time (and filename) for all *.TXT files. (You need to use %%F if the command was in a BATch file, rather than just used at the command line).

The format of the date/time is subject to change though (depending on the system locale or somesuch), watch out for that.

I would then process the Date/Time parameter in some way - probably pass it to an SProc that was actually responsible for generating (or not ...) the BCP file.