Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: check if file exist over ftp  (Read 21304 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    check if file exist over ftp
    « on: February 16, 2015, 02:40:40 PM »
    I have a backup over FTP and sometimes happend backup didn't finished or even started.
    How can I check over FTP if file exist if not, to run backup again?
    I found this but not quite understand batch.
    Can someone help me with it? Thank you.

    Code: [Select]
    @echo off

    :FETCHFILE
    ftp -s:ftp.txt
    IF EXIST filetocheckfor.txt (
       REM the file was there, so do something
    ) ELSE
       echo Trying again...
       REM ping "sleep" would go here
       GOTO FETCHFILE
    )



    here is my ftp batch for backup. this is txt file which is called with batch
    Code: [Select]
    open 184.50.15.14
    username
    password
    status
    binary
    cd /imagine_zip
    lcd D:\zipano_arhiv\imaginesql
    mdelete *.7z.*
    mput "*.7z.*"
    quit
    del /Q D:\zipano_arhiv\imaginesql\*.*
    close
    quit

    Sometimes also happends that files which are copied over ftp on end side they are zero bytes and have no clue why.

    Linux711



      Mentor

      Thanked: 59
      • Yes
      • Programming Blog
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: check if file exist over ftp
    « Reply #1 on: February 19, 2015, 05:13:06 AM »
    One way to do this would be to use the batch file to generate a temporary ftp script and use that to download the file, then check if it exists on the local machine. This would be a pretty bad approach though.

    I think the easiest way is to download the wget tool for windows: http://users.ugent.be/~bpuype/wget/

    Then in your batch file you'd type something like this:

    Code: [Select]
    @echo off
    start /wait wget ftp://username:password@ip_address/thefolder/thefile.txt
    if exist thefile.txt (
       echo file was found.
    ) else (
       echo file missing.
    )

    I don't know if this will work with spaces in the file name. You probably have to put quotes around it. Also note the forward slashes in the wget's path.
    YouTube

    "Genius is persistence, not brain power." - Me

    "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: check if file exist over ftp
      « Reply #2 on: February 19, 2015, 05:46:38 AM »
      thak you for helping.
      I know that works with ftp too, maybe someone else knows how.

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: check if file exist over ftp
      « Reply #3 on: February 19, 2015, 05:49:13 AM »
      One way to do this would be to use the batch file to generate a temporary ftp script and use that to download the file, then check if it exists on the local machine. This would be a pretty bad approach though.

      It's likely that wget will download and create a zero byte file, but the filesize could then be tested - to see if it the correct filesize too.

      Blisk

        Topic Starter


        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: check if file exist over ftp
        « Reply #4 on: February 19, 2015, 06:44:27 AM »
        I have managed to delete zero bytes files on server so what I need is check if file is there if it is not than ftp backup should run again.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other

        Blisk

          Topic Starter


          Intermediate

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: check if file exist over ftp
          « Reply #6 on: February 19, 2015, 07:27:18 AM »
          http://www.dostips.com/DtTipsFtpBatchScript.php#Batch.FtpBatchUploadOnlyNewFiles

          I see that script is in parts the same like I found out.
          but have no clue what should be in part

           REM the file was there, so do something

          Code: [Select]
          @echo off

          :FETCHFILE
          ftp -s:ftp.txt
          IF EXIST filetocheckfor.txt (
             REM the file was there, so do something
          ) ELSE
             echo Trying again...
             REM ping "sleep" would go here
             GOTO FETCHFILE
          )

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: check if file exist over ftp
          « Reply #7 on: February 19, 2015, 08:33:38 AM »
          It is synching the FTP folder with your local folder!
          If the file does not exist on the FTP site but exists in the folder on your computer it will upload it.

          Blisk

            Topic Starter


            Intermediate

            Thanked: 1
            • Experience: Familiar
            • OS: Windows 7
            Re: check if file exist over ftp
            « Reply #8 on: February 27, 2015, 04:14:00 AM »
            OK this is the script I try to setup folders to upload but not working, maybe I have set some settings wrong.
            Can someone please quick check of it and make some suggestions?
            Thank you.

            Code: [Select]
            @Echo Off
            Setlocal Enabledelayedexpansion

            REM -- Define File Filter, i.e. files with extension .txt
            Set FindStrArgs=/E /D:"*.7z.*"

            REM -- Extract Ftp Script to create List of Files
            Set "FtpCommand=ls"
            Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp"
            Rem Notepad "%temp%\%~n0.ftp"

            REM -- Execute Ftp Script, collect File Names
            Set "FileList="
            For /F "Delims=" %%A In ('"Ftp -v -i -s:"%temp%\%~n0.ftp"|Findstr %FindStrArgs%"') Do (
                Call Set "FileList=%%FileList%% "%%A""
            )

            REM -- Extract Ftp Script to upload files that don't exist in remote folder
            Set "FtpCommand=mput"
            For %%A In (%FileList%) Do set "Exist["%%~A"]=Y"
            For /F "Delims=" %%A In ('"dir /b "%localdir%"|Findstr %FindStrArgs%"') Do (
                If Not defined Exist["%%~A"] Call Set "FtpCommand=%%FtpCommand%% "%%~A""
            )
            Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp"
            rem  Notepad "%temp%\%~n0.ftp"

            For %%A In (%FtpCommand%) Do Echo.%%A

            REM -- Execute Ftp Script, download files
            ftp -i -s:"%temp%\%~n0.ftp"
            Del "%temp%\%~n0.ftp"
            GOTO:EOF


            :extractFileSection StartMark EndMark FileName -- extract a section of file that is defined by a start and end mark
            ::                  -- [IN]     StartMark - start mark, use '...:S' mark to allow variable substitution
            ::                  -- [IN,OPT] EndMark   - optional end mark, default is first empty line
            ::                  -- [IN,OPT] FileName  - optional source file, default is THIS file
            :$created 20080219 :$changed 20100205 :$categories ReadFile
            :$source http://www.dostips.com
            SETLOCAL Disabledelayedexpansion
            set "bmk=%~1"
            set "emk=%~2"
            set "src=%~3"
            set "bExtr="
            set "bSubs="
            if "%src%"=="" set src=%~f0&        rem if no source file then assume THIS file
            for /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do (
                if /i "%%B"=="%emk%" set "bExtr="&set "bSubs="
                if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B)
                if /i "%%B"=="%bmk%"   set "bExtr=Y"
                if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y"
            )
            EXIT /b


            [Ftp Script 1]:S
            !Title Connecting...
            open 184.50.15.14
            username
            password

            !Title Preparing...
            cd /imagine_zip
            lcd D:\zipano_arhiv\imaginesql
            binary
            hash

            !Title Processing... %FtpCommand%
            %FtpCommand%

            !Title Disconnecting...
            disconnect
            bye

            and this are my ftp settings

            Code: [Select]
            open 184.50.15.14
            username
            password
            status
            binary
            cd /imagine_zip
            lcd D:\zipano_arhiv\imaginesql
            mdelete *.7z.*
            mput "*.7z.*"
            quit
            del /Q D:\zipano_arhiv\imaginesql\*.*


            Blisk

              Topic Starter


              Intermediate

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Re: check if file exist over ftp
              « Reply #9 on: February 27, 2015, 07:56:00 AM »
              I get all the time this
              FINDSTR: Bad command line
              FINDSTR: Bad command line


              and don't know why scritp trying to connect
              http://www.dostips.com

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: check if file exist over ftp
              « Reply #10 on: February 28, 2015, 01:41:48 PM »
              No where in that code does it do any type of connection to DosTips.com

              Blisk

                Topic Starter


                Intermediate

                Thanked: 1
                • Experience: Familiar
                • OS: Windows 7
                Re: check if file exist over ftp
                « Reply #11 on: February 28, 2015, 03:20:38 PM »
                No where in that code does it do any type of connection to DosTips.com
                you are right
                I checked again I maded a log from script it doesn't.
                But script still don't work
                First part looks like there is trouble searching file.

                Blisk

                  Topic Starter


                  Intermediate

                  Thanked: 1
                  • Experience: Familiar
                  • OS: Windows 7
                  Re: check if file exist over ftp
                  « Reply #12 on: March 03, 2015, 04:15:44 AM »
                  can someone help me with this please?