Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Thank you

It works very well.

Re: Check if a directory exists before making it

Can't you use option batch continue like so? I'm guessing they were trying to not use .NET
option batch on
option confirm off
lcd C:\my_local_files
option batch continue
mkdir 123456
option batch on
put .\123456\orderconf_1234.xml ./123456/orderconf_1234.xml
Просто мимо ш

C# // Check & create remote directory

// Check & create remote directory
if (!session.FileExists(dirTo))
    string[] ss = dirTo.Split('/');
    string curdir = "";
    foreach(string s in ss)
        curdir += '/' + s;
        if (!session.FileExists(curdir))

Using remote command programm

Have you tried using the remote command possibilities? E.g.:
call bash -c "[ -d <Path to directory> ] || mkdir <Path to directory>"

In case you stumble upon this from the google, the Session.FileExists(path) command works for directories as well. :)

follow up

I think we both have the same issue.

Able to do

1. create a directory
2. move files to that directory

Unable to do

1. Check to see if directory exists
2. if it exists move files to it.
3. If it doesn't exist then create dir and then move files to it.

Searching existing folders in SFTP server

This is how use my WinSCP to access SFTP server.
Dim startInfo As New ProcessStartInfo
startInfo.FileName = uWinScpPath
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False
startInfo.CreateNoWindow = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
''Start WINscp
Dim process As New Process
process.StartInfo = startInfo
''run the commands
''Open sftp connection with User and Password validations
process.StandardInput.WriteLine("open " + _Host)
Dim ans = process.StandardOutput.ReadLine
While ans <> Nothing
   If ans.Contains("Session started.") Then
         ans = ans
        Exit While
  End If
  ans = process.StandardOutput.ReadLine
End While
process.StandardInput.WriteLine("cd " + uDestPath)

From the above process, I create sub directory, copy file to it, and delete the file.

But how can I check if a subdirectory/folder in that SFTP site exists?


Needing more clarification after review your link

@martin The example you provide uses JavaScript while I have used some JavaScript the coding is not familiar to me.

My issue is during an automated process I want to check and see if a folder exists on a remote computer and if it does write a file to it.

If it does not mkdir a folder and then write the file.
IF EXIST ( /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd Pervasive\Production\test\concentra\" "exit") ( /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd Pervasive\Production\test\concentra\" "lcd ""C:\Users\naa\Cosmos9_Work\Production\concentra\work\""" "put ""elig_test.20120813.txt""" "exit"
) ELSE ( /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "mkdir Pervasive\Production\test\concentra\" "exit"

rem %1 is local directory
rem %2 remote directory
rem %3 is file name
cd "c:\Program Files (x86)\WinSCP"
pause /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd ""%2""" "lcd ""%1""" "put ""%3""" "exit"
echo %errorlevel%
rem IF 2 GEQ 15 echo "bigger"
   Echo No error found
   GOTO exit
} ELSE (
   GOTO error
echo.errorlevel = %errorlevel%
GOTO exit
rem /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "lcd ""C:\Users\naa\Cosmos9_Work\Production\jacob\""" "cd ""Pervasive\Production\test\""" "put """"" "exit"
pause /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "mkdir Pervasive\Production\test\concentra\" "exit" /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd ""%2""" "lcd ""%1""" "put ""%3""" "exit"

Re: Same problem while using mkdir if folder already exists

See example linked above.

Same problem while using mkdir if folder already exists

Hi Martin

I am using C# to upload files at a folder but if I need to create folder but mkdir command produces error if a folder already exists at FTP server.

Please let me know how can I check its existance at FTP server before creating it.

Thanks in advance

Re: Check if a directory exists before making it

Please read documentation:
Checking file existence
If that does not help, come back.

Check if a directory exists before making it

I am using WinSCP to upload edi-files to an FTP-server with customer number as a subfolder. So my script could look like this:
option batch on
option confirm off
lcd C:\my_local_files
mkdir 123456
put .\123456\orderconf_1234.xml ./123456/orderconf_1234.xml

If the folder 123456 already exists, WinSCP gives the error:

Error creating folder '123456'.
Can't create directory: File exists

The thing is that I don't know whether this folder exists or not on the FTP server, and I don't wan't WINSCP to raise an error if I try to make a folder that already exists. What is the best approach to "fix" this?

Can I with WinSCP say that "only do mkdir my_folder if my_folder doesn't exist" on the remote system?