How to not change title with WinSCP in batch file

1

2

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

Mark Deven

Posted 2018-12-21T15:27:03.780

Reputation: 1 101

It’s a chat program. An assignment. – Mark Deven – 2018-12-21T16:42:12.890

Just it changes the title. I handle the output. – Mark Deven – 2018-12-21T16:42:46.247

You can look at it and see... – Mark Deven – 2018-12-21T16:43:02.527

Answers

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

Martin Prikryl

Posted 2018-12-21T15:27:03.780

Reputation: 13 764

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 – 2018-12-21T18:34:51.287

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 - https://winscp.net/eng/docs/logging_xml#xslt

– Martin Prikryl – 2018-12-21T18:38:08.160

There is no native xml parser in batch code though. – Mark Deven – 2018-12-21T18:39:00.923

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 – 2018-12-21T18:41:51.047

I searched for something like that last year for weeks and found nothing :/. Thanks. – Mark Deven – 2018-12-21T18:47:03.427

I've realized that there's another easy solution for you. See my update answer. – Martin Prikryl – 2018-12-21T19:56:18.737

that's perfect yes. I was missing bWaitOnReturn in the past – Mark Deven – 2018-12-21T20:01:46.757

I'm a total noob, where do I put in the bWaitOnReturn in this script? It's not working with the oShell.Run. Thanks for your patience. – Mark Deven – 2018-12-22T19:39:19.837

true is already there in my answer: oShell.Run strArgs, 0, true – Martin Prikryl – 2018-12-22T20:43:21.610

I see, the true is for the attribute. Thanks – Mark Deven – 2018-12-22T22:06:59.107

e>Dim strArgs strArgs = "cmd.exe /c ""C:\Users\theBATeam\Documents\Batch\FTP-Chat\Bin\winscp.com"" /ini=nul /script=""C:\Users\theBATeam\Documents\Batch\FTP-Chat\Bin\temp.ftp"" ftp://username:pass@ftp.address.com > getWelcome.txt" oShell.Run strArgs, 0, true ``` isnt working for me. Any ideas why? The output into getWelcome.txt is blank, and it doesn't seem to execute anything since no files are downloaded or uploaded. – Mark Deven – 2018-12-24T13:16:54.207

Handling of quotes by cmd.exe is very strange. Remove the quotes around C:\Users\theBATeam\Documents\Batch\FTP-Chat\Bin\temp.ftp. – Martin Prikryl – 2018-12-24T13:22:09.047

Or this should work too: Dim strArgs strArgs = "cmd.exe /c """"C:\Users\theBATeam\Documents\Batch\FTP-Chat\Bin\winscp.com"" /ini=nul /script=""C:\Users\theBATeam\Documents\Batch\FTP-Chat\Bin\temp.ftp"" ftp://username:pass@ftp.address.com"" > getWelcome.txt – Martin Prikryl – 2018-12-24T13:24:59.237

will either or both of those still work if there are spaces in the path? – Mark Deven – 2018-12-24T14:43:53.017

The latter should work even with spaces. – Martin Prikryl – 2018-12-24T15:14:57.473

Thanks for all your help, January’s release should have these in it. Merry Christmas! – Mark Deven – 2018-12-24T15:18:17.527

Let us continue this discussion in chat.

– Mark Deven – 2018-12-24T20:30:40.690