I'm new to WinSCP. I'm trying to set an Errorlevel for the batch IM running. If the Errorlevel= 0 PRINT SUCCESS and transfers those files with Errorlevel 0 to folder call success.
if the error not equal to 0 moves the file with the error level to different folder call errors. Any suggestions.
HERE IS MY.bat
WinSCP.exe /console /script="c:\users\PDP\script.txt" /log="c:\users\PDP\lastrun.txt"
if %ERRORLEVEL% equ 0
echo Success
sendmail.exe -t < success_mail.txt
move OPTTXM* c:\users\PDP\sent ( the files in the batch start with OPTTXM*)
exit /b 0
if %ERRORLEVEL% neq 0 goto error
echo Error!
sendmail.exe -t < error_mail.txt
move ????????????????????????????????? ( how you can get inside each file and check the status
)
exit /b 1
Thanks in advance
You can make WinSCP create XML log with files that were and were not transferred. But it would be difficult (if possible at all) to parse/process those XML files in a batch file.
You better use a more powerful scripting language, like PowerShell.
See for example Move files after upload using PowerShell/WinSCP Script.
(there are many other similar questions here)
There's also WinSCP article on this topic:
Moving local files to different location after successful upload
Related
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 4 years ago.
I'm currently trying to write a batch file to automatically detect if a VPN is logged in or not. However, the code is not working as expected.
set VAR=SUCCESS
for /f "delims=: tokens=1" %%i in ('"C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info') do (
set str=%%i
set rep=%str:logged out=%
echo %str%
echo %rep%
if not "%str%"=="%rep%" (
set VAR=FAIL
)
echo %VAR%
)
echo %VAR%
Running "C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info will give exactly the following (will have spaces before the start of each line):
Command arguments:
INFO result:
session: code: status:
xxxxxx 64 logged out
There is 1 active session(s)!
The status can be either logged out or session established. What I'm trying to determine is that I'd have run the VPN. If the user has successfully logged into the VPN, then I need to do something, otherwise, have to execute some other code.
VAR=FAIL means the VPN failed to log in successfully and would be in logged out state. The problem I'm facing is that the str and rep variables are not assigned any value. I can determine it by the echo statements.
Can somebody help me out in this?
There is a much more simple way to work around this. The way you have the for statement setup will cause it to perform echo's and set strings for each new line of the output string.
A very easy way is to use the | FIND /I after your command. This will filter and search for a statement or text your looking for without any loops.
"C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info | FIND /I "logged out">Nul && (Echo.logged out) || (Echo.session established)
To use the setup, simply put your command in the front (Before the pipe) and use the find /I "" to search for that within the output. Please keep in mind that >Nul will silence the command from being shown on console, which is great for this use.
I want to search text in a log file.
If found show Error and Text.
Else show Not Found and Text.
set str2=Testing Failed in env
findstr "%str2%" SystemOut.log >nul
if not errorlevel 1 ( echo ERROR: Testing Failed in env)
if errorlevel 1 ( echo Not Found Testing Failed in env!)
Whenever in the log file it encounters Testing it says ERROR but it should not do that.
When I try to make changes by adding quotes or something Positive condition gets passed but it fails for negative condition.
Please help me with the script.
Thanks,
Machpatel
You need the /c: switch to include spaces in literal mode.
#echo off
set str2=Testing Failed in env
findstr /c:"%str2%" SystemOut.log >nul
if errorlevel 1 ( echo Not Found "%str2%")
if not errorlevel 1 ( echo found "%str2%")
The problem
I have a batch file the is being called from a Pre-Build event.
When the solution is being built by Jenkins it fails.
When the solution is being built from MSBuild or visual studio in command line It works fine.
My question is, what could cause such a behaviour?
Details
In my solution (.Net 4.5 and C++ projects), I have a batch file that is called from one of the project's Pre-Build events.
It works fine, except when I am trying to build the solution via Jenkins (CI Server).
The error is :
The command ""C:\Jenkins\apps\jenkins\jenkins_home\jobs\Builld_SMW2\workspace\Autosync\CopySymAlignFiles.bat" "C:\Jenkins\apps\jenkins\jenkins_home\jobs\Builld_SMW2\workspace\" "C:\Jenkins\apps\jenkins\jenkins_home\jobs\Builld_SMW2\workspace\Autosync\" "Debug"" exited with code 1.
from the error report, I copied the command that was used for attempting to build the solution:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /p:Platform=x86 /p:configuration=Debug /t:Rebuild /verbosity:quiet /p:WarningLevel=0 /p:Platform=x86 /p:configuration=Debug /t:Rebuild /verbosity:quiet /p:WarningLevel=0 ..\SMW2_Analysis.sln
(I am aware that the arguments are being passed twice, I have no Idea why jenkins does that, but this is not the issue...)
If I run this command from command line, the batch file works fine once the build event is triggered.
The content of CopySymAlignFiles.bat is:
Echo off
set solutionDir=%1
set projectDir=%2
set configuration=%3
REM Removing quotes
set solutionDir=%solutionDir:"=%
set projectDir=%projectDir:"=%
set configuration=%configuration:"=%
REM Does strings have a trailing slash? if so remove it
IF %solutionDir:~-1%==\ SET solutionDir=%solutionDir:~0,-1%
IF %projectDir:~-1%==\ SET projectDir=%projectDir:~0,-1%
IF %configuration:~-1%==\ SET configuration=%configuration:~0,-1%
REM This is just for easier debugging
Echo Solution Directory is: "%solutionDir%"
Echo Project Directory is: "%projectDir%"
Echo Configuration directory is: "%configuration%"
Echo Current Directory is: "%CD%"
Echo The batch file is executed at "%~dp0"
REM Copying sym_align files
copy /Y "%solutionDir%\SymAlignGlue\sym_align.dll" "%projectDir%"
copy /Y "%solutionDir%\SymAlignGlue\sym_align.ctf" "%projectDir%"
set isPV=%configuration%=="PV Release"
IF %isPV% copy /Y "%solutionDir%\SymAlignGlue\Release\SymAlignGlue.dll" "%projectDir%"
IF NOT %isPV% copy /Y "%solutionDir%\SymAlignGlue\%configuration%\SymAlignGlue.dll" "%projectDir%"
REM The result...
IF %ERRORLEVEL% EQU 0 (
REM Success
Echo Copied SymAlign files.
exit /b 0
)
IF NOT %ERRORLEVEL% EQU 0 (
REM Error
Echo An error was found while Copied SymAlign files. Error level was %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
Some additional Information:
I am running Jenkins as a service and I have the service logon as An Administrator.
Does Anyone have an Idea how can this happen?
At the end, it seems that the issue was that there was a duplicate workspace folder.
1 was updated, and the other 1 was built, resulting the tested workspace did not get latest updates.
according to this link this was due to the way jenkins behaves with multiple configurations.
Am trying to run a Batch file in Post build event in Visual studio.
Referred Can we execute a .bat file in post build event command line in visual studio? for reference.
When i post the line
xcopy "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin" /S in postbuild
am getting the expected result
Same line i put in bat and tried calling
call "$(SolutionDir)\Deploy.bat"
or
call "Physical path\deploy.bat"
Am getting excited with code 1. What am i doing wrong here ?
Can i specify macros inside batch file ?
Thanks
You are getting a VS error because it returned an exit code that is not 0. This does not necessarily mean there was an error.
The error code returned means that no files were copied.
These are the return codes for Xcopy:
Exit Code
0 Files were copied without error.
1 No files were found to copy.
2 The user pressed Ctrl+C to terminate xcopy.
4 Various errors including insufficient memory or disk space, an invalid drive name, or invalid syntax.
5 Disk write error occurred.
Try this code in your batch file. Use the /Y so that you will not have to deal with any prompts. You can handle the return code of 1 with another action or just return 0.
VS Post Build command line code:
CALL "$(SolutionDir)"Deploy.bat "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin"
Deploy.bat file
Xcopy %1 %2 /S /Y
If errorlevel 1 #exit 0
Is there any way to add files/folders to multiple zip files at once. For example:
Updates:
folder1
folder2
file1
file2
Archives:
archive1.zip
archive2.zip
archive3.zip
I need to add updates to all archives without opening all of them and pasting updates in there.
Any way to do this?
Or is there another archive program that can do this?
This can be done using a batch file and for example WinRAR.
#echo off
if not exist archive*.zip (
echo There are no archive*.zip files to update in
echo.
echo %CD%
echo.
pause
goto :EOF
)
set "ErrorCount=0"
for %%I in (archive*.zip) do (
"%ProgramFiles%\WinRAR\WinRar.exe" u -afzip -cfg- -ep1 -ibck -inul -r -y "%%I" folder1 folder2 file1 file2
if errorlevel 1 (
echo Error on updating %%I
set /A ErrorCount+=1
)
)
if not "%ErrorCount%" == "0" (
echo.
pause
)
set "ErrorCount="
For each archive*.zip file WinRAR is called to update the ZIP file with the two folders and the two files.
The batch processing finishes without printing any message and without pausing if all ZIP files found could be updated successfully. Otherwise the batch file outputs which ZIP file could not be updated for example because of read-only attribute set, and pauses the batch processing before finishing so that the user can read the error message(s).
For details on the WinRAR command u and the used switches open in WinRAR from menu Help the Help topics, open on tab Contents the item Command line mode and read at least the help pages:
Command line syntax
Commands - Alphabetic commands list
Switches - Alphabetic switches list
For understanding the other used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
pause /?
set /?
You can do it by using 7zip command line version
7z a archive1.zip dir\ -v10k -v15k -v2m #listfile.txt
this will do archive all files and folder is in that folder named 'dir'
and first volume will be 10kb, second will be 15kb and others will be 2mb each except last
and you can use a file that has list of all files named.