Batch File wouldn't allow me to log in [duplicate] - linux

This question already has answers here:
FTP commands in a batch script does not working properly
(2 answers)
Closed 5 years ago.
#echo off
set datetoday=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
set /a batchdate=%datetoday%-1
cd c:\batchreports\DAYENDREPORTS\PREDAYEND\%batchdate%
ftp 192.168.18.188
username
password
cd autoemail
send DEALS_ENTERED_TODAY_ALL_2OM_UP_20170813.xls
bye
exit
When I try to run the code line by line in cmd prompt it works properly but if I run the bat file itself, it just prompts the login screen, which means my code stops working at the line of username.. what should I do so that the code will proceed?

To securely get yesterdays date you can't simply subtract one from todays date if this will swap the month as Compo already pointed out.
#echo off
For /f %%Y in (
'powershell -NoP -C "(get-date).AddDays(-1).ToString(\"yyyyMMdd\")"'
) Do Set Yesterday=%%Y
Set "Folder=c:\batchreports\DAYENDREPORTS\PREDAYEND\%yesterday%"
Pushd "%folder%"||(Echo Can't locate %folder% &Pause&Exit /B 1)
> ftpscript.ftp (
echo host 192.168.18.188
echo username
echo password
echo cd autoemail
echo send DEALS_ENTERED_TODAY_ALL_2OM_UP_%Yesterday%.xls
echo bye
)
(ftp -i -s:ftpscript.ftp 2>&1 >ftpscript.log) && (Del ftpscript.*) || (
Echo An error occured, view log:
more < ftpscript.log
pause
Exit /B 1
)
Exit /B 0

Related

cmd.exe stays open after start command opeing multiple files

I already googled and tried several solutions but without success...
My batch script looks the following, it opens four files but then cmd.exe window stays open and cannot be closed anymore. Even Task Manager asks for admin rights if I want to close. I am no admin, only normal user, so to force closing the cmd window I can only logoff and re-login to windows.
I already tried several options ("/b" option on start command, "exit" command at the end, also "exit 0" ... without any difference)
start "" /b "file1.xlsx"
#ping -n 3 localhost> nul
start "" /b "file2.xlsx"
#ping -n 1 localhost> nul
start "" /b "file3.xlsx"
#ping -n 1 localhost> nul
start "" /b "file4.xlsx"
I am using Windows 10 Enterprise 21H2.
At the bottom add:
:end
exit
Then most probably you can solve this 😊
#phuclv Thank you! I changed the pause from ping command to timeout command as you proposed: and this solved the problem for me.
Great! Thank you!
start "" /b "file1.xlsx"
timeout 2
start "" /b "file2.xlsx"
timeout 1
start "" /b "file3.xlsx"
timeout 1
start "" /b "file4.xlsx"

Replace line in textfile from a Batch script

So I have a batch script that does telnet to a switch and runs some commands on it.
I need to change a line every time in a textfile. it's the 5th line with the IP Address. How can I do it?
#echo off
set IP=""
:start
set /p IP="Enter IP Adress:"
echo : IP is set to %IP%
cd "C:\Program Files\PuTTY\"
echo : Trying to connect to %IP%
plink.exe -telnet %IP% < C:\Users\w0w40\Desktop\5ahitn\shruns\commands.txt
for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
pause
goto start
this is the main batch script, i need to replace COMMANDS.txt which is
ITAC
enable
ITAC
copy running-config tftp:
10.51.11.75
i need to replace the ip address in the main batch script
You can't replace something within a file with batch. You have to recreate the whole file.
First remove the last line. That's easy, as it's the only line that contains dots:
find /v "." commands.txt > commands.tmp
then add the new line to the new file:
>commands.tmp echo %NetworkIP%
and rename it to the original name:
move /y commands.tmp commands.txt

Batch Scripting to Search text with space in a .log file

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

Display text from .txt file in batch file

I'm scripting a big batch file.
It records the date to a log.txt file:
#echo off
echo %date%, %time% >> log.txt
echo Current date/time is %date%, %time%.
#pause
exit
It can record it several times, on several lines. Now what I want to do is that the batch file file shows the last recorded date/time from the log.txt file.
How?
type log.txt
But that will give you the whole file. You could change it to:
echo %date%, %time% >> log.txt
echo %date%, %time% > log_last.txt
...
type log_last.txt
to get only the last one.
hmm.. just found the answer. it's easier then i thought. it just needs a bunch more stuff:
#echo off
if not exist log.txt GOTO :write
echo Date/Time last login:
type log.txt
del log.txt
:write
echo %date%, %time%. >> log.txt
#pause
exit
So it first reads the log.txt file and deletes it. After that it just get a new file (log.txt) with the date & time!
I hope this helps other people!
(the only prob is that the first time it does not work, but then just enter in random value at log.txt.)
(This problem is solved and edited.)
Use the tail.exe from the Windows 2003 Resource Kit
Try this: use Find to iterate through all lines with "Current date/time", and write each line to the same file:
for /f "usebackq delims==" %i in (`find "Current date" log.txt`) do (echo %i > log-time.txt)
type log-time.txt
Set delims= to a character not relevant in the date/time lines. Use %%i in batch files.
Explanation (update):
Find extracts all lines from log.txt containing the search string.
For /f loops through each line the command inside (...) generates.
As echo > log-time.txt (single > !) overwrites log-time.txt every time it's executed, only the last matching line remains in log-time.txt
Here's a version that doesn't fail if log.txt is missing:
#echo off
if not exist log.txt goto firstlogin
echo Date/Time last login:
type log.txt
goto end
:firstlogin
echo No last login found.
:end
echo %date%, %time%. > log.txt
pause
Ok I wonder when's the use but, here are two snipets you could use:
lastlog.cmd
#echo off
for /f "delims=" %%l in (log.txt) do set TimeStamp=%%l
echo %TimeStamp%
Change the "echo.." line, but the last log time is within %TimeStamp%. No temp files used, no clutter and reusable as it is in a variable.
On the other hand, if you need to know this WITHIN your code, and not from another batch, change your logging for:
set TimeStamp=%date%, %time%
echo %TimeStamp% >> log.txt
so that the variable %TimeStamp% is usable later when you need it.
A handy timestamp format:
%date:~3,2%/%date:~0,2%/%date:~6,2%-%time:~0,8%
Just set the time and date to variables if it will be something that will be in a loop then
:top
set T=%time%
set D=%Date%
echo %T%>>log.txt
echo %d%>>log.txt
echo time:%T%
echo date:%D%
pause
goto top
I suggest making it nice and clean by putting:
#echo off
in front of every thing it get rid of the rubbish C:/users/example/...
and putting
cls
after the :top to clear the screen before it add the new date and time to the display
Here is a good date and time code:
#echo off
if %date:~4,2%==01 set month=January
if %date:~4,2%==02 set month=February
if %date:~4,2%==03 set month=March
if %date:~4,2%==04 set month=April
if %date:~4,2%==05 set month=May
if %date:~4,2%==06 set month=June
if %date:~4,2%==07 set month=July
if %date:~4,2%==08 set month=August
if %date:~4,2%==09 set month=September
if %date:~4,2%==10 set month=October
if %date:~4,2%==11 set month=November
if %date:~4,2%==12 set month=December
if %date:~0,3%==Mon set day=Monday
if %date:~0,3%==Tue set day=Tuesday
if %date:~0,3%==Wed set day=Wednesday
if %date:~0,3%==Thu set day=Thursday
if %date:~0,3%==Fri set day=Friday
if %date:~0,3%==Sat set day=Saturday
if %date:~0,3%==Sun set day=Sunday
echo.
echo The Date is %day%, %month% %date:~7,2%, %date:~10,4% the current time is: %time:~0,5%
pause
Outputs: The Date is Sunday, September 27, 2009 the current time is: 3:07
#echo off
set log=%time% %date%
echo %log%
That is a batch for saving the date and time as a temporary variable, and displaying it.
In a hurry, I don't have time to write a script to open a txt, maybe later.

How to force bash autocomplete to one command?

I just upgrade to Ubuntu Karmic 9.10 and there is now emphaty.
The problem is that em-TAB- doesn't autocomplete to emacs anymore ...
I there a way to the my Bourne-Shell to complete em-TAB- always to emacs ?
Thank you
If you add an alias for em to your .bashrc you can skip the Tab-completion all together:
alias em=emacs
With this you can start emacs by simply typing the command em.
I use another approach - simple e command:
#!/bin/sh
# Written by Oleksandr Gavenko , 2008.
# File placed by author in public domain.
# View file in emacs buffer using emacsclient.
# If emacs not already running, run it.
# Put this file 'e' in your PATH.
# Name `e' because `edit'.
case "$1" in
-h|-help|--help)
echo "Emacsclient run script."
echo "Usage:"
echo " e [-h|--help] file..."
exit 0
;;
"")
echo "What file you want to open?"
exit 0
;;
esac
if [ -n "$COMSPEC" ]; then
# We probably under Windows like OS. I like native Emacs over Cygwin.
for arg; do
emacsclientw -a emacs -n "$arg"
done
else
# We under UNIX like OS.
for arg; do
emacsclient -a emacs -n "$arg"
done
fi
Also I write similar script in batch file (.bat) language for MS Windows:
#echo off
REM Written by Oleksandr Gavenko , 2008.
REM File placed by author in public domain.
REM View files in emacs buffer using emacsclientw.
REM If emacs not already running, run it.
REM Put this file (e.bat) in your PATH.
REM Name `e' because `edit'.
REM If path to file contain spaces it must be inclosed into quotes.
if x%1 == x-h goto usage
if x%1 == x-help goto usage
if x%1 == x--help goto usage
:loop
emacsclientw -a runemacs -n %1
shift
if not x%1 == x goto loop
goto :eof
:usage
#echo Alias for emacsclient without waiting for editing ending.
#echo Usage:
#echo e [-h^|--help] file...

Resources