1

I'm using WinSCP in my batch file (the portable version) to download FTP files, however, whenever I run it from a batch file it changes the window title. Is there any way I can avoid that?

My complete script is here on github

But to summarize my use of WinSCP, it basically generates a temporary script file, executes it with winscp.com, redirects its output to another temporary file and parses the output for some keywords.

An example of execution:

WinSCP.com /open /script=t.ftp /ini=nul ftp://%ftpusr%:%ftppass%@%server% >test.ftp
  • It’s a chat program. An assignment. – Mark Deven Dec 21 '18 at 16:42
  • Just it changes the title. I handle the output. – Mark Deven Dec 21 '18 at 16:42
  • You can look at it and see... – Mark Deven Dec 21 '18 at 16:43
2

No you cannot prevent winscp.com from changing console window title.


Note that a console window title is changed by winscp.com only, whose sole purpose is that it is a console application. As a console application, it inherits a console of the parent console application (if any), like that of cmd.exe, when executed from a batch file. Then it can write its output to it, instead of opening a separate console window, what would otherwise equivalent winscp.exe /console call do (winscp.exe is GUI application, so it cannot inherit a console window of parent process). Read about WinSCP executables.

But you seem to want to prevent users from seeing the output of winscp.com too. You only abuse the (hidden) output for error checking. That's not a very reliable approach. You better use WinSCP exit code to check for errors. See How do I know that script completed successfully? If you need even more detailed error checking, you can use XML logging.

Once you get rid of your abuse of WinSCP output, you can switch to winscp.exe with the same arguments. When winscp.exe is called with /command switch, but without /console switch, it runs the commands completely silently (and it does not change console title).

Though for such a complicated use, you should switch from plain WinSCP scripting to WinSCP .NET assembly and PowerShell. Your code will be way cleaner and more robust.


For a quick solution, you can run winscp.com in its own hidden console.
See Run a batch file in a completely hidden way.
(though contrary to most examples, you want to set the bWaitOnReturn argument to True).

You need your batch file to generate a .vbs script like this:

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = "cmd.exe /c ""C:\some\path\winscp.com"" /ini=nul /script=temp.ftp ftp://username:password@host > output.txt"
oShell.Run strArgs, 0, true

And then run it from the batch file like:

cscript runwinscp.vbs
  • Well I handle individually the output of many of the commands. For instance, with file sharing section of my code (:fileman), I handle the results of the ls command to get a list of file names without downloading all the files. Is there a way to run winscp.exe /command and send output to a text file? – Mark Deven Dec 21 '18 at 18:34
  • No. You should not parse WinSCP output anyway. If you need detailed results, parse XML log file. There’s even example for retrieving listing fron the log - winscp.net/eng/docs/logging_xml#xslt – Martin Prikryl Dec 21 '18 at 18:38
  • There is no native xml parser in batch code though. – Mark Deven Dec 21 '18 at 18:39
  • You can download Microsoft msxsl (as you can see in the linked article) - though indeed doing such complex scripting in a batch file is not a good idea. – Martin Prikryl Dec 21 '18 at 18:41
  • I searched for something like that last year for weeks and found nothing :/. Thanks. – Mark Deven Dec 21 '18 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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