Enable Logging to Batch File Commands - windows

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

Related

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

Check for a File Periodically in a MS DOS Batch File

I'm writing an MS DOS batch file that looks for a specific file until it is found.
My code gives me the following error after the SET commands
The syntax of the command is incorrect
C:\> If not exist d:\fdev\data\filename.csv
Here's the code:
SET driveltr=d:\
SET envdir=fdev\
SET datadir=data\
SET archivedir=archive\
SET inputdir=c:\Epic\v8.2\Analytics Tools\Epic BI\Input
SET filename=filename.csv
:while1
if not exist %driveltr%%envdir%%datadir%%filename%
(
echo "Waiting to check for file"
ping -n 11 127.0.0.1 > nul
goto :while1
)
You need "" If your path contains spaces. wait till file exists is working this way:
:while
IF EXIST proceed.txt goto :break
echo "Waiting to check for file"
ping -n 11 127.0.0.1 > nul
goto :while
:break

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

Hiding text in cmd

When I use timeout (timeout /t2) with echo off I am getting output as below:
#echo off
timeout /t 2
#echo on
Output
-->Waiting for 0 seconds, press a key to continue ..
I want to hide the above text in cmd.
Redirect the output with >NUL, as #echo off only supresses the output of the command line itself, not of the output of the command.
#echo off
timeout /t 2 > nul
Try this:
#echo off
timeout 5 >nul
#echo on

Resources