window script-winscp

Hi,

I have written a script which downloads files from server to folders based on file name. It is a very simple script and I like to  use a script that checks the file name and move it to a correct folder using maybe an array instead of the way I have hard coded the names in my script. Any solution?

here is my script:

set sourcepath=D:\....
set destinationpath=D:\....

"C:\PRogram Files (x86)\winscp.com" /command "option batch on" "option confirm off" "option transfer binary" "open username@server.com"
if exist (/srv/sourceFolder/Home*.*) "get /srv/sourceFolder/Home*.* C:\X\Y\ALL\HME\*_%datestamp%.*"
else if exist (/srv/sourceFolder/Fact*.*) "get /srv/sourceFolder/Fact*.* C:\X\Y\ALL\BN\*_%datestamp%.*"

is it possible to create an array of file names and relevant folder names  and loop through them instead of hard coding the names?how can it be done?or is there any other way of doing this?
e.g: array of file names: Home*.*, Fact*.*
and array of folders: HME, BN,....

__________________________________________________________________________
After download I like to redirect them to the exact same folder structure on D drive. I currently use the following script:
xcopy %sourcepath% %destinationpath%

how can it be done avoiding hard coding the path?

Thanks in advance
shmzAsked:
Who is Participating?
 
NVITCommented:
Hi,

I've ironed out the ftp download part.

I noticed possibly some unnecessary code. Do you need the part that copies after the ftp download, i.e. :MakeBat_Copy? If not, we could download directly from ftp? Please confirm.
@echo off
:START
set LogFN=%temp%\winscp_log.txt
set ScrFN=%temp%\winscp_script.txt
set BatCopyFN=%temp%\BatCopy.bat

set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@server.com:userpassword@server.com
set wscp_SrcDir=/srv/sourceFolder
set wscp_Prog=C:\Program Files (x86)\winscp\winscp.com

del /q "%ScrFN%" "%BatCopyFN%"

Call :MakeScr_Header
Call :MakeScr_Paths home home
Call :MakeScr_Paths fact fact
Call :MakeScr_Tail

"%wscp_Prog%" /script="%ScrFN%"
  
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

:END
goto :eof

:MakeBat_Copy
REM Copy downloads to other folders
>> "%BatCopyFN%" echo if not exist "%LocSrcDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocSrcDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo if not exist "%LocTgtDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocTgtDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo xcopy /e /i "%LocSrcDir%\%1\*.*" "%LocTgtDir%\%1\" ^>^>"%LogFN%"
goto :eof

:MakeScr_Header
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # e.g. Connect as a user2 with password...
>> "%ScrFN%" echo #      open sftp://user2:password@example.com/
>> "%ScrFN%" echo # Connect as a user
>> "%ScrFN%" echo open %wscp_AcctName%
>> "%ScrFN%" echo # Automatically abort script on errors
>> "%ScrFN%" echo option batch abort
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # Commands fail when file mask matches no files
>> "%ScrFN%" echo option failonnomatch on
goto :eof

:MakeScr_Paths
>> "%ScrFN%" echo # Directory name are case sensitive!
>> "%ScrFN%" echo # Download file to local directory
>> "%ScrFN%" echo cd /%1/
>> "%ScrFN%" echo get *.* %LocSrcDir%\%2\
goto :eof

:MakeScr_Tail
>> "%ScrFN%" echo # Disconnect
>> "%ScrFN%" echo close
>> "%ScrFN%" echo # Exit WinSCP
>> "%ScrFN%" echo exit
goto :eof

:Err_winscp
>> "%LogFN%" echo %%date%% %%time%% An error occurred in wincsp
goto :eof

Open in new window

0
 
NVITCommented:
> ...create an array of file names and relevant folder names
Not quite following you. Do you mean you'll have different folder/filenames other than what you show?
/srv/sourceFolder/Home*.*
/srv/sourceFolder/Fact*.*
C:\X\Y\ALL\HME
C:\X\Y\ALL\BN

For example, under /srv/sourceFolder, you'll have other filename patterns?
0
 
NVITCommented:
Maybe something like this?
- Add a CALL line for each pattern pair (array)

call :DoIt Home*.* HME
call :DoIt Fact*.* BN
goto :eof

:DoIt
set SrcFNPatt=%1
set TgtDir=%2

