Im trying to connect my phone to my PC via FTP and open the FTP with Windows Explorer using the cmd.

Below are the codes:

@echo off 
echo Setup connection to ftp 192.168.43.1 
ipconfig | findstr /I "192.168.43.1"
:: Opens ipconfig and searches for string "192.168.43.1" in the output of ipconfig to check if 192.168.43.1 is connected 
if %errorlevel% == 0 ( 
echo Connection set
%windir%\explorer.exe ftp://192.168.43.1:8888/ 
) else ( 
echo Connection not set 
echo Setting up connection... 
netsh wlan connect name="sdc-yayjg"
:: "sdc-yayjg" is the host name of 192.168.43.1
timeout 3 > NUL 
echo Opening ftp 192.168.43.1
ipconfig | findstr /I "192.168.43.1" 
if %errorlevel% == 0 ( 
echo Connection set
%windir%\explorer.exe ftp://192.168.43.1:8888/ 
) else ( 
echo FAIL: Connection not set. 
echo Press any key to exit. 
pause > NUL

I store those codes in a batch file and when I run the batch file, cmd opens up for a millisecond or so and then exits, I mean the cmd window just flashes for a few milliseconds and nothing happens. No connection is set or whatsoever.
The codes work fine without the "findstr" command. I can't remove that command as it is vety much essential to check the network status.

  • What happens to CMD when you place ";pause" at the end of your batch file? ("pause > NUL" doesn't seem to work for you.) – Ultrasonic54321 Apr 5 at 15:40
  • @Ultrasonic54321 I added ';pause', at the end, the same is happening i.e. the window flashing. Didn't notice any changes. – Xlam Apr 5 at 15:40
  • Then obviously findstr isn't finding the string in the ipconfig output. Also your subject said you are trying to ftp from your phone to your desktop, but this would be going the other way around ( you installed an ftp server on your phone? ). – psusi Apr 5 at 15:48
  • @psusi When I directly type ipconfig and hit enter in a cmd window when my PC is connected to my phone, I do see the IP address in the output. The problem occurred only while running it from the batch file along with the other codes. Anyways, the problem seems to be with the 'nested if-else' I had used in my code. If the nested if-else is replaced with 'goto' the codes work fine. See Ultrasonic54321's answer – Xlam Apr 5 at 16:19
up vote 1 down vote accepted

Try this code (edited to improve modularity for future users, and slightly improved code for a better user experience):

@echo off
goto setup
:setup
title FTP Connection w/ network support 

SET "FTP=192.168.43.1"
:: Location of the FTP server

SET "FTPPORT=8888"
:: The FTP server's port

SET "WIRELESS=sdc-yayjg"
:: The network where the FTP server resides

SET "TRIES=0"
:: Please do not tamper with.

SET "MAXTRIES=3"
:: How many tries before failure

SET "TIMETOCONNECT=3"
:: How much time to give to connect to the network

echo Setting up connection to FTP %FTP%...
goto check1
:check1
if %TRIES% LSS %MAXTRIES% (
SET /A "TRIES=TRIES+1"
goto check2
) else (
goto Fail
)
:check2
echo Checking for FTP Server presence...
ipconfig | findstr /I "%FTP%"
:: Opens ipconfig and searches for %FTP%. (in this case it's "192.168.43.1") in the output of ipconfig to check if 192.168.43.1 is connected 
if %errorlevel% EQU 0 ( 
goto 0
) else ( 
echo Presence not found. Assuming connection not set...
goto 1
)
:1
echo Connection not set after %TRIES% times.
echo Setting up connection... 
netsh wlan connect name="%WIRELESS%"
:: This script assumes that WIRELESS (sdc-yayjg) houses the IP (192.168.43.1)
timeout %TIMETOCONNECT% > NUL 
goto check1
:Fail
echo FAIL: Connection not set after %MAXTRIES% tries. 
echo Press any key to exit. 
pause > NUL
exit
)
:0
echo Internet Connection set - Connecting to FTP via Windows Explorer...
%windir%\explorer.exe ftp://%FTP%:%FTPPORT%/ 
echo Connection Sucessful. Windows Explorer should now open...
echo Press any key to close this window.
pause > NUL
exit

In the event that the code above is not working, the original code (that works according to OP with some cosmetic changes) is below.

@echo off 
echo Setting up connection to FTP 192.168.43.1...
goto Check
:check
ipconfig | findstr /I "192.168.43.1"
:: Opens ipconfig and searches for 192.168.43.1 in the output of ipconfig to check if 192.168.43.1 is connected 
if %errorlevel% == 0 ( 
goto 0
) else ( 
goto 1

)
:: Close your if statements!
:1
echo Connection not set 
echo Setting up connection... 
netsh wlan connect name="sdc-yayjg"
:: "sdc-yayjg" is the host name of 192.168.43.1
timeout 3 > NUL 
echo Opening ftp 192.168.43.1
ipconfig | findstr /I "192.168.43.1" 
if %errorlevel% == 0 ( 
goto 0
) else ( 
echo FAIL: Connection not set after three tries. 
echo Press any key to exit. 
pause > NUL
:: Close your if statements!
exit
)
:0
echo Internet Connection set - Connecting to FTP via Windows Explorer...
%windir%\explorer.exe ftp://192.168.43.1:8888/ 
exit
  • I would edit your code to make it more modular, but I am not at my computer right now. – Ultrasonic54321 Apr 5 at 15:54
  • Using your code, the device connects successfuly but does not open the Windows Explorer as it should with your codes. Im not able to figure out why its not opening – Xlam Apr 5 at 15:59
  • Oh wait now its opening up. Dont know what went wrong that time. Thanks – Xlam Apr 5 at 16:01

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.