Execute python via batch based on some condition? - windows

I have a python script which it is downloading some files, before executing the task, I want to know if directory is empty or if the number of files less than 5 then run the python otherwise just say the files already exist, how can I modify the batch code in order to apply this change.
#echo off
.....
:begin
python.exe "%ScriptFolderPath%"\extractDAILYdata.py -u %UserName% -p %Password% -s %BirstSpace% -r %ATIBaseUrl% -sp %DailyLoadPath% -f "%LogPath%" -i %LogLevel% -l %LogFile%
if %errorlevel%==0 goto bcsuccess
:bcerror
echo Task "%TaskName%" failed: %date% >> "%LogPath%\%LogFile%"
exit /B %errorlevel%
:bcsuccess
echo Task "%TaskName%" succeeded: %date% >> "%LogPath%\%LogFile%"

Related

Enable Logging to Batch File Commands

I want to enable logging to a batch file which should log all the contents of command prompt including input/output/error. So I have tried something like the following but it results in a empty log file without any contents. It looks, I am missing something. Below is the batch file.
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root% >> %LOGFILE%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
The log file is empty because you suppressed the output with the command
echo off
Use echo on instead before commands that shall be logged. The command cd usually doesn't send anything to stdout. That's why you got an empty file.
Instead of this ping thingy I recommend
timeout 6
Try this one:
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
echo on
call :logit >>%LOGFILE%
exit /b 0 ​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
#timeout 6 >nul
Edit: (beacause asked in a comment)
The log file name can also include the date and time. You need to change the SET LOGFILE command appropriately. You can experiment, starting with this:
SET LOGFILE=C:\Users\xason\Desktop\Logs\log-%date%-%time%.txt
cd command has not STDOUT. You probably need to append STDOUT of your batch file to a text file. Run your batch file from cmd:
batch_file.bat >> "C:\Users\xason\Desktop\Logs\logs.txt"
Or, inside your batch file:
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit >>"%LOGFILE%"
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
Or even:
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora >> %LOGFILE%
ping 127.0.0.1 -n 6 > nul

Pass variables and loop FTP script for different servers in Windows command line

I would like to build a script that runs from the Windows command line to update an existing file to multiple web domains using FTP. I would like to know how to build a loop that runs the ftp commands and passes variables to it.
The file that passes the arguments to the FTP script will be in the format of:
ftp.domain1.com username1 password1
ftp.domain2.com username2 password2
and so forth.
The FTP script will have the following commands:
open "$variable_for_domain_name"
$variable_for_username
$variable_for_password
cd /public_html
bin
hash
put "test.txt"
close
bye
Using this example, the first iteration of the loop would execute the following FTP commands:
open "ftp.domain1.com"
username1
password1
cd /public_html
bin
hash
put "test.txt"
close
bye
and the second iteration of the loop would execute the following:
open "ftp.domain2.com"
username2
password2
cd /public_html
bin
hash
put "test.txt"
close
bye
I understand to run the script once I would execute:
c:\windows\system32\ftp.exe -i <ftp_script.txt
Where ftp_script.txt contains the above FTP commands with the values stated explicitly. How can the loop be done so that I can execute one command from the windows command line but update files on multiple domains?
Also, I would like to add a command to the FTP script to verify the new contents of test.txt.
The current version of the batch file I'm running is:
`
#echo off
setlocal EnableDelayedExpansion
FOR /F "tokens=1,2,3 delims= " %%a in (ftp_config.txt) do (
echo %%a
echo %%b
echo %%c
call :SUB_ftp_cmd %%a %%b %%c
)
::exit
:SUB_ftp_cmd
echo open %1>ftp.txt
echo %2>>ftp.txt
echo %3>>ftp.txt
echo cd /public_html>>ftp.txt
echo bin>>ftp.txt
echo hash>>ftp.txt
echo put "test.txt">>ftp.txt
echo close>>ftp.txt
echo bye>>ftp.txt
::c:\windows\system32\ftp.exe -i <ftp.txt
::del ftp.txt
::exit /b
`
The resulting file ftp.txt contains the following:
`
open
ECHO is off.
ECHO is off.
cd /public_html
bin
hash
put "test.txt"
close
bye
`
I added the setlocal EnableDelayedExpansion to try and resolve the fact that the variables are not correctly being passed to the subroutine, but it did not change the result.
The simplest way is to create a sub-function in a batch file and call it multiple times with different arguments. This way you do not need any external configuration file and hence no loop:
#echo off
call :download ftp.domain1.com username1 password1
call :download ftp.domain2.com username2 password2
exit
:download
echo open %1>ftp.txt
echo %2>>ftp.txt
echo %3>>ftp.txt
echo bye>>ftp.txt
ftp.exe -s:ftp.txt
del ftp.txt
exit /b
If you really need to configure the parameters in an external file use for command to read the file:
#echo off
FOR /F "tokens=1,2,3" %%i in (config.txt) do call :download %%i %%j %%k
exit
:download
echo open %1>ftp.txt
echo %2>>ftp.txt
echo %3>>ftp.txt
echo cd /public_html>>ftp.txt
echo bin>>ftp.txt
echo hash>>ftp.txt
echo put "test.txt">>ftp.txt
echo close>>ftp.txt
echo bye>>ftp.txt
ftp.exe -s:ftp.txt
del ftp.txt
exit /b
Where the config.txt has the format your wanted:
ftp.domain1.com username1 password1
ftp.domain2.com username2 password2

Windows Batch script to redirect stdout to stdin of an EXE we've just run

