Sh M
asked on
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\*_%datestam p%.*"
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
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*.*
else if exist (/srv/sourceFolder/Fact*.*
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
Maybe something like this?
- Add a CALL line for each pattern pair (array)
- 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
ASKER
Yes I will have up to 10 different files which have to go to 10 different folders.
Could explain what your code does?
Could explain what your code does?
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.
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.
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
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.
ASKER
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
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
> 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:
Keep in mind... no error checking is done as far as confirming if the source or target folder exist.
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
Keep in mind... no error checking is done as far as confirming if the source or target folder exist.
ASKER
Files on the server are all under one folder:
/srv/sourcefolder/
When downloaded they will be downloaded to:
C:\x\y\all\herewillbeeacht arhetfolde r
/srv/sourcefolder/
When downloaded they will be downloaded to:
C:\x\y\all\herewillbeeacht
ASKER
Is it generating error and stops or does it jump to the next call if folder doesn't exist?
ASKER
Could you please provide the error checking too?
Thanks
Thanks
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:
Although your example doesn't have it, you (might) need a
Can you do this on some test folders? Just to be sure...
I've included logging for the missing folders and XCOPY results.
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%.*"
Although your example doesn't have it, you (might) need a
echo exit
...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
Can you do this on some test folders? Just to be sure...
I've included logging for the missing folders and XCOPY results.
ASKER
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%\*_%dat estamp%.*" 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
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
: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
your code looks different...
Should be...
set srcPath=1%
set TrgDir=2%
Should be...
set SrcFNPatt=%1
set TgtDir=%2
ASKER
I could get it working by removing goto :eof
I noticed it now login to server before downloading each file?
I noticed it now login to server before downloading each file?
ASKER
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?
Why is it creating a new session to the server everytime it download a new file?
Hi.
If you don't mind, would you copy and paste the entire code?
If you don't mind, would you copy and paste the entire code?
> 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.
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.
ASKER
Sure. Many thanks in advance.
ASKER
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%\*_%dat estamp%.*"
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.
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
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.
ASKER
Sorry winscp prompt appear right before the script run the if exist line.
I see you posted the question again here https://www.experts-exchange.com/questions/28654601/Windows-scripting.html
I've been working on it.
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
ASKER
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
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
> 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:
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.
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%"
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.
There may be an error as I have not tested it. On line 55
I have not covered the %datestamp%.
I'll work on it later.
>> "%ScrFN%" echo get %wscp_SrcDir%/%1 %LocSrcDir%\%2\*_%%datestamp%%.*
I have not covered the %datestamp%.
I'll work on it later.
ASKER
No worries on Datestamp
I'll try the script today.
Thank you
I'll try the script today.
Thank you
ASKER
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
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
I should be able to work on it tomorrow.
ASKER
great, I've got to submit it asap. :(
ASKER
NewVillageIT
Could you please let me know if issues can be fixed?
Thanks
Could you please let me know if issues can be fixed?
Thanks
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.
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.
ASKER
Thank you. I'm anxciously waiting.... :(
ASKER
Hi NewVillageIT
Have you had a chance to look at this?
Have you had a chance to look at this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
After downloading I need to archive them.
If it causing problem I can write a separate script later for archiving(Xcopy).
Thanks
ASKER
So it is downloading from server to correct folder as per makesrc_path
Have you tried it? You may need to change the line to:
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.
set wscp_AcctName=sftp://username@server.com:userpassword@server.com
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.
ASKER
Please note password will not be placed in script. Only username. Should be pre-entered password
ASKER
Making changes, haven't tried it yet
ASKER
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
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
ASKER
Actually in previous code I never used %temp% and it only created the log file in temp folder.
> ...previous code...
The previous code for the sftp download portion won't work. I'm not at a desktop now.
The previous code for the sftp download portion won't work. I'm not at a desktop now.
ASKER
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\
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\
> ...it created only winscp_script.txt
My bad. These lines were missing:
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%
My bad. These lines were missing:
Call :MakeBat_Copy home
Call :MakeBat_Copy fact
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%
ASKER
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.
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.
@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
ASKER
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
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
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
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
ASKER
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. :)
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. :)
ASKER
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
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
ASKER
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?
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?
ASKER
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?
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?
Does it work for some folders but not all?
Please post all code.
Please post all code.
ASKER
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.
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.
Probably because source or target folders don't exist. I'm commuting to work now...
ASKER
@echo off
:START
set LogFN=temp\winscp_log.txt
set ScrFN=temp\winscp_script.t xt
set BatCopyFN=temp\BatCopy.bat
set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@ser ver.com
set wscp_SrcDir=/srv/sourceFol der
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
:START
set LogFN=temp\winscp_log.txt
set ScrFN=temp\winscp_script.t
set BatCopyFN=temp\BatCopy.bat
set LocSrcDir=C:\x\y\all
set LocTgtDir=D:\x\y\all
set wscp_AcctName=username@ser
set wscp_SrcDir=/srv/sourceFol
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%
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
ASKER
TTT is deliberately does not exist and if it doesn't then it should only skip and go to the next file.
ASKER
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.
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.
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
ASKER
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
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
> If folder does not exists...
The ftp or the local?
The ftp or the local?
ASKER
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.
- 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.
ASKER
Prompt is an issue it should not prompt at all :)
ASKER
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
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
Hi, shmz...
I appreciate your patience. I'll take a look later. Hive other things right now.
I appreciate your patience. I'll take a look later. Hive other things right now.
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:
In addition to running BatCopyFN, if you want it to log the error, change it to:
> 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
In addition to running BatCopyFN, if you want it to log the error, change it to:
if %ERRORLEVEL% neq 0 call :Err_winscp
ASKER
Thank you
Can I ask one last question or create a new question and leave the link here?
Can I ask one last question or create a new question and leave the link here?
Sure. What can I help you with?
ASKER
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?
How can this be done?
> ...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.
Sounds like this happens on winscp.
Do you need it now? I can take a look later.
ASKER
Sure, thanks
ASKER
Congrates!
You have done a great job.
You have done a great job.
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?