"C:\PRogram Files (x86)\winscp.com" /command "option batch on" "option confirm off" "option transfer binary" "open username@server.com"
if exist (/srv/sourceFolder/%SrcFNPatt%) "get /srv/sourceFolder/%SrcFNPatt% C:\X\Y\ALL\%TgtDir%\*_%datestamp%.*"
goto :eof

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
shmzAuthor Commented:
Yes I will have up to 10 different files which have to go to 10 different folders.

Could explain what your code does?
0
 
NVITCommented:
Hi...
Basically, it loops, passing the source filename pattern and target folder to the winscp command.
Just add a CALL line for each pattern you'd like. e.g.
call :DoIt Home*.* HME
call :DoIt Fact*.* BN
call :DoIt FakePatt1*.* FPE1
call :DoIt FakePatt2*.* FPE2

Open in new window


This version assumes the patterns are under /srv/sourceFolder and C:\X\Y\ALL. But, you could make the folder names variable, too.

goto :eof does 2 things:
First, in the loop, it exits, effectively returning back to the next CALL. Second, after the last CALL line, it jumps over the other code and finishes.
0
 
shmzAuthor Commented:
I got you code. Thanks
The second part of question for redirecting to another drive will then be as follow?
Xcopy "C:\x\y\all\%Tgtdir%\*.* D:\x\y\all\%TgtDir%\*.*"

Thanks
0
 
NVITCommented:
> Xcopy "C:\x\y\all\%Tgtdir%\*.* D:\x\y\all\%TgtDir%\*.*"
Following the first CALL, where %Tgtdir%=HME, then:
Xcopy "C:\x\y\all\HME\*.* D:\x\y\all\HME\*.*"

Revised code:

call :DoIt Home*.* HME
call :DoIt Fact*.* BN
goto :eof

:DoIt
set SrcFNPatt=%1
set TgtDir=%2

"C:\PRogram Files (x86)\winscp.com" /command "option batch on" "option confirm off" "option transfer binary" "open username@server.com"
if exist (/srv/sourceFolder/%SrcFNPatt%) "get /srv/sourceFolder/%SrcFNPatt% C:\X\Y\ALL\%TgtDir%\*_%datestamp%.*"
Xcopy "C:\x\y\all\%Tgtdir%\*.* D:\x\y\all\%TgtDir%\*.*"
goto :eof

Open in new window


Keep in mind... no error checking is done as far as confirming if the source or target folder exist.
0
 
shmzAuthor Commented:
Files on the server are all under one folder:
/srv/sourcefolder/

When downloaded they will be downloaded to:
C:\x\y\all\herewillbeeachtarhetfolder
0
 
shmzAuthor Commented:
Is it generating error and stops or does it jump to the next call if folder doesn't exist?
0
 
shmzAuthor Commented:
Could you please provide the error checking too?
Thanks
0
 
NVITCommented:
If the folder doesn't exist... for example

On the wincsp end, I believe you have a line that checks for the files. If it does, it GETs the files:
if exist (/srv/sourceFolder/%SrcFNPatt%) "get /srv/sourceFolder/%SrcFNPatt% C:\X\Y\ALL\%TgtDir%\*_%datestamp%.*"

Open in new window


Although your example doesn't have it, you (might) need a
echo exit

Open in new window

...to tell winscp to exit. I haven't used winscp much to know. I've included a REM'd out line.


set LogFN=%temp%\winscp_log.txt
call :DoIt Home*.* HME
call :DoIt Fact*.* BN
goto :eof

:DoIt
set SrcFNPatt=%1
set TgtDir=%2

"C:\PRogram Files (x86)\winscp.com" /command "option batch on" "option confirm off" "option transfer binary" "open username@server.com"
if exist (/srv/sourceFolder/%SrcFNPatt%) "get /srv/sourceFolder/%SrcFNPatt% C:\X\Y\ALL\%TgtDir%\*_%datestamp%.*"

REM Not sure if you need to tell winscp to EXIT here
REM exit
if not exist "C:\x\y\all\%Tgtdir%\*." (echo %date% %time% Missing folder "C:\x\y\all\%Tgtdir%" >>"%LogFN%" & goto :eof)
if not exist "D:\x\y\all\%Tgtdir%\*." (echo %date% %time% Missing folder "D:\x\y\all\%Tgtdir%" >>"%LogFN%" & goto :eof)
xcopy /e /i "C:\x\y\all\%Tgtdir%\*.*" "D:\x\y\all\%TgtDir%\" >>"%LogFN%"
goto :eof

Open in new window


Can you do this on some test folders? Just to be sure...

I've included logging for the missing folders and XCOPY results.
0
 