I know the normal behaviour when running an EXE in a batch script is for the batch script to wait for the EXE to exit before continuing, but is there any way to get the batch script to continue execution, but redirect its stdout to the stdin of the EXE?
Basically I'm trying to achieve this neat trick or something similar...
#ECHO OFF
echo This is a windows batch script...
dir /p C:\
C:\cygwin\bash.exe <--- Do some magic here
echo This is a bash shell script...
ls -la /cygdrive/c/
exit
echo We're back to the windows batch script again
REM Note: being able to return to the batch script isn't important
Any ideas on how to achieve this? Thanks.
This should work:
#ECHO OFF
echo This is a windows batch script...
dir /p C:\
(
ECHO echo This is a bash shell script...
ECHO ls -la /cygdrive/c/
ECHO exit
) | C:\cygwin\bash.exe
echo We're back to the windows batch script again
EDIT: Reply to the comment
If you want not to add ECHO to each bash line, you may use this method:
#ECHO OFF
echo This is a windows batch script...
dir /p C:\
rem Get the number of the first line in this file that start with "###"
for /F "delims=:" %%a in ('findstr /N "^###" "%~F0"') do set "line=%%a" & goto break
:break
rem Pass from that line to end of this file to bash.exe
more +%line% "%~F0" | C:\cygwin\bash.exe
echo We're back to the windows batch script again
goto :EOF
#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################
echo This is a bash script!!
ls -la /cygdrive/c/
EDIT #2:
I borrowed SonicAtom's method and slightly modified it in order to make it simpler. Here it is:
#ECHO OFF
setlocal
rem This is a magic trick to run the bottom half of this script as a bash script
if not defined _restarted (
set _restarted=true
cmd.exe /Q /D < "%~F0"
rem Put any cleanup commands here
echo/
echo Bash script finished
pause
exit /B
)
cls
"C:\cygwin\bin\bash.exe"
#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################
echo This is a bash script!!
ls -la /cygdrive/c/
No replies?
Well I found the answer myself:
test.bat:
#ECHO OFF
rem This is a magic trick to run the bottom half of this script as a bash script
if not exist _.bat (
echo #ECHO OFF >_.bat
echo cmd.exe -c ^< %~nx0 >>_.bat
_.bat
rem Put any cleanup commands here
del _.bat
echo.
echo Bash script finished
pause
exit /B
)
cls
"C:\cygwin\bin\bash.exe"
#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################
echo This is a bash script!!
ls -la /cygdrive/c/
How it works:
The file starts off as a batch script. It writes another (temporary) batch script which runs another cmd.exe shell, directing the original batch file to cmd.exe as stdin. The original batch file then acts like a text file containing commands typed on the keyboard. It runs bash.exe, and continues to provide stdin to bash. The only disadvantage is that the #ECHO OFF doesn't take effect in the part of the batch file that runs before bash.exe is called. This is because there doesn't seem to be a way to turn off keyboard echoing in cmd.exe.

Run batch file which run another cmd file

I have script in batch file:
cd C:\TESTS\front-tests
call git pull
cd C:\TEST\front-tests\AutoApp\bin\debug
start AutoApp.exe
And I want to call git pull but it fails. Permission denied.
But after I write from hand command to run "start-ssh-agent.cmd" and after that i call git pull it works.
My question is how to modify the batch file which will do git pull and after that run AutoApp.exe?
I have a little snippet here for easily running batch files as admin. See if this helps.
::You'll need this because my snippet uses delayedexpansion heavily.
setlocal enableextensions enabledelayedexpansion
::Restore running directory. Put this before any file operations. I usually put it near the top.
if !CD!==%SystemRoot%\system32 pushd %~dp0
::Put this after where you exit the script.
:admincheck
net session > nul 2>&1
if not %errorlevel%==0 goto continuecheck
if exist "%temp%\uacprompt*.vbs" (
del "%temp%\uacprompt*.vbs" > nul 2>&1
exit /b
)
:continuecheck
ping 127.0.0.1 -n 1 > nul
net session > nul 2>&1
if not %errorlevel%==0 goto nouac
exit /b
:nouac
call :printui Administrative privileges not found. Requesting...
ping 127.0.0.1 -n 1 > nul
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\uacprompt%instance%.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\uacprompt%instance%.vbs"
"%temp%\uacprompt%instance%.vbs" > nul 2>&1
::Check if uacprompt.vbs script exists. VBS script should be deleted when Admin prompt launches.
ping 127.0.0.1 -n 2 > nul
if exist "%temp%\uacprompt%instance%.vbs" (
del "%temp%\uacprompt%instance%.vbs" > nul 2> nul
echo This script requires Admin privileges to run^^! Exiting...
ping 127.0.0.1 -n 3 >nul
exit
)
exit

BATCH- How to put a variable into the move command?

Okay, this is my first question on Stackoverflow, so I'll try to make it a good one.
I've searched all over the web and I couldn't find any information to this. I have created a little batch file that prompts you to put in the name of a file that you would like to move. After that, it asks your your usersname. The problem I'm having is cmd tells me I have incorrect syntax.
Can anyone see what I did wrong?
Below is the code I am using. I just pasted in the part that is having trouble.
Thanks guys!
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename%
echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move C:\Users\%USERNAME%\Desktop\%filename%.bat
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\StartMenu\Programs\Startup\%filename%goto COMPLETED
goto FAILED
I editted a bit your script:
Surrounded filepaths with double quotes
Added /y parameter to move command to override automatically the file in new location (if exists)
Spotted some issues:
goto MOVE
:MOVE has no sense as it will continue anyway that path.
goto FAILED GOTO COMPLETED - there're no such labels in your script.
Please shout if you have other problems.
#echo off
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename% echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move /Y "C:\Users\%USERNAME%\Desktop\%filename%.bat" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%filename%" goto COMPLETED
goto FAILED
:FAILED

Resources