Run batch file which run another cmd file - windows

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

Related

Window batch commands to detect if downloaded file exists?

I use a windows batch file that scans download folder and if it detects label.pdf it sends to local printer. This works fine most of the time but often it detects file before the file completely downloads which causes PDFtoPrinter error. Is there a way to make sure the download process is complete before attempting to detect?
#echo off
cls
:start
IF EXIST "C:\Users\Downloads\label.pdf" (
echo "Found!"
ping 127.0.0.1 -n 1 -w 3000> nul
"C:\Users\Downloads\PDFtoPrinter" label.pdf "4BARCODE 4B-2054A"
ping 127.0.0.1 -n 1 -w 10000> nul
del "C:\Users\Downloads\label.pdf"
) ELSE (
echo "it isn't here!"
)
goto start
You can use the mechanism to check if a file is locked for exclusive access by another application.
#echo off
cls
:start
IF EXIST "C:\Users\Downloads\label.pdf" (
REM check if file is locked
2>nul (>>"C:\Users\Downloads\label.pdf" (call )) && ("C:\Users\Downloads\PDFtoPrinter" label.pdf "4BARCODE 4B-2054A") || (echo file is locked)
) ELSE (
echo "it isn't here!"
)
Timeout /T 10
goto start

I always get the README file in cmd rather than commands if I type commands

I am trying to make a program in Windows, and whenever you type "commands" or anything else, it always shows you the README file. Nothing else.
I have tried adding quotes to the commands in the cmd file, but that still didn't work. I'm using Windows 8.1 x64.
Prototype.cmd:
#echo off
set version=1.0.0 ALPHA
title Prototype version %version%
echo Welcome to Prototype!
echo You have to change the User Directory in the command file to your username.
timeout 1 > NUL
IF NOT EXIST C:\Users\OldBo-5\Desktop\Prototype\System GOTO SYSTEMNOTFOUND
timeout 5 > NUL
cls
:PrototypeMain
echo Type "commands" to get a list of commands.
echo Type "README" to get the readme text file.
timeout 1 > NUL
set /p cmd=">"
cd C:\Users\OldBo-5\Desktop\Prototype\System\Commands
if %cmd% == "readme" goto :ReadmeCmd
:ReadmeCmd
notepad C:\Users\OldBo-5\Desktop\Prototype\READ ME.text
cls
goto PrototypeMain
if %cmd% == "commands" goto :HelpCmd
:HelpCmd
cls
echo "COMMANDS" - Shows this.
echo "README" - Shows readme.text
timeout 10 > NUL
goto PrototypeMain
:SYSTEMNOTFOUND
cls
title Error starting Prototype.
echo Error starting Prototype. Error Code: 4 - System directory not found. Output error log to ROOT\ErrorLog.text
echo Did you forget to install?
echo To open, right click and click Edit. Select Notepad.
cd C:\Users\OldBo-5\Desktop\Prototype\
type NUL > ErrorLog.text
echo Prototype failed to boot. Error Code: 4 > ErrorLog.text
echo Reason: Error Code 4 means that the system directory could not be found. >> ErrorLog.text
timeout 5 > NUL
exit
Does this help you?
#Echo Off
Set "version=1.0.0 ALPHA"
Title Prototype version %version%
CD /D "%UserProfile%\Desktop\Prototype" 2>NUL||Exit /B
Echo Welcome to Prototype!
Timeout 1 >NUL
If Not Exist "System\" GoTo SYSTEMNOTFOUND
:PrototypeMain
ClS
Set "cmnd="
Echo To get a list of commands type COMMANDS
Echo To get the ReadMe text file type README
Set /P "cmnd=>"
If /I "%cmnd%" == "readme" (
Notepad "READ ME.text"
) Else If /I "%cmnd%" == "commands" (
ClS
Echo COMMANDS - Shows this.
Echo README - Shows readme.text
Timeout 10 >NUL
)
GoTo PrototypeMain
:SYSTEMNOTFOUND
ClS
Title Error starting Prototype.
Echo Error starting Prototype. Error Code: 4 - System directory not found. Output error log to ErrorLog.text
Echo Did you forget to install?
( Echo Prototype failed to boot. Error Code: 4
Echo Reason: Error Code 4 means that the system directory could not be found.
)>"ErrorLog.text"
Timeout 5 >NUL
Exit /B

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

Execute python via batch based on some condition?

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%"

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