shmzAuthor Commented:
Hi,
I have tried the following code
Rem @echo off
:START
Call :DoIt Home*.* HME
Call :DoIt Fact*.* FAT
Goto :eof
:DoIt
set srcPath=1%
set TrgDir=2%
"C:\Program Files (x86)\winscp\winscp.com /command "option batch on" "option confirm off" "option transfer binary"
"Open username@server.com"
If exist ( /srv/sourcefolder/%srcPath%)
"get /srv/sourcefolder/%srcPath% C:\x\y\all\%TrgDir%\*_%datestamp%.*" goto :eof "Exit" "Close"
:END

this is the result:
Session started
Unknown command 'if'
Unknown command 'exist'
Unknown command '/srv/sourcefolder/1'.
Can't get attribute of file /srv/sourcefolder/1'.
No such file or directory
Error code: 2
Error message from server: no such file.
Request code:7
Unknown command 'goto'
Unknown command ':eof'

In my hard coded version, the download happen without any problem I think the issue is related to the way the file and folders defined in CALL stmt.
How can this be fixed
0
 
NVITCommented:
your code looks different...
set srcPath=1%
set TrgDir=2%

Open in new window


Should be...
set SrcFNPatt=%1
set TgtDir=%2

Open in new window

0
 
shmzAuthor Commented:
I could get it working by removing goto :eof
I noticed it now login to server before downloading each file?
0
 
shmzAuthor Commented:
You are right I put back Goto :eof and fixed the %
Why is it creating a new session to the server everytime it download a new file?
0
 
NVITCommented:
Hi.

If you don't mind, would you copy and paste the entire code?
0
 
NVITCommented:
> Why is it creating a new session to the server everytime it download a new file?
That is how it is setup. Does it take too long with that method? It could probably be modified to prevent doing that. I'd need more time though. Working on other things.
0
 
shmzAuthor Commented:
Sure. Many thanks in advance.
0
 
shmzAuthor Commented:
I tried to move server connection outside the DoIt method(or anything it is called in window scripting) but didn't work.
Here is the code:

Hi,
I have tried the following code
Rem @echo off
:START
Call :DoIt Home*.* HME
Call :DoIt Fact*.* FAT
Goto :eof
:DoIt
set srcPath=1%
set TrgDir=2%
"C:\Program Files (x86)\winscp\winscp.com /command "option batch on" "option confirm off" "option transfer binary"
"Open username@server.com"
If exist ( /srv/sourcefolder/%srcPath%)
"get /srv/sourcefolder/%srcPath% C:\x\y\all\%TrgDir%\*_%datestamp%.*"
Xcopy "c:\x\y\%tirDir%\*.*" "d:\...."
goto:eof "Exit" "Close"
:END

One more issue I have is display of winscp> prompt before xcopy line when I run. I have to type exit for the script to continue running.
0
 
shmzAuthor Commented:
Sorry winscp prompt appear right before the script run the if exist line.
0
 
NVITCommented:
I see you posted the question again here http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28654601.html

I've been working on it.
@echo off
:START
set LogFN=%temp%\winscp_log.txt
set ScrFN=winscp_script.txt
set BatCopyFN=BatCopy.bat
set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@server.com
set wscp_SrcDir=/srv/sourceFolder

del /q "%ScrFN%" "%BatCopyFN%"

Call :MakeScr_Header
Call :MakeScr_Paths Home*.* HME
Call :MakeScr_Paths Fact*.* FAT
Call :MakeScr_Tail
Call :MakeBat_Copy HME
Call :MakeBat_Copy FAT

"C:\Program Files (x86)\winscp\winscp.com /command ^
  "option batch on" ^
  "option confirm off" ^
  "option transfer binary" ^
  "open %wscp_AcctName%" ^
  /script "%ScrFN%"
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

:END
goto :eof

:MakeBat_Copy
REM Copy downloads to other folders
>> "%BatCopyFN%" echo if not exist "%LocSrcDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocSrcDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo if not exist "%LocTgtDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocTgtDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo xcopy /e /i "%LocSrcDir%\%1\*.*" "%LocTgtDir%\%1\" ^>^>"%LogFN%"
goto :eof

:MakeScr_Header
>> "%ScrFN%" echo # Automatically answer all choice prompts negatively
>> "%ScrFN%" echo option batch abort
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # Commands fail when file mask matches no files
>> "%ScrFN%" echo option failonnomatch on
>> "%ScrFN%" echo # Connect
>> "%ScrFN%" echo open %wscp_AcctName%
goto :eof

