P
P
Pavel2019-11-06 11:57:02
SSH
Pavel, 2019-11-06 11:57:02

How can I find out if a folder exists and take an action depending on the result?

I have a winscp synchronization script that works from a bat file

@echo off
del %APPDATA%\WinSCP\WinSCP.log

SET userfolder=foldername
SET server=/home/userfolder

"%APPDATA%\WinSCP\WinSCP.com" ^
  /log="%APPDATA%\WinSCP\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://user:[email protected]/ -hostkey=*" ^
  "cd %server%"^
  "mkdir %userfolder%"^
  "synchronize remote %APPDATA%\ViberPC %server%/%userfolder%/" ^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

exit /b %WINSCP_RESULT%

in fact, if the folder foldername exists, then mkdir gives an error and the whole script is interrupted, how to check for the existence of the folder and, if it exists, continue the script further?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lynn "Coffee Man", 2019-11-06
@mrusklon

https://winscp.net/forum/viewtopic.php?t=15867&sid...
You can set option batch continue before mkdir to ignore any errors and restore the option batch abort after.

M
Moskus, 2019-11-06
@Moskus

Almost verbatim - the very first case of the syntax https://ss64.com/nt/if.html

V
Viktor Taran, 2019-11-06
@shambler81

test ! -d /var/backup/mysql/`date +%Y` && { mkdir -p /var/backup/mysql/`date +%Y` ; }

or
FILE=/etc/resolv.conf
if ; then
    echo "$FILE exist"
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question