Getting output stored into variable from piped commands

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Getting output stored into variable from piped commands

#1 Post by BoQsc » 17 Apr 2016 01:26

The problem is in FOR loop, echo 'dir %remote_servers_file% | ftp -A localhost what did i missed?
CMD automaticaly closes after running batch file instead of staying and displaying the values.


Code: Select all

echo ---------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (echo 'dir %remote_servers_file% | ftp -A localhost') DO (
  SET var!count!=%%F
  SET /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL
echo ---------------
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting output stored into variable from piped commands

#2 Post by foxidrive » 17 Apr 2016 06:30

BoQsc wrote:echo ---------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (echo 'dir %remote_servers_file% | ftp -A localhost') DO (
SET var!count!=%%F
SET /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL
echo ---------------
pause


This is an obvious error. I didn't look any further at present.

This is a basic form of the syntax.

Code: Select all

'command ^|ftp '

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Getting output stored into variable from piped commands

#3 Post by BoQsc » 18 Apr 2016 00:07

Code: Select all

echo ---------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN ('echo dir %remote_servers_file% ^| ftp -A localhost') DO (
SET var!count!=%%F
SET /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL
echo ---------------


Doesn't seems to work.

The output shows that DIR command is unrecognizable, but FTP command is working perfectly.

Code: Select all

command not recognized
Anonymous login succeeded for ****@D****-*****
Invalid command.
ECHO is off.
ECHO is off.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting output stored into variable from piped commands

#4 Post by foxidrive » 18 Apr 2016 17:44

Code: Select all

@echo off
echo ---------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN ('echo dir %remote_servers_file%^| ftp -A localhost') DO (
echo "%%F"
SET var!count!=%%F
SET /a count+=1
)
ECHO.%var1%
ECHO.%var2%
ECHO.%var3%
ENDLOCAL
echo ---------------


Try the small changes here - some are just to reduce the irrelevant messages on the screen

This variable is not set in the code shown %remote_servers_file% but try logging into the FTP server and issue a DIR command just to confirm the dir command manually.

I suspect the code isn't what you are actually using, so debugging different code is really a different kettle of fish.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Getting output stored into variable from piped commands

#5 Post by BoQsc » 19 Apr 2016 09:07

Output with your latest fix implemented:

Code: Select all

---------------
"echo dir "Recovery.txt"| ftp -A localhost"
> ftp: connect :Connection refused
Invalid command.


---------------





This is my whole code. Quickfixes seems to not really work.

Code: Select all

@ECHO OFF && TITLE Receive size of file inside FTP

::Connecting to ftp, executing dir commmand on file, storing it into file [variable in this version]
::Filtering output of command inside stored file [variable in this version] to find size of file and
::Placing filtered value into variable.



:loop
::_______________________________________________________________________________________
::               [Function]                    [File to check size of inside FTP server]
call :extracting_filesize_from_remote_servers_file "Recovery.txt"
echo %remote_servers_file% %filesize%
::_______________________________________________________________________________________
goto loop









::-------------END OF THE PROGRAM----------------------|
  PAUSE                                     |
  EXIT                                         |
::-------------END OF THE PROGRAM----------------------|








::-------------Functions OF The PROGRAM--------------------
EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT




:extracting_filesize_from_remote_servers_file

::---------------------------------[Initializing variables Beginning]---------------------------------------------

::A file inside root of FTP server to check for file size.
SET "remote_servers_file=%1"

:: Dir commands output contains file size witch later is filtered out from |temporary_dir_command_output| file or Variable (still not working)
SET "temporary_dir_command_output=temporary_dir_command_output"

::---------------------------------[Initializing variables END]---------------------------------------------------





:Connecting_to_FTP_server
:: [INFO] And executing dir command, storing output to file.
:: [FIX]  Setting ouput of command to filesize already for "No connection" FTP output
echo dir %remote_servers_file% | ftp -A localhost 2>NUL > %temporary_dir_command_output%

::NOTE to myself: Bring it back when everything will be alright
::&& set /p filesize=<%temporary_dir_command_output%


::===========================LOOK=HERE!================================
CLS
:Connecting_to_FTP_server
:: [INFO] And executing dir command inside FTP server, storing output into variables.
echo ---------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN ('echo dir %remote_servers_file%^| ftp -A localhost') DO (
echo "%%F"
SET var!count!=%%F
SET /a count+=1
)
ECHO.%var1%
ECHO.%var2%
ECHO.%var3%
ENDLOCAL
echo ---------------

::Filters output of command to get size of file stored inside FTP server
for /f "tokens=5 delims= " %%b in (temporary_dir_command_output) do set "filesize=%%b"
del "temporary_dir_command_output"

::Delay control (slows down the process for computer stability )
set "delay=1" && PING 127.0.0.1 -n %delay% >NUL 2>&1 || PING ::1 -n %delay% >NUL 2>&1


::
::echo size of %remote_servers_file%: %filesize%
GOTO :EOF

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting output stored into variable from piped commands

#6 Post by foxidrive » 20 Apr 2016 22:23

ftp: connect :Connection refused


That is a problem with your FTP server.
Try logging in manually to ensure it is working.

I have no idea of the methods you are using for authentication.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Getting output stored into variable from piped commands

#7 Post by BoQsc » 21 Apr 2016 00:24

foxidrive wrote:
ftp: connect :Connection refused


That is a problem with your FTP server.
Try logging in manually to ensure it is working.

I have no idea of the methods you are using for authentication.



I'm using ftpdmin Executable to run the simplest FTP server
Command FTP -A localhost is used to connect to FTP server (localhost) from Command Prompt Line Anonymously (-A)
The server's default settings do not require to have any kind of username/password to be entered. (my server is fully default)

I want to assure that with or without FTP server running - the command "DIR" or any other is not executed properly inside the FOR LOOP after connection to server.


Here is output from exactly the same script with fix you gave me
---------------
"echo dir "Recovery.txt"| ftp -A localhost"
command not recognized
Anonymous login succeeded for vaida@DESKTOP-7FBO6QL
Invalid command.


---------------
Press any key to continue . . .


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting output stored into variable from piped commands

#8 Post by foxidrive » 21 Apr 2016 03:54

BoQsc wrote:Here is output from exactly the same script with fix you gave me

Code: Select all

"echo dir "Recovery.txt"| ftp -A localhost"
command not recognized
Anonymous login succeeded for vaida@DESKTOP-7FBO6QL
Invalid command.
---------------


Where did the outer double quotes come from in the first line?

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Getting output stored into variable from piped commands

#9 Post by BoQsc » 21 Apr 2016 04:31

Search for "fromhere" and you will find echo that is giving it.

Code: Select all


---------------
"fromhere echo dir "Recovery.txt"| ftp -A localhost"
command not recognized
Anonymous login succeeded for vaida@DESKTOP-7FBO6QL
Invalid command.


---------------
"Recovery.txt" 0








CODE USED TO GET OUTPUT:

Code: Select all

@ECHO OFF && TITLE Receive size of file inside FTP

::Connecting to ftp, executing dir commmand on file, storing it into file [variable in this version]
::Filtering output of command inside stored file [variable in this version] to find size of file and
::Placing filtered value into variable.



:loop
::_______________________________________________________________________________________
::               [Function]                    [File to check size of inside FTP server]
call :extracting_filesize_from_remote_servers_file "Recovery.txt"
echo %remote_servers_file% %filesize%
::_______________________________________________________________________________________
goto loop









::-------------END OF THE PROGRAM----------------------|
  PAUSE                                     |
  EXIT                                         |
::-------------END OF THE PROGRAM----------------------|








::-------------Functions OF The PROGRAM--------------------
EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT EXIT




:extracting_filesize_from_remote_servers_file

::---------------------------------[Initializing variables Beginning]---------------------------------------------

::A file inside root of FTP server to check for file size.
SET "remote_servers_file=%1"

:: Dir commands output contains file size witch later is filtered out from |temporary_dir_command_output| file or Variable (still not working)
SET "temporary_dir_command_output=temporary_dir_command_output"

::---------------------------------[Initializing variables END]---------------------------------------------------





:Connecting_to_FTP_server
:: [INFO] And executing dir command, storing output to file.
:: [FIX]  Setting ouput of command to filesize already for "No connection" FTP output
echo dir %remote_servers_file% | ftp -A localhost 2>NUL > %temporary_dir_command_output%

::NOTE to myself: Bring it back when everything will be alright
::&& set /p filesize=<%temporary_dir_command_output%


::===========================LOOK=HERE!================================
CLS
:Connecting_to_FTP_server
:: [INFO] And executing dir command inside FTP server, storing output into variables.
echo ---------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN ('echo dir %remote_servers_file%^| ftp -A localhost') DO (
echo "fromhere %%F"
SET var!count!=%%F
SET /a count+=1
)
ECHO.%var1%
ECHO.%var2%
ECHO.%var3%
ENDLOCAL
echo ---------------

::Filters output of command to get size of file stored inside FTP server
for /f "tokens=5 delims= " %%b in (temporary_dir_command_output) do set "filesize=%%b"
del "temporary_dir_command_output"

::Delay control (slows down the process for computer stability )
set "delay=1" && PING 127.0.0.1 -n %delay% >NUL 2>&1 || PING ::1 -n %delay% >NUL 2>&1


::
::echo size of %remote_servers_file%: %filesize%
GOTO :EOF

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting output stored into variable from piped commands

#10 Post by foxidrive » 21 Apr 2016 05:01

Take out the USEBACKQ

I should have noticed it - maybe 2 liters of whisky is too much for dinner.

Code: Select all

FOR /F "tokens=* USEBACKQ" %%F IN ('echo dir %remote_servers_file%^| ftp -A localhost') DO (

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Getting output stored into variable from piped commands

#11 Post by BoQsc » 23 Apr 2016 12:41

You are a great man, foxidrive.
Hope the things will get better for you, let the dank beat hit you hard,
So that all the whiskey won't struggle you anymore.
Sincerely someguy.

Ps. actualy it worked well. And instead of removing the line i got used to it. Thank you sir.
foxidrive wrote:FOR /F "tokens=* USEBACKQ" %%F IN (`echo dir %FILE% ^| ftp -A %REMOTE_SERVER% 2^>nul`) DO (




http://ss64.com/nt/for_cmd.html wrote: usebackq use the alternate quoting style:
  • - Use double quotes for long file names in "filenameset".
  • - Use single quotes for 'Text string to process'
  • - Use back quotes for `command_to_process`

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting output stored into variable from piped commands

#12 Post by foxidrive » 24 Apr 2016 08:17

BoQsc wrote:So that all the whiskey won't struggle you anymore.


Thanks. :)

I only drink one or two glasses of grog a year, but I talk a lot of crap to make up for it. :D
You should see the rot I type in social forums! ;)

Cheers

Post Reply