Topic "Check if a directory exists before making it"

Author Message
d0ppler

Guest


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:
Code:

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 allready exists, WINSCP gives the error :
Quote:

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 allready 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?
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Please read documentation. If that does not help, come back.
_________________
Martin Prikryl
Dhirendra

Guest


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
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
See example linked above.
AlphaUno
[View user's profile]

Joined: 2012-08-13
Posts: 5
Location: Nashville, TN
prikryl 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.

Code:
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-
Code:
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
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
AlphaUno wrote:
prikryl 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.

I'm sorry, but this is not a programming forum.

Alternatively you can use .NET assembly, if that helps:
http://winscp.net/eng/docs/library
http://winscp.net/eng/docs/library_session_getfiles#example
_________________
Martin Prikryl
obannala@...

Guest


This is how use my winscp to access sftp server.

Code:
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!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
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.
http://winscp.net/eng/docs/library
The assembly has Session.FileExists method.
http://winscp.net/eng/docs/library_session_fileexists
_________________
Martin Prikryl
AlphaUno
[View user's profile]

Joined: 2012-08-13
Posts: 5
Location: Nashville, TN
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.
Siemandelc

Guest


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

Guest


Have you tried using the remote command possibilities? E.g.:

call bash -c "[ -d <Path to directory> ] || mkdir <Path to directory>"
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
See also http://winscp.net/eng/docs/script_checking_file_existence
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License