SSIS Archiving a file - sql-server

I have an SSIS package that outputs a csv file what I want to do is look in the destination folder before I generate a csv and if one already exists (I produce them on a daily basis ) then move it to an archive folder.
my file name structure is "NAME_YYMMDD_HHMM.csv" I'm not sure how I go about looking for a file as the date and time will always be different. i've created a variable for the filename "NAME_*_.csv but not sure how id add this to a file system task? if a file doesn't exist id like it to just move on to next step in package if it does exist then move file to archive folder

You should use a script task to do this:
Use a variable in string file instead which holds your values from YYMMDD since HHMM is unable to get when you created it run time.
Date formats can be found here
Use namespace using System.IO;
string filepath = #"D:\new";
string file = "test_171205_1010.txt";
string fullpath = filepath + #"\" + file;
string newpath = #"D:\arch\";
string newfullpath = newpath + file;
if(
File.Exists(fullpath))
{
File.Move(fullpath, newfullpath);
}
Update
And if you wanna use something about todays date, you can do like below where i look for all files of today and move them. Its up to your what the logic should be.
string txtFile = null;
string txtMoveFile = null;
string date = DateTime.Now.ToString("yyMMdd");
string filepath = #"D:\new";
/*USE THIS LINE IF YOU WANNA USE VARIABLE FROM FOREACH LOOP CONTAINER*/
string file = Dts.Variables["User::name"].Value.ToString() +"_*_*.txt";
//string file = "test_"+date+"_*.txt";
string[] allfilesfromcurrentdate = Directory.GetFiles(filepath, file);
string fullpath = filepath + #"\" + file;
string newpath = #"D:\arch\";
string newfullpath = newpath + file;
foreach(string fileName in allfilesfromcurrentdate)
{
if(fileName.Contains("test_"+date))
{
txtFile = fileName;
txtMoveFile = txtFile.Replace("new", "arch");
if(File.Exists(txtFile))
{
File.Move(txtFile, txtMoveFile);
}
}
}

Related

Python-Change String in file and output all content with the new strings to a new file

