Check if a directory exists before making it

Advertisement

d0ppler
Guest

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
exit
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?

Reply with quote

Advertisement

Dhirendra
Guest

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
Dhiredra

Reply with quote

AlphaUno
Joined:
Posts:
5
Location:
Nashville, TN

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 (winscp.com /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd Pervasive\Production\test\concentra\" "exit") (
winscp.com /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 ( 
winscp.com /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "mkdir Pervasive\Production\test\concentra\" "exit"
)
-or-
rem %1 is local directory
rem %2 remote directory
rem %3 is file name
pause
cd "c:\Program Files (x86)\WinSCP"
pause
winscp.com /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd ""%2""" "lcd ""%1""" "put ""%3""" "exit"
pause
echo %errorlevel%
pause
rem IF 2 GEQ 15 echo "bigger"
IF %ERRORLEVEL% == 0 (
   Echo No error found
   GOTO exit
} ELSE (
   GOTO error
)
 
echo.errorlevel = %errorlevel%
pause
 
GOTO exit
rem winscp.com /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "lcd ""C:\Users\naa\Cosmos9_Work\Production\jacob\""" "cd ""Pervasive\Production\test\""" "put ""test1.map.xml""" "exit"
 
:error
pause
 
winscp.com /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "mkdir Pervasive\Production\test\concentra\" "exit"
winscp.com /command "option batch ""off""" "option confirm ""off""" "open ""NAA_FTP_TSL""" "cd ""%2""" "lcd ""%1""" "put ""%3""" "exit"
 
pause
 
:exit

Reply with quote

Advertisement

obannala@...
Guest

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
process.Start()
 
''run the commands 
''Open sftp connection with User and Password validations
process.StandardInput.WriteLine("open " + _Host)
process.StandardInput.WriteLine(_UserName)
process.StandardInput.WriteLine(_Password)
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?

Thanks!

Reply with quote

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

Re: Searching existing folders in Sftp Server

obannala@yahoo.com wrote:

But how can I check if a subdirectory/folder in that SFTP site exists?
If you are using VB.NET, use WinSCP .NET assembly instead of interacting with WinSCP scripting interface directly.
https://winscp.net/eng/docs/library
The assembly has Session.FileExists method.
https://winscp.net/eng/docs/library_session_fileexists

Reply with quote

AlphaUno
Joined:
Posts:
5
Location:
Nashville, TN

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.

Reply with quote

Advertisement

Siemandelc
Guest

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

Reply with quote

Martinorosious
Guest

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>"

Reply with quote

Просто мимо ш
Guest

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))
            session.CreateDirectory(curdir);
    }                        
}

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum