Executing WinSCP command in batch file - batch-file

I am new to batch and trying to execute a script I wrote, essentially I want to go to a remote server copy the files there and transfer it to a folder in my local directory. I am executing the bactch file but nothing is being copied any suggestions would be great. This is my script
open sftp://site:#ftp.site.net -hostkey="server finger print"
synchronize local C:\Users\localdirectory\Desktop\test2 /Home/folderA/NewFiles
exit
I am positive all the information is correct because that's how I login with WinSCP. I got this script from https://www.youtube.com/watch?v=ndvEYOQLc4c

This is WinSCP script. There's no batch file in your question.
To run WinSCP script, use for example:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="C:\path\to\winscp.txt"
(assuming your WinSCP script is in C:\path\to\winscp.txt).
You can put the above command to a batch file.
See guide to scripting with WinSCP.
You can also have WinSCP GUI generate both the script and batch file for you (or even a batch file that directly contains the WinSCP commands).

Related

How to rename a downloaded file from within the WinSCP script?

I have WinSCP script that downloads a file from the SFTP server to the local machine. How can I rename the file from within the WinSCP script?
The script is being run from Windows machine.
The WinSCP script:
option batch off
option confirm off
open sftp://username:password#servername -hostkey=""
option transfer binary
get -latest "oldname.csv" c:\localfolder
mv c:\localfolder\oldname.csv latestname.csv
exit
I tried renaming using the commands:
rename InitialFileName FinalFileName
then tried using:
mv InitialFileName FinalFileName
But the script is throwing the error:
File not found, Language: en
Tried stuff from this blog
If you want to download the file to a different name, specify the new name directly in the get command:
get "oldname.csv" c:\localfolder\latestname.csv
(Note that when downloading one specific file, the -latest switch is pointless. The latest file out of one file[s] is that one file.)

.vbs script runs in batch file but not when run through task scheduler

I have a batch script with 4 commands as follows
sqlcmd (truncate table in database)
del "path to existing csv to delete"
"path to my .vbs script" "file path to xls file to convert" "file path to where csv is to be created"
"File path to Python" "File path to python script to read csv into database"
It runs just fine when I double click the batch file. When I try to run the batch file from the command prompt it doesn't see the .csv file to delete, I'm not sure why this is, but I don't think it matters for my purposes.
When I run the task from task scheduler manually it truncates the table in the database and deletes the existing csv file but the new one is not created which leads me to believe it is getting stuck on the .vbs file.
I have tried making an individual task with the .vbs script and passing in the path to the .xls and .csv files as arguments, but again it just runs without stopping.
I have tried adding
#echo off
>log.txt(
the body of my batch script
)
but nothing gets written to the log. I have also tried adding cscript and also tried adding wscript at the beginning of the line in the batch file which calls the .vbs script and have tried changing the start in directory to C:\batch where the file resides.
No luck with any of the above attempts. Not sure where to go from here.
Found this 2013 answer by eric on Superuser:
Create these two folders:
32Bit:
C:\Windows\System32\config\systemprofile\Desktop
64Bit:
C:\Windows\SysWOW64\config\systemprofile\Desktop
Excel needs these folders if it's not run interactively. Create both folders even if you are on a 64-bit OS.

Using 'for' command to find and download the latest file on SFTP server in PSFTP

