Avatar of Matthew Emery
Matthew EmeryFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Advice on a batch file that copies, moves, sftp's & deletes files

I wrote 3 batch jobs to run in order at 15, 30 and 45 minutes past each hour to process some orders which works fine but I have been asked for the whole process to run every 15 minutes so combined all 3 of these into one batch file as below.  My only changes are to move the "exit" command from the end of the SFTP part of the script and added the @echo off sleep 120 command between each script so that the previous parts of the script are allowed 2 minutes to process before the next part of the script starts.

While I am waiting for the supplier to give me to go ahead to send test files I wondered if someone could comment as to whether the script was ok or something else added to improve it

The one issue I have is the files are picked up immediately at the destination server and processed so I am unable to use a script to only delete once files have been cross checked on the destination server.

Copy /y "D:\GenesisIntegration\XXX\PO\*.*" "D:\GenesisIntegration\XXX\XXXFiles\"
Copy /y "D:\GenesisIntegration\XXX\RETURNS\*.*" "D:\GenesisIntegration\XX\XXXFiles\"
Copy /y "D:\GenesisIntegration\XXX\GRN\*.*" "D:\GenesisIntegration\XXX\XXXFiles\"
Move /y "D:\GenesisIntegration\XXX\PO\*.*" "D:\GenesisIntegration\XXX\XXXArchive\"
Move /y "D:\GenesisIntegration\XXX\RETURNS\*.*" "D:\GenesisIntegration\XXX\XXXArchive\"
Move /y "D:\GenesisIntegration\XXX\GRN\*.*" "D:\GenesisIntegration\XXX\XXXArchive\"

@echo off
sleep 120

"C:\Program Files (x86)\WinSCP\WinSCP.com" /command "open sftp://xxxxxxxxxxx:xxxxxxxxxxxxxx@xxx.xxx.xxx.xxx/" "put D:\GenesisIntegration\XXX\XXXFiles\* /genesis/" 

@echo off
sleep 120

Del /Q "D:\GenesisIntegration\XXX\XXXFiles\*.*"

"exit"

Open in new window

Shell ScriptingScripting Languages

Avatar of undefined
Last Comment
Matthew Emery
SOLUTION
Avatar of skullnobrains
skullnobrains

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Matthew Emery
Matthew Emery
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks for both comments:

I need to find a better way of handling issues with Ftp'd files, maybe the supplier needs to delay their processing task so I can setup a file check.

With regards to what you have provided Bill that is very helpful.

1) I was initially after a command to take only the contents of 3 folders and FTP them but as I did not know how to copy the files only I added the first part of the script to move them all to the consolidated directory first.

2) I added the exit command to the WinSCP command initially as the scheduled tasks continued to run without the exit command being there.

Very happy with what you have done, it is better for others to understand and makes good sense!
Avatar of skullnobrains
skullnobrains

you probably can simplify further

for %%A in (%CopyFolders%/*) do (
  if "%WinSCP%" /command "open sftp://xxxxxxxxxxx:xxxxxxxxxxxxxx@xxx.xxx.xxx.xxx/" "put %BaseDir%\%%A %FtpDir%/" (
    move /y "%BaseDir%\%%A\*.*" "%ArchiveDir%\"
  )
)

Open in new window


note that this connects using scp once for each file which is not ideal either.
i'm not used to winscp but the regular sftp would allow scripting this within the sftp command so the connection stays open. maybe that's feasible in winscp, and that would be much better.
Avatar of Matthew Emery

ASKER

Thank you so much for the help.

This is working a treat I do not need build in 1 last command check to ensure remote server connection is open first which is first on my list for Monday
Avatar of Matthew Emery

ASKER

Almost forgot

Instead of the sleep command I have used

Ping 1.1.1.1 -n -w 10000>nul

And a variation of this as it seems to work better
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo