Directory assistance

Advertisement

PyrateKing
Guest

Directory assistance

Looking for some help determining where I am making a mistake. I have a script that will remove files older than 14 days then get files from the previous day (24 hrs). The issue I am having is that its also bringing over every directory empty as well.
For instance I have directory 01234 with 3 files (1.wav, 2.wav and 3.wav) in it. The creation date (or modified date) is from 3 days back, so I really want to ignore it. I also have directory 56789 with 1 file in it (4.wav) and a creation date (modified date) within the last 24 hours.
Currently the directories 01234 and 56789 are being brought over (with a current timestamp btw) then are being loaded only if it is within the last day. In this case 56789 gets the 4.wav (with the timestamp preserved on the .wav file).

How can I get it to 1) ignore directory 01234 and 2) preserve the timestamp on directory 56789 not just the files in 56789? Any thoughts?

(Side note: I have a batch file passing in the directory at %1% with the /parameter command)
Batch file:
@echo off
 
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set year=%dt:~0,4%
set month=%dt:~4,2%
if %month%==01 set month=Jan
if %month%==02 set month=Feb
if %month%==03 set month=Mar
if %month%==04 set month=Apr
if %month%==05 set month=May
if %month%==06 set month=Jun
if %month%==07 set month=Jul
if %month%==08 set month=Aug
if %month%==09 set month=Sep
if %month%==10 set month=Oct
if %month%==11 set month=Nov
if %month%==12 set month=Dec
set filePath=\\fileserver\Dir1\%year%\%month%\
 
"C:\Program Files (x86)\WinSCP\WinSCP.exe" /console /script=FTPScript.txt /parameter %filePath% /log=FTPlog.txt 
exit
Script:
option confirm off
option batch on
open sftp://UID:PWORD@SomeHost.com/ -hostkey="Some Host Key" 
rm *<14D
get -preservetime -filemask="*>=1D" / %1% 
exit

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,390
Location:
Prague, Czechia

Re: Directory assistance

Option not to transfer "empty" directories in on TODO list:
Issue 207 – Do not synchronize/transfer empty directories

Meanwhile, your only option is to script the transfer using .NET assembly (e.g. in PowerShell).

Your task is somewhat similar to this:
Recursively move files in directory tree to/from SFTP/FTP server while preserving source directory structure
So you can base your script on that.

To preserve timestamps of directories, you can use PreserveTimeDirs settings:
https://winscp.net/eng/docs/rawtransfersettings
(edited in 2022)

Though that affects newly created folders. If you want to update timestamps of modified folders, WinSCP cannot do that. Again, you would have to script it (e.g. in PowerShell as above).

Reply with quote

PyrateKing
Guest

Re: Directory assistance

Awesome! Thank you Martin! I have played around with the .NET assembly and have been able to everything I can with the scripting, and more with file and directory control.

Reply with quote

PyrateKing
Guest

Re: Directory assistance

I am using this in an SSIS package and have been able to build a C# script to delete empty directories with the following:
public void Main()
{
    try
    {
        ProcessDirectory(Dts.Variables["OuterDirectoryYYYYMMM"].Value.ToString());
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    catch (Exception ex)
    {
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}
 
private static void ProcessDirectory(string startLocation)
{
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        ProcessDirectory(directory);
        if (Directory.GetFiles(directory).Length == 0 && Directory.GetDirectories(directory).Length == 0)
        {
            Directory.Delete(directory, false);
        }
    }
}

Reply with quote

Advertisement

You can post new topics in this forum