I have gone through the following guide to set up an SSIS package to retrieve a text file located on an SFTP server:
https://www.mssqltips.com/sqlservertip/3435/using-sftp-with-sql-server-integration-services/
To summarize, the SSIS package executes PSFTP.exe (A PuTTY tool) which takes the necessary credentials to connect to the server. It also takes a batch file that it executes after connecting. This batch file contains the commands to retrieve the desired text file. To start from the guide, it simply contains a cmd command to change directory, and a get command to retrieve the file:
cmd DataDump
get TeleMarketingResults.txt
All of this works fine.
The issue arises when I try to make this batch file logic more complex as it does not seem to recognize basic keywords. For instance, I would like to modify it to retrieve the most recent file, so I tried adding this:
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo The most recently created file is %LAST%
but then I get these errors:
psftp: unknown command "for"
psftp: unknown command "echo"
If I execute the batch file manually in a local directory, it works. The issue only occurs when passing it as a parameter to PSFTP.exe. Why is this?
psftp script file can contain psftp commands only. for, set or dir are not psftp commands.
There's hardly any reasonable way to retrieve latest file using psftp. You would have to do it in two steps. First to retrieve listing and store it to a file. Then parse that file using some smart batch file commands to find the latest files. And then run psftp again to download that file. It is cumbersome and ineffective as it requires two connections.
You better use a more powerful SFTP client. For example it's trivial with my WinSCP SFTP client. See
Question WinSCP select most recent file or
WinSCP article Downloading the most recent file.

Command Line Installation with SCCM 2012

I have a few applications that I am trying to deploy with SCCM 2012 but the installations are failing through the application catalog. So what I have for the deployment type is a script installer. I have "cmd.exe" (Without quotations) in the Installation program field and "Installer.bat" in the installation start in field.
When I look at the ccmcache folder, all the contents over that application are there but the following error displays the Software Center:
0x8007010B(-217024629)
I have done some reading online and the "10B" is a common command line error for invalid directory. I have tested the batch file when hard coding a path but my question is, how can I edit the batch file or SCCM to pull from the CCMCache path where the files are downloaded to on the local client? Currently the Batch File is simply:
#echo off
ApplicationName.exe
Do I need to edit the file to cd into the CCMCache folder where the files are placed? How can I get the batch file to run the executable that is downloaded to the CCMCache folder?
Thank You!
You need to have the full path to the installation in your script
#echo Off
\\path to .exe
The way the command is written will not be able to find the .exe file. You need to add the full unc path to the .exe into your .cmd file. You should have your installation .exe and .cmd file in the same location on the distribution share
Recommended Solution:
Before starting, since you are only launching an exe with your batch file, I would recommend just using your ApplicationName.exe as your command line parameter in SCCM instead of using a batch. This will eliminate the need to engineer any further.
Modifying the existing solution to work:
If you do still want to use a batch file, keep a few things in mind. The syntax you are using to launch the batch file will not work. I would recommend just using the batch file name "installer.bat" as your command line. If you still want to preface the batch with the cmd.exe, you absolutely need to use the /c switch with it
cmd.exe /c installer.bat
If you don't use /c the console host will only open to a promopt and not execute your batch.
This is not an ideal solution though because using "cmd.exe /c" will set your working directory to the location of cmd.exe (ie "C:\windows\system32") and since your content is staged in ccmcache, you will need to specify its location within your batch. To do this, you would use %~dp0 variable which gives you the directory the current batch is running from. This means modifying your batch to read
#echo off
%~dp0ApplicationName.exe

Using FTP.exe and mget to download files and directories from an FTP directory

I am trying to set up an automatic download of the files from a FTP directory on a scheduled basis. The solution I have come up with is to create a batch file that will be ran by windows task scheduler. The batch file is as follows (so far)
prompt
open ftp://xx.xx.xx.xx
myuser
mypassword
lcd C:\localdir
cd /remotedir/
mget *
quit
The problem is that this does not get everything in the specified directory. Only the loose files in the directory such as a .zip or .txt file. I have searched the questions here to no avail. So how can I get mget to not only download loose files in the root directory but also download the sub-directories and containing files (keeping the structure intact)?
Just answering this in case anyone else has a similar issue. The ftp.exe was a bust but I found out it is fairly easy using WinSCP just create a scheduled task in task scheduler and use the options section for command line parameters
Instructions for creating download script with WinSCP:
http://winscp.net/eng/docs/guide_automation
Instructions for scheduling WinSCP transfer task:
http://winscp.net/eng/docs/guide_schedule
WinSCP downloads directories recursively by default.

Resources