Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We have a third party app that stores data to directories by date (and then by instance) and we want to copy the *.csv files from that directory to a server. We only want the data from the instance directories -- nothing higher, nothing lower.

Example structure (ItemX is the instance and there can be child directories):

C:\APP
------ Machine1
------ -------- Data
------ -------- ---- 081209
------ -------- ---- ------ Item1 -> report.csv
------ -------- ---- ------ Item2 -> report.csv
------ -------- ---- 081109
------ -------- ---- ------ Item1 -> report.csv
------ -------- ---- ------ Item2 -> report.csv
------ Machine2
------ -------- Data
------ -------- ---- 081209
------ -------- ---- ------ Item1 -> report.csv
------ -------- ---- ------ Item2 -> report.csv
------ -------- ---- 081109
------ -------- ---- ------ Item1 -> report.csv
------ -------- ---- ------ Item2 -> report.csv

Is there a generic way to write this so that we can just create one script instead of one per machine?

share|improve this question
add comment

2 Answers

If the machineX is the name of the machine you can use the environment variable %COMPUTERNAME%

something similar to

xcopy c:\app\%computername%\Data\*\Item?\*.csv target\* /s
share|improve this answer
add comment

Here's one way of potentially doing it. It's not quite command-line, but it can be called from a command-line:

using System;
using System.IO;

namespace MachineInstanceCopyThing
{
    static class Program
    {
    	[STAThread]
    	static void Main ()
    	{
    		string AppDir = @"C:\App";
    		string TargetDir = @"W:\Dest";

    		if (!Directory.Exists (AppDir))
    		{
    			// do something to report the error
    			return;
    		}

    		if (!Directory.Exists (TargetDir))
    		{
    			// do something to report the error
    			return;
    		}

    		DirectoryInfo AppDirInfo = new DirectoryInfo (AppDir);

    		// iterate through machine folders
    		foreach (DirectoryInfo MachineDirectory in AppDirInfo.GetDirectories ())
    		{
    			string DataPath = MachineDirectory.FullName + @"\data";

    			if (!Directory.Exists (DataPath))
    			{
    				// do something to report the error
    				continue;
    			}

    			DirectoryInfo DataDir = new DirectoryInfo (DataPath);

    			// iterate through date directories
    			foreach (DirectoryInfo DateDirectory in DataDir.GetDirectories ())
    			{
    				// iterate through instance directories
    				foreach (DirectoryInfo InstanceDirectory in DateDirectory.GetDirectories ())
    				{
    					string TargetPath = TargetDir + @"\" + InstanceDirectory.Name;

    					// create instance directory at the target
    					Directory.CreateDirectory (TargetPath);

    					foreach (FileInfo CSVFile in InstanceDirectory.GetFiles ("*.csv"))
    					{
    						File.Copy (CSVFile.FullName, TargetPath + @"\" + CSVFile.Name);
    					}
    				}
    			}
    		}
    	}
    }
}

It doesn't do much in the way of error checking aside from ensuring that the source and target exist.

share|improve this answer
    
I've got something similar... –  Austin Salonen Aug 12 '09 at 21:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.