I have a Windows 2012 server where I am trying to copy a folder through FTP. The folder contains multiple folders inside it and the size is around 12 GB.
What command can be used to copy the whole tree structure including all the folders and files inside it.
I cannot zip this folder.
Also I have tried using mget* but it copies all files from all the
folders but doesn't created folder structure.
I am unable to use TAR command as the prompt shows "invalid command".
Windows command-line FTP client, the ftp.exe, does not support recursive directory transfers.
You have to use a 3rd party FTP client for that.
For example with WinSCP FTP client, you can use a batch-file like:
winscp.com /command ^
"open ftp://user:password#example.com/" ^
"get /folder/* c:\target\" ^
"exit"
It will automatically download all files and subfolders in the /folder.
For details, see WinSCP guide to automating file transfers from FTP server. There's also a guide for converting Windows ftp.exe script to WinSCP.
(I'm the author of WinSCP)
The target dir is a zip file. You can copy the full zip file into the ftp server using below code.
//Taking source and target directory path
string sourceDir = FilePath + "Files\\" + dsCustomer.Tables[0].Rows[i][2].ToString() + "\\ConfigurationFile\\" + dsSystems.Tables[0].Rows[j][0].ToString() + "\\XmlFile";
string targetDir = FilePath + "Files\\Customers\\" + CustomerName + "\\" + SystemName + "\\";
foreach (var srcPath in Directory.GetFiles(sourceDir))
{
//Taking file name which is going to copy from the sourcefile
string result = System.IO.Path.GetFileName(srcPath);
//If that filename exists in the target path
if (File.Exists(targetDir + result))
{
//Copy file with a different name(appending "Con_" infront of the original filename)
System.IO.File.Copy(srcPath, targetDir + "Con_" + result);
}
//If not existing filename
else
{
//Just copy. Replace bit is false here. So there is no overwiting.
File.Copy(srcPath, srcPath.Replace(sourceDir, targetDir), false);
}
}
Related
I have multiple folders in a single directory and each folder(folder name as per Siteid) contains the .orig file. so I want to copy the latest file created ".orig" file from multiple directories into a single directory.
The file received from ftp in each directory on a weekly basis
Filename format
:PSAN{SiteId}_20190312_TO_20190318_AT_201903191600.txt
I tried to copy a file using windows command
for /R "source" %f in (*.orig) do copy %f "destination"
using this command I able to copy an all .orig file from multiple folders into a single folder but it fetches all file.
can we do modification in command to fetch the latest file?
How to perform this task using an SSIS package or using cmd.
Create two package variables called InputFileNameWithPath and OutputFolderPath. The Former will be dynamically set in the package. The former should be hardcoded to your destination folder name.
Create a ForEachLoop enumerator of type File Enumerator. Set the path to the root folder and select the check box for traverse sub-folders. You may want to add an expression for the FileMask and set it to *.orig that way only the .orig files are moved (assuming there are other files in the directory that you do not want to move) Assign the fully qualified path to the package variable InputFileName.
Then add a System File Task into the ForEachLoop enumerator and have it move the file. from the InputFileNameWithPath to the OutputFolderPath.
You can use a Script Task to achieve that, you can simply store the folders within an array, loop over these folders get the latest file created and copy it to the destination directory (you can achieve this without SSIS), as example:
using System.Linq;
string[] sourcefolders = new string[] { #"C:\New Folder1", #"C:\New Folder2" };
string destinationfolder = #"D:\Destination";
foreach (string srcFolder in sourcefolders)
{
string latestfile = System.IO.Directory.GetFiles(srcFolder, "*.orig").OrderByDescending(x => System.IO.File.GetLastWriteTime(x)).FirstOrDefault();
if (!String.IsNullOrWhiteSpace(latestfile))
{
System.IO.File.Copy(latestfile, System.IO.Path.Combine(destinationfolder, System.IO.Path.GetFileName(latestfile)));
}
}
An example C# Script Task for this is below. This uses SSIS variables for the source and destination folders, and make sure to add these in the ReadOnlyVariables field if that's how you're storing them. Be sure that you have the references listed below as well. As noted below, if a file with the same name exists it will be overwritten to avoid such an error, however I'd recommend confirming that this meets your requirements. The third parameter of File.Copy controls this, and can be omitted as it's not a required parameter.
using System.Linq;
using System.IO;
using System.Collections.Generic;
string sourceFolder = Dts.Variables["User::SourceDirectory"].Value.ToString();
string destinationFolder = Dts.Variables["User::DestinationDirectory"].Value.ToString();
Dictionary<string, string> files = new Dictionary<string, string>();
DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceFolder);
foreach (DirectoryInfo di in sourceDirectoryInfo.EnumerateDirectories())
{
//get most recently created .orig file
FileInfo fi = di.EnumerateFiles().Where(e => e.Extension == ".orig")
.OrderByDescending(f => f.CreationTime).First();
//add the full file path (FullName) as the key
//and the name only (Name) as the value
files.Add(fi.FullName, fi.Name);
}
foreach (var v in files)
{
//copy using key (full path) to destination folder
//by combining destination folder path with the file name
//true- overwrite file if file by same name exists
File.Copy(v.Key, destinationFolder + v.Value, true);
}
Using the dropbox sample code I am working on a Lua script that will run each time a new file is added (a photo in this case) to a specific folder. I see examples to iterate through a folders files to upload them each in turn. What I need is the properties that are passed to the file that will execute each time a new file is written. I'm hoping it will be passed the filename for the last file created so I can use that to upload the file directly to Dropbox or FTP site.
HI someone in japan solved your doubt as the following.
last_filepath = ""
max_mod = 0
fpath = "/DCIM/100__TSB"
for filename in lfs.dir(fpath) do
filepath = fpath .. "/" .. filename
mod = lfs.attributes( filepath, "modification" )
if mod > max_mod then
max_mod = mod
last_filepath = filepath
end
end
basically it set a directory to search for the latest file with newest modification date/time.
details r here
http://dotnsf.blog.jp/archives/1040120555.html
I am making Windows 8.1 Universal App and want to retrieve file,
(local -> folder -> sub Folder -> file) which is stored in Local Folder.
One way is open Local Folder, then open folder, then open sub folder, then file.
But If I know the path, can I simply open the file?
(Like in Windows Phone 8, store.OpenFile())
Is there an API which would return Storage File when passed path?
Example: GetStorageFile("local/folder/subFolder/file") ?
You can use StorageFile.GetFileFromApplicationUriAsync and pass in a URI such as ms-appdata:///local/folder/subfolder/file.txt. This is preferred over GetFileFromPathAsync (#Parad1s3's suggestion) since it is relative to your data folder and won't change if the location of the app changes (eg, on a phone the user can move to or from the SD card).
string path = ApplicationData.Current.LocalFolder + "\\" + "YourFolder" + "\\" + "YourSubfolder" + "\\yourfile.xml";
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync(path);
Hope this helps.
I have a process that creates Windows internet shortcut files (.url). The files are encoded in UTF-8. The files contain an [InternetShortcut] section, where a URL= is specified. In this case, these are file:/// protocol URLs, which allow people to open paths on their LAN. The URLs are all UNC paths.
Normally the process works fine. But when a UNC path contains Unicode characters, such as the "í" from the code sample below, Windows is unable to "find" the URL when an end user tries to open the internet shortcut from Windows Explorer:
A sample file follows:
[InternetShortcut]
URL=file:///\\lt-splourde\d$\POC\Montería Test\
IconIndex=1
When I open the sample .url file above with a text editor, I see the path with the proper Unicode characters. But when I try to open the file from Windows Explorer, in order to access the path, Windows reports that it is unable to access the path, and it seems to mangle the Unicode characters.
The source code that creates these shortcuts follows:
private void CreateShortcutAsUrl(string uncRootPath, string name, string path, int projectId)
{
path = path + (path.EndsWith(#"\") ? "" : #"\");
using (StreamWriter writer = new StreamWriter(
String.Format(#"{0}\{1}\{2}.url", uncRootPath,
ShortcutsDirectory, new FileServerController().SanitizeNameForDirectory(name)),
false, Encoding.UTF8))
{
writer.WriteLine(#"[InternetShortcut]");
writer.WriteLine(#"URL=file:///" + path);
writer.Flush();
}
}
Does anyone know of a solution for this issue?
Thanks!
(I had posted this on superuser originally, but I feel like the content is more programmer oriented)
Try the .NET equivalent of InternetCanonicalizeUrl, which is System.Uri.EscapeUriString, so something like this (assuming your URI is in szOriginalString
String szEscapedString = System.Uri.EscapeUriString(szOriginalString);
Then write szEscapedString as the URI instead of the original.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I dynamically specify a file in DOS?
I am using c#.NET 2.0 to execute DOS commands to ftp a file. All works except for 1 thing, in the cmd file I call, it runs a PUT statement. Right now the put statement has a hardcoded local file path. I need to specify a dynamic path. I've tried
put %~dp0\myfile.DTL myfile.dtl
but it says it can't find the file.
Right now the .NET code calls a BAT file which only exist to call the CMD file. Interestingly, the BAT file DOES successfully use a relative path in its call to the CMD file:
ftp.exe -s:%~dp0\oit.cmd
However, I can't get that relative path to wrok in the cmd file:
open <my host>
<user name>
<password>
put <hardcoded path that needs to be relative path>localfilename remotefilename
I'll bever know where it will exist so I just need to grab whatever local directorey the file is in.
Relative is "." (dot).
Can you post the exact situation? What directory are you in, and where is the file?
Be careful with the "~" char ... it has a special meaning for DOS files (8.3 notation)
Use System.IO.Directory.GetCurrentDirectory() to get the current (working) folder.
Alternatively, if you want the path relative to your .EXE file, you can use System.Reflection.Assembly.GetEntryAssembly().CodeBase to get full path to your .EXE file, then use System.IO.Path.GetDirectoryName() to get the directory name of that path.
After that, you can use System.IO.Path.Combine to get absolute path out of relative:
string absPath = Path.Combine( #"c:\working\folder", #"sub\folder\file.ext" );
// absPath == "c:\working\folder\sub\folder\file.ext
// Works with double-dot too:
string absPath2 = Path.Combine( #"c:\working\folder", #"..\up\file.ext" );
// absPath == "c:\working\up\file.ext