:MakeScr_Paths
>> "%ScrFN%" echo # Download file to local directory
>> "%ScrFN%" echo ls %wscp_SrcDir%/%1
>> "%ScrFN%" echo get %wscp_SrcDir%/%1 %LocSrcDir%\%2\*_%%datestamp%%.*
goto :eof

:MakeScr_Tail
>> "%ScrFN%" echo # Disconnect
>> "%ScrFN%" echo close
>> "%ScrFN%" echo # Exit WinSCP
>> "%ScrFN%" echo exit
goto :eof

:Err_winscp
>> "%LogFN%" echo %%date%% %%time%% An error occurred in wincsp
goto :eof

Open in new window

0
 
shmzAuthor Commented:
Thank you so much. I was not sure if you have a time to get back to me.

I'm not familiar with scripting and here are some questions:

1- what are wiscp_script.txt and BatCopy.bat? Which is which?

2- what does del /q "%ScrFN%" "%BatCopyFN%" this line do?

3- does script create folders in makeBat_copy if folder does not exist?
Thanks in advance
0
 
NVITCommented:
> what are wiscp_script.txt and BatCopy.bat
winscp_script.txt is the script file used by winscp.com /script option

BatCopy.bat xcopys the downloaded files to the local folders HME and FAT. It runs if winscp.com returns no error

> what does del /q "%ScrFN%" "%BatCopyFN%" this line do?
At the beginning, it erases wiscp_script.txt and BatCopy.bat, to make sure you have fresh files to work with.

> does script create folders in makeBat_copy if folder does not exist?
Per the answer to your first question, the folders are created if BatCopy.bat runs. If it runs, xcopy creates the folders.

To get a better feel, if you haven't tried it yet, do this.

Disable or remove lines 20 to 27. These lines:
"C:\Program Files (x86)\winscp\winscp.com /command ^
  "option batch on" ^
  "option confirm off" ^
  "option transfer binary" ^
  "open %wscp_AcctName%" ^
  /script "%ScrFN%"
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

Open in new window


Then run it.

Now, check your %temp% folder, which should now have 2 files, winscp_script.txt and BatCopy.bat

Open those files and take a look.

FYI... Per your request, this version runs just one winscp session, instead of 2 sessions, as the first version did.
0
 
NVITCommented:
There may be an error as I have not tested it. On line 55
>> "%ScrFN%" echo get %wscp_SrcDir%/%1 %LocSrcDir%\%2\*_%%datestamp%%.*

Open in new window


I have not covered the %datestamp%.

I'll work on it later.
0
 
shmzAuthor Commented:
No worries on Datestamp
I'll try the script today.
Thank you
0
 
shmzAuthor Commented:
Hi,
I tried the script but ran into some problems.
- After session starts, I get a winscp prompt. I have to type exit to continue.
- I get error message that cyclic file copy can not be done even after when I used 2 different drives.
- it does create the two files winscp_script.txt and batcopy and logfile but no files are downloaded to folders. It creates a strange folder called 1folder and any folder under that into the target.

Any chance you look into the script and see what the problem is?

Thanks in advance
0
 
NVITCommented:
I should be able to work on it tomorrow.
0
 
shmzAuthor Commented:
great, I've got to submit it asap. :(
0
 
shmzAuthor Commented:
NewVillageIT
Could you please let me know if issues can be fixed?
Thanks
0
 
NVITCommented:
Hi,

Sorry. Been swamped with other tasks.

Yes. The issue is fixing the winscp.com section. I started to continue on it but have other priorities.

I can get to it again later today. I anticipate it should be fixed by then.
0
 
shmzAuthor Commented:
Thank you. I'm anxciously waiting.... :(
0
 
shmzAuthor Commented:
Hi NewVillageIT
Have you had a chance to look at this?
0
 
shmzAuthor Commented:
It is sftp download.

After downloading I need to archive them.
If it causing problem I can write a separate script later for archiving(Xcopy).

Thanks
0
 
shmzAuthor Commented:
So it is downloading from server to correct folder as per makesrc_path
0
 
NVITCommented:
Have you tried it? You may need to change the line to:
set wscp_AcctName=sftp://username@server.com:userpassword@server.com

Open in new window


Is it working?

Regarding the :MakeBat_Copy archive section, I don't think it's causing a problem. I just thought, if it's a simple COPY command, we could get rid of that part and the SFTP could download directly instead.
0
 