i`m trying to scan for a string in all files under a directory and several sub directories , replace it , and when i preformed the replacement save it into a new file,
I managed to
1) search for the string in all the files in the folder and sub folders ,
2) count the amount of appearances of the string
3) create new files for those who had the string .
But for some reason the new files i get are empty , instead of containing the lines of the old files with the replaced string .
Here is my Code :
import os
parentDirectory = "E:\ProjectPython\Test"
oldString = "oldString"
newString = "newString"
if (os.path.isdir(parentDirectory)):
for dirName,directories,files in os.walk(parentDirectory):
for fileName in files:
fullPath = os.path.join(dirName,fileName)
with open(fullPath) as file:
counter=0
for line in file:
if line.__contains__(oldString) or line.__contains__(oldString.lower()):
counter=counter+1
if counter>>0 :
with open(fullPath+".New","w") as newFile:
for line in file:
newFile.write(line.replace(oldString,newString))
print("File:"+fullPath+"\nStrings that were replaced:"+str(counter))
else:
print(parentDirectory+"is missing!")

Find string in log files and return extra characters

How can I get Python to loop through a directory and find a specific string in each file located within that directory, then output a summary of what it found?
I want to search the long files for the following string:
FIRMWARE_VERSION = "2.15"
Only, the firmware version can be different in each file. So I want the log file to report back with whatever version it finds.
import glob
import os
print("The following list contains the firmware version of each server.\n")
os.chdir( "LOGS\\" )
for file in glob.glob('*.log'):
with open(file) as f:
contents = f.read()
if 'FIRMWARE_VERSION = "' in contents:
print (file + " = ???)
I was thinking I could use something like the following to return the extra characters but it's not working.
file[:+5]
I want the output to look something like this:
server1.web.com = FIRMWARE_VERSION = "2.16"
server2.web.com = FIRMWARE_VERSION = "3.01"
server3.web.com = FIRMWARE_VERSION = "1.26"
server4.web.com = FIRMWARE_VERSION = "4.1"
server5.web.com = FIRMWARE_VERSION = "3.50"
Any suggestions on how I can do this?
You can use regex for grub the text :
import re
for file in glob.glob('*.log'):
with open(file) as f:
contents = f.read()
if 'FIRMWARE_VERSION = "' in contents:
print (file + '='+ re.search(r'FIRMWARE_VERSION ="([\d.]+)"',contents).group(1))
In this case re.search will do the job! with searching the file content based on the following pattern :
r'FIRMWARE_VERSION ="([\d.]+)"'
that find a float number between two double quote!also you can use the following that match anything right after FIRMWARE_VERSIONbetween two double quote.
r'FIRMWARE_VERSION =(".*")'

batch to search in a folder

I am trying to write a batch file . which looks in a folder for specific files, takes their path and uses them for calling a java script: for example: i have a folder cbacklog in desktop which include *.xls file and i have a converter.js java script. i want to look in cbacklog if it has excell files , i will take this file path and call converter.js converter will convert this file. then batch file will move to next excell file... take its pats and uses it for convert.js
var fso = new ActiveXObject("Scripting.FileSystemObject");
var xls03Path = WScript.Arguments(0);
xls03Path = fso.GetAbsolutePathName(xls03Path);
var xls95Path = xls03Path.replace("cbacklog", "dbacklog");
xls95Path =xls95Path.replace(/\.xls[^.]*$/, ".xls");
var objExcel = null;
try
{
WScript.Echo("Saving '" + xls03Path + "' as '" + xls95Path + "'...");
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = false;
var objExcl = objExcel.Workbooks.Open(xls03Path);
var wdFormatxls = 39;
objExcl.SaveAs(xls95Path, wdFormatxls);
objExcl.Close();
fso.MoveFile(xls03Path,"C:\\Users\\cguneyel\\Desktop\\cbacklog\\processed\\");
}
finally
{
if (objExcl != null)
{
objExcl.Quit();
}
}
for %%a in ("c:\somewhere\cbacklog\*.xls") do cscript converter.js "%%~fa"
for each xls file in folder call the script with the full path to file as argument

check if a number of files exists in a directory - matlab

How could I check if a number of csv files are present in the current directory?
I have a csv file called PowerOutput.csv, I can see if this exists with
exist('PowerOutput.csv','file')
However, I could have a number of these files e.g. PowerOutput1.csv, PowerOutput2.csv, PowerOutput3.csv and so on.
What is the best way of finding which files exist in a directory?
At the moment I have tried:
TopFolder = pwd;
SubFolder = dir(TopFolder);
SubFolder = {SubFolder.name};
SubFolder(strncmp(SubFolder,'.',1)) = [];
% -- find the number of PowerOutput
num_Power = strncmp({'PowerOutput'}, SubFolder,length('PowerOutput'));
num_Power(num_Power == 0) = [];
num_Power = 1:length(num_Power);
and then I can import the data by:
% -- import inflow
for i = 1:length(num_Power);
filename = fullfile(TopFolder,horzcat('PowerOutput',num2str(num_Power(i)),'.csv'));
fid = fopen(filename);
headers = textscan(fid, '%s%s', 1, 'delimiter',',');
dat = textscan(fid,'%s%f%f','delimiter',',','headerlines',1);
fclose(fid);
end
But this seems like a really long-winded way of doing it. Any suggestions?
use * in dir:
files = dir( fullfile( TopFolder, SubFolder.name, 'PowerOutput*.cvs' ) );

groovy copy files with same last modified date

Hi I want to copy a file from 1 directory to another, but the date has to be the same. so when the last modified date in the fromdirectory is 14:35, I want it to be the same in the todirectory.
How can I do this using groovy?
Using AntBuilder
new AntBuilder().copy ( file : 'path/to/source',
tofile : 'path/to/destination',
preservelastmodified : 'true' )
Using Java/Groovy File API
def source = new File ('path/to/source')
def destination = new File ('path/to/destination')
source.withInputStream { is ->
destination << is
}
destination.lastModified = source.lastModified()

Resources