LordByron
LordByron

Reputation: 9

Create Log of User Input from Batch code

I have a Windows Batch script I cobbled together from Google searches. My latest attempts to find what I need have been a mixed bag. I hope someone could assist.

Recently one of my users swears that my script opens and closes for them without their input. I personally find this unlikely as any attempts to witness the "error" is impossible, and for years this script has worked flawlessly for our needs.

I'd like to add code to create an output.log of any user input while the batch is running. I expect output to contain either y, n, or empty/nul if the user pressed ENTER on an empty prompt.

I can only use batch script due to business security restraints, so if this isn't possible then I'll have to figure some other way.

Would someone review and show me how to add the necessary code to achieve, if possible, what I need?

Thank you.

@echo off

SET /P ANSWER=Have checks been deposited and daily balance file saved (Y/N)?
echo You chose: %ANSWER%
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)

goto :no

:yes
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "DailyBalance.xlsm" %%e-%%f-%%g-Deposit.xlsm
echo File Rename Complete!
pause
move "C:\Deposit\*Deposit.xlsm" "C:\Deposit\Complete\" 
echo Renamed File Move Complete!
pause
copy "C:\Deposit\Reset\DailyBalance.xlsm" "C:\Deposit\" 
echo Daily Balance File Refreshed and ready for tomorrow!
echo Daily Backup has completed!
pause
exit /b 0

:no
echo Please run DailyBackup once the deposit has been completed.
pause
exit /b 1

Upvotes: 0

Views: 584

Answers (1)

lit
lit

Reputation: 16236

The code below omits some of the actual work in order to focus on flow control. I suspect the real problem will be determining where the LOGFILE will be and its name. This code will name it the same as the .bat file, but with ".log" as the extension.

@echo off
SET "LOGFILE=%TEMP%\%~n0.log"

CHOICE /M Have checks been deposited and daily balance file saved?
ECHO>>"%LOGFILE%" Choice at %DATE% %TIME% by %USERNAME% is %ERRORLEVEL%
IF ERRORLEVEL 2 (GOTO No)

:Yes
rem ...
echo Daily Backup has completed!
pause
exit /b 0

:No
echo Please run DailyBackup once the deposit has been completed.
pause
exit /b 1

UPDATE:

At Squashman's suggestion, I have included the functional code. I have also used an IF/ELSE rather than GOTO. In addition, it accomplishes the file rename without a FOR loop.

@echo off
SET "LOGFILE=%TEMP%\%~n0.log"

CHOICE /M Have checks been deposited and daily balance file saved?

ECHO>>%LOGFILE% Choice at %DATE% %TIME% by %USERNAME% is %ERRORLEVEL%

IF %ERRORLEVEL% EQU 1  (
    for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "DailyBalance.xlsm" %%e-%%f-%%g-Deposit.xlsm
    echo File Rename Complete!
    pause
    move "C:\Deposit\*Deposit.xlsm" "C:\Deposit\Complete\"
    echo Renamed File Move Complete!
    pause
    copy "C:\Deposit\Reset\DailyBalance.xlsm" "C:\Deposit\"
    echo Daily Balance File Refreshed and ready for tomorrow!
    echo Daily Backup has completed!
    SET "EXITCODE=0"
) ELSE (
    echo Please run DailyBackup once the deposit has been completed.
    SET "EXITCODE=1"
)

pause
exit /b %EXITCODE%

Upvotes: 1

Related Questions