shmzAuthor Commented:
Please note password will not be placed in script. Only username. Should be pre-entered password
0
 
shmzAuthor Commented:
Making changes, haven't tried it yet
0
 
shmzAuthor Commented:
I get access denied message. I used your code putting the password as well but not authenticated.

So I changed it to previous format with only username and I could successfully connect.
But I get message:
Batch abort
Transfer binary
Unknown option 'fileonnomatch'
C:/....

The script is saved on the desktop and with previous code was creating the three files (bat, log and winscp script) but now it does not.
The download folder is on different drive which does not have program files folder...
So there is no %temp% folder
0
 
shmzAuthor Commented:
Actually in previous code I never used %temp% and it only created the log file in temp folder.
0
 
NVITCommented:
> ...previous code...
The previous code for the sftp download portion won't work. I'm not at a desktop now.
0
 
shmzAuthor Commented:
Okay I created a temp folder on the desktop and changed the path to the folder and run it, it created only winscp_script.txt
 Got same message as before batch abort....

The other thing I noticed in winscp_script is:
Cd /home/  --->and  this is the file name not the folder name i.e. It is the home*.*
Get *.* c:\x\y\all\home\
0
 
NVITCommented:
> ...it created only winscp_script.txt
My bad. These lines were missing:
Call :MakeBat_Copy home
Call :MakeBat_Copy fact

Open in new window


Clarification please: So, the files are in a folder like /srv/sourceFolder/home*.* and /srv/sourceFolder/fact*.*?

FYI, you can change the created files to a different location instead of %temp%, if you like. It doesn't have to be at %temp%
0
 
shmzAuthor Commented:
The first 4-5 lines of my first message on top explains the functionality.
So the script checks for file name starting home*.* on the server and download it to HME folder. If file name is fact*.* then download it to FAT folder.
0
 
NVITCommented:
@echo off
:START
set LogFN=%temp%\winscp_log.txt
set ScrFN=%temp%\winscp_script.txt
set BatCopyFN=%temp%\BatCopy.bat

set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=sftp://username@server.com
set wscp_SrcDir=/srv/sourceFolder
set wscp_Prog=C:\Program Files (x86)\winscp\winscp.com

del /q "%ScrFN%" "%BatCopyFN%"

Call :MakeScr_Header
Call :MakeScr_Paths home*.* hme
Call :MakeScr_Paths fact*.* fat
Call :MakeScr_Tail
Call :MakeBat_Copy hme
Call :MakeBat_Copy fat

"%wscp_Prog%" /script="%ScrFN%"
  
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

:END
goto :eof

:MakeBat_Copy
REM Copy downloads to other folders
>> "%BatCopyFN%" echo if not exist "%LocSrcDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocSrcDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo if not exist "%LocTgtDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocTgtDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo xcopy /e /i "%LocSrcDir%\%1\*.*" "%LocTgtDir%\%1\" ^>^>"%LogFN%"
goto :eof

:MakeScr_Header
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # e.g. Connect as a user2 with password...
>> "%ScrFN%" echo #      open sftp://user2:password@example.com/
>> "%ScrFN%" echo # Connect as a user
>> "%ScrFN%" echo open %wscp_AcctName%
>> "%ScrFN%" echo # Automatically abort script on errors
>> "%ScrFN%" echo option batch abort
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # Commands fail when file mask matches no files
>> "%ScrFN%" echo option failonnomatch on
goto :eof

:MakeScr_Paths
>> "%ScrFN%" echo # Directory name are case sensitive!
>> "%ScrFN%" echo # Download file to local directory
REM >> "%ScrFN%" echo cd /%1/
>> "%ScrFN%" echo get %wscp_SrcDir%/%1 %LocSrcDir%\%2\
goto :eof

:MakeScr_Tail
>> "%ScrFN%" echo # Disconnect
>> "%ScrFN%" echo close
>> "%ScrFN%" echo # Exit WinSCP
>> "%ScrFN%" echo exit
goto :eof

:Err_winscp
>> "%LogFN%" echo %%date%% %%time%% An error occurred in wincsp
goto :eof

Open in new window

0
 
shmzAuthor Commented:
Here is the code I ran.
The bold lines removed and the underlined added.
the two files batcopy and winscript are created and it seems correct directory and folder path created.
I did not see any log file.
server connection is active but no file being downloaded to the folders!
it shows
Session started
active session: [2] username@server.com
batch Abort
Unknow option 'failonnomatch'.

ba

@echo off
:START
set LogFN=temp\winscp_log.txt
set ScrFN=temp\winscp_script.txt
set BatCopyFN=temp\BatCopy.bat

set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@server.com
set wscp_SrcDir=/srv/sourceFolder
set wscp_Prog=C:\Program Files (x86)\winscp\winscp.com

del /q "%ScrFN%" "%BatCopyFN%"

Call :MakeScr_Header
Call :MakeScr_Paths home*.* hme
Call :MakeScr_Paths fact*.* fat
Call :MakeScr_Tail
Call :MakeBat_Copy hme
Call :MakeBat_Copy fat

"%wscp_Prog%" /script="%ScrFN%"
  
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

:END
goto :eof

:MakeBat_Copy
REM Copy downloads to other folders
>> "%BatCopyFN%" echo if not exist "%LocSrcDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocSrcDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo if not exist "%LocTgtDir%\%1\*." (echo %%date%% %%time%% Missing folder "%LocTgtDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo xcopy /e /i "%LocSrcDir%\%1\*.*" "%LocTgtDir%\%1\" ^>^>"%LogFN%"
goto :eof

:MakeScr_Header
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # e.g. Connect as a user2 with password...
>> "%ScrFN%" echo #      open sftp://user2:password@example.com/
>> "%ScrFN%" echo # Connect as a user
>> "%ScrFN%" echo open %wscp_AcctName%
>> "%ScrFN%" echo # Automatically abort script on errors
>> "%ScrFN%" echo option batch abort
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
[b]>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary[/b]
>> "%ScrFN%" echo # Commands fail when file mask matches no files
>> "%ScrFN%" echo option failonnomatch on
goto :eof

:MakeScr_Paths

[u]set srcFilename = %1
set trgDirc=%2[/u]

>> "%ScrFN%" echo # Directory name are case sensitive!
>> "%ScrFN%" echo # Download file to local directory
[b]REM >> "%ScrFN%" echo cd /%1/[/b]
[u]>> "%ScrFN%" echo get %wscp_SrcDir%/%srcFilename%  %LocSrcDir%\%TrgDir%\*_%mydate%.*[/u]
goto :eof

:MakeScr_Tail
>> "%ScrFN%" echo # Disconnect
>> "%ScrFN%" echo close
>> "%ScrFN%" echo # Exit WinSCP
>> "%ScrFN%" echo exit
goto :eof

:Err_winscp
>> "%LogFN%" echo %%date%% %%time%% An error occurred in wincsp
goto :eof

Open in new window

0
 
NVITCommented:
Line 59, you have: set trgDirc=%2
Line 64, you have: TrgDir

Line 64, you have: mydate
I assume you have accounted for that?

Line 63. REM >> "%ScrFN%" echo cd /%1/
Remove that line, just to be sure it doesn't interfere.

> Unknow option 'failonnomatch'
Possibly due to version? I used the latest version of WinSCP 5.7.2. It works fine
0
 
shmzAuthor Commented:
the answer to both your questions is yes.

I removed these lines :

>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo # Commands fail when file mask matches no files
>> "%ScrFN%" echo option failonnomatch on

I also have a date.

and worked!!
Ill do more testing and get back to you.

Thank you so so much. :)
0
 
shmzAuthor Commented:
Hi NewVillageIT,
I did a test in case folder does not exist for download.
The error message correctly created in log file but the prompt is asking:
The system cannot find the path specified
Abort retry skip....

How can I automate this question to skip?

Thanks
0
 
shmzAuthor Commented:
I missed couple of lines from the script.
I added the option abort off and now it does not prompt but it does not create the log file any more?

Multiple session start:
Starting to run the script:
Authenticate and start session twice one after other?
0
 
shmzAuthor Commented:
Here is the problem:
I cleared all folders.
I changed the following lines:

Call :makesrc_path  home*.* HME
Call :makesrc_path  Fact*.* FAT
Call :makesrc_path test.* TTT
Call :makesrc_path test*.* MMM

The TTT folder doesn't exist but MMM exist.
Now the script running and save files to
%locSrcDir%\%1
i.e. Save to HME and FAT , gets to TTT which does not exits and batch abort and finish
It does not store anything to YYY and also do not copy them to target directory anymore?
0
 
NVITCommented:
Does it work for some folders but not all?
Please post all code.
0
 
shmzAuthor Commented:
Removing batch abort off
actually solves most problems except two:

1. Prompt for can not find folder TTT
After selecting p it skips and download test file correctly.

2. files correctly downloaded to correct folders. Home*.* and Fact*.* copied to correct folders, except the MMM/test*.* nothing copied from there to target.
0
 
NVITCommented:
Probably because source or target folders don't exist. I'm commuting to work now...
0
 
shmzAuthor Commented:
@echo off
:START
set LogFN=temp\winscp_log.txt
set ScrFN=temp\winscp_script.txt
set BatCopyFN=temp\BatCopy.bat

set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@server.com
set wscp_SrcDir=/srv/sourceFolder
set wscp_Prog=C:\Program Files (x86)\winscp\winscp.com

For /f "token=2-4 delims=/ " %%a in ('date /t) do (set tdate=[%%c-%%a-%%b)

del /q "%ScrFN%" "%BatCopyFN%"

Call :MakeScr_Header
Call :MakeScr_Paths home*.* HME
Call :MakeScr_Paths fact*.* FAT
Call :MakeScr_Paths test*.* TTT
Call :MakeScr_Paths test*.* MMM
Call :MakeScr_Tail
Call :MakeBat_Copy hme
Call :MakeBat_Copy fat
Call :MakeBat_Copy TTT
Call :MakeBat_Copy MMM

"%wscp_Prog%" /script="%ScrFN%"
 
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

:END
goto :eof

:MakeBat_Copy
REM Copy downloads to other folders
>> "%BatCopyFN%" echo if not exist "%LocSrcDir%\%1" (echo %tdate%  Missing folder "%LocSrcDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo if not exist "%LocTgtDir%\%1" (echo %tdate% Missing folder "%LocTgtDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo xcopy /e /i "%LocSrcDir%\%1\*.*" "%LocTgtDir%\%1\" ^>^>"%LogFN%"
goto :eof

:MakeScr_Header
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo open %wscp_AcctName%
>> "%ScrFN%" echo # Automatically abort script on errors
>> "%ScrFN%" echo option batch abort
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
goto :eof

:MakeScr_Paths
Set srcfile=%1
Set targetFldr=%2
>> "%ScrFN%" echo # Directory name are case sensitive!
>> "%ScrFN%" echo # Download file to local directory
>> "%ScrFN%" echo get %wscp_SrcDir%/%srcFile%  %LocSrcDir%\%targetFolder%\*_%mydate%.*
goto :eof

:MakeScr_Tail
>> "%ScrFN%" echo # Disconnect
>> "%ScrFN%" echo close
>> "%ScrFN%" echo # Exit WinSCP
>> "%ScrFN%" echo exit
goto :eof

:Err_winscp
>> "%LogFN%" echo %tdate% An error occurred in wincsp
goto :eof
0
 
shmzAuthor Commented:
TTT is deliberately does not exist and if it doesn't then it should only skip and go to the next file.
0
 
shmzAuthor Commented:
Two connection is opened in makesrc_header
I removed it snd now it works fine with singly authentication.

I also tested for missing file on arc but download folder exists and it works fine but when folder is missing it is not going any further.
0
 
NVITCommented:
Added :MakeLocalDirs...

@echo off
:START
set LogFN=temp\winscp_log.txt
set ScrFN=temp\winscp_script.txt
set BatCopyFN=temp\BatCopy.bat

set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@server.com
set wscp_SrcDir=/srv/sourceFolder
set wscp_Prog=C:\Program Files (x86)\winscp\winscp.com

For /f "token=2-4 delims=/ " %%a in ('date /t) do (set tdate=[%%c-%%a-%%b)

del /q "%ScrFN%" "%BatCopyFN%"

Call :MakeScr_Header
Call :MakeScr_Paths home*.* HME
Call :MakeScr_Paths fact*.* FAT
Call :MakeScr_Paths test*.* TTT
Call :MakeScr_Paths test*.* MMM
Call :MakeScr_Tail
Call :MakeBat_Copy hme
Call :MakeBat_Copy fat
Call :MakeBat_Copy TTT
Call :MakeBat_Copy MMM
Call :MakeLocalDirs hme
Call :MakeLocalDirs fat
Call :MakeLocalDirs TTT
Call :MakeLocalDirs MMM

"%wscp_Prog%" /script="%ScrFN%"
  
if %ERRORLEVEL% neq 0 goto :Err_winscp
if exist "%BatCopyFN%" call "%BatCopyFN%"

:END
goto :eof

:MakeLocalDirs
REM Make local folders
if not exist "%LocSrcDir%\%1" md "%LocSrcDir%\%1" >>"%LogFN%"
if not exist "%LocTgtDir%\%1" md "%LocTgtDir%\%1" >>"%LogFN%"

:MakeBat_Copy
REM Copy downloads to other folders
>> "%BatCopyFN%" echo if not exist "%LocSrcDir%\%1" (echo %tdate%  Missing folder "%LocSrcDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo if not exist "%LocTgtDir%\%1" (echo %tdate% Missing folder "%LocTgtDir%\%1" ^>^>"%LogFN%" ^& goto :eof)
>> "%BatCopyFN%" echo xcopy /e /i "%LocSrcDir%\%1\*.*" "%LocTgtDir%\%1\" ^>^>"%LogFN%"
goto :eof

:MakeScr_Header
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
>> "%ScrFN%" echo # Force binary mode transfer
>> "%ScrFN%" echo option transfer binary
>> "%ScrFN%" echo open %wscp_AcctName%
>> "%ScrFN%" echo # Automatically abort script on errors
>> "%ScrFN%" echo option batch abort
>> "%ScrFN%" echo # Disable overwrite confirmations that conflict with the previous
>> "%ScrFN%" echo option confirm off
goto :eof

:MakeScr_Paths
Set srcfile=%1
Set targetFldr=%2
>> "%ScrFN%" echo # Directory name are case sensitive!
>> "%ScrFN%" echo # Download file to local directory
>> "%ScrFN%" echo get %wscp_SrcDir%/%srcFile%  %LocSrcDir%\%targetFolder%\*_%mydate%.*
goto :eof

:MakeScr_Tail
>> "%ScrFN%" echo # Disconnect
>> "%ScrFN%" echo close
>> "%ScrFN%" echo # Exit WinSCP
>> "%ScrFN%" echo exit
goto :eof

:Err_winscp
>> "%LogFN%" echo %tdate% An error occurred in wincsp
goto :eof

Open in new window

0
 
shmzAuthor Commented:
Hi NewVillageIT,

I do not intend to create the missing folder.
They might want to delete a folder for ever and forget to remove it from the script and then script breaks.
If folder does not exists then I want them to just skip. In this case then they can trace it in the log file.

Thanks
0
 
NVITCommented:
> If folder does not exists...
The ftp or the local?
0
 
shmzAuthor Commented:
Here is the two issues I have:

- prompt for overwrite the file
 (In case script or something fails and have to rerun the script)
- on the server all files are in the same folder.  
When downloaded they are downloaded to different folders based on their name. So if a folder is deleted and script can't find the right folder to download the file then, it should not abort the batch and do not prompt and  continue to download rest of files and write the error to log file.
0
 
shmzAuthor Commented:
Prompt is an issue it should not prompt at all :)
0
 
shmzAuthor Commented:
Hi,

I could get rid of the prompt issue by changing
On Batch Abort
to
On Batch Continue

I also added to xcopy /y

now it does overwrite without any prompt.



Currently the problem is only the missing folder. if the folder is deleted from either srcLocDir or TrgLocDir, then it does download partially until t richeds missing folder but it does not copy to TrgLocDir at all and such issues.

I appreaciate if anyone can help on how to skip the missing folder and move on...


thanks in advance
0
 
NVITCommented:
Hi, shmz...
I appreciate your patience. I'll take a look later. Hive other things right now.
0
 
NVITCommented:
Hi shmz...

> Currently the problem is only the missing folder....it does not copy to TrgLocDir

Disable or remove Line 34 (per my last post). This should then run BatCopyFN:
REM if %ERRORLEVEL% neq 0 goto :Err_winscp

Open in new window


In addition to running BatCopyFN, if you want it to log the error, change it to:
if %ERRORLEVEL% neq 0 call :Err_winscp

Open in new window

0
 
shmzAuthor Commented:
Thank you
Can I ask one last question or create a new question and leave the link here?
0
 
NVITCommented:
Sure. What can I help you with?
0
 
shmzAuthor Commented:
I tried to add the code to makeBat_Copy to also move all files on SFTP server to Archive. Archive is also in sftp server.
How can this be done?
0
 
NVITCommented:
> ...move all files on SFTP server to Archive. Archive is also in sftp server.
Sounds like this happens on winscp.

Do you need it now? I can take a look later.
0
 
shmzAuthor Commented:
Sure, thanks
0
 
shmzAuthor Commented:
Congrates!
You have done a great job.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.