Batch (X) Key, Want to End Process - batch-file

Is it possible to close the Batch File, like "press any key"?
Because I have made a Batch file, that renames a file. And when I Launch the .Bat file, The File changes name to the desired name. And when I "press any key" I made it so the File changes name back to the normal name.
However When i press the (X) Button, to close the batch. It does not follow the last line of code. To rename the file Back. Only works when I "press any key".

What I think you're trying to say is that you want the file to change its name so that, when you press any key or exit out of the program it goes back to the original name, right?
I do not think this is possible with batch as when you terminate the process, (command prompt in our case) it cannot execute a command as it is closing. What you could do is make the command prompt launch another command prompt on start and make that command prompt wait until the 1st one is closed and then rename the file back and close itself. This is not that practical though. If you are realy desperate however, try this code:
#echo off
start "CommandPrompt" cmd.exe /WAIT /K ren OriginalFileName.txt NewFileName.txt & pause>nul & ren NewFileName.txt OriginalFileName.txt
ren NewFileName.txt OriginalFileName.txt
exit
While it does essentialy do what you want it to, it relies on the 2 command windows to be closed in order. It is however probably the least complex way out there. It probably goes without saying that you need to replace OriginalFileName.txt and NewFileName.txt with whatever names you need.

Related

Get all info through one command prompt

I know when I start a bat file and have pause at the end it will stay open.
Then when pressing the spacebar the bat file ends, and the command prompt will be closed.
Also I know when we use cmd /k it will stay opened, and there is even a command prompt left to enter some bat file code.
Without pause or cmd /k there will be a new window opened and it closes itself.
What I really want to is to have a console window opened, and every time I run a bat file I want the output to be seen on the cmd that is already opened.
I like the way that I can see all the code that was running, without to having close the windows every time, and like it as if all the code will be seen in one opened cmd prompt.
edit: 2
first_file.bat
#echo first
call "C:\ProgramData\Cool\second_file.bat"
#echo thirth
second_file.bat
#echo second

What is the batch script to open another cmd window to run another line of script

I know that the question was not very clear, so I will try to make it clearer.
I am looking for the batch script command to open a cmd window that runs like a batch program.
I know the command exists as I have seen it used before and have used it before, however, as of late I have not been
able to find it or remember it. The command looks something like this
#echo off
start cmd.exe ("#echo off && echo second window opened && pause")
pause
It would open a second cmd window that read.
second window opened
press any key to continue...
And when you pressed a key the second window would close, just like a batch file cmd window would. As you probably can tell I am relatively new to batch scripts and am still a little iffy on how it works.
Not bad memory. Almost done
start "title" cmd /c "echo in other window & echo. & pause"
Type cmd /? and start /? to get all the needed information for this commands usage

Use batch file to launch ssdeep with file arguments

I am attempting to use a looping batch file to launch the CMD app ssdeep and pass a file argument to it, then keep the ssdeep window open to copy a chunk of output to the clipboard I have the following code:
#ECHO OFF
:start
SET /p filetohash= What file would you like to fuzzy hash?
START C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash%
PAUSE
goto start
This allows me to run the batch file, which I can then drag and drop a file to hash into the CMD window. Upon hitting return, the ssdeep CMD window appears for the moment it takes to hash the file, then closes. This leaves me with the 1st window generated by the batch file, that is requesting a key press.
I want to have the 2nd CMD window stay open so I can copy the hash out. Similar to the PAUSE I used in the batch file, but I need it to apply to the 2nd CMD window created.
I'm not exactly sure how to search for this information. I have searched info on batch files. I used these resources to get thus far:
https://superuser.com/questions/582095/how-to-create-a-batch-file-that-will-run-cmd-and-a-exe-with-parameters
and
Batch files : How to leave the console window open
Thanks in advance,
PTW-105
use the /b switch to the start command - see if that does what you need (leave the pause in).
start "" /b C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash%
The empty double quotes protect the start command if you ever add quoted items in the commands.
Try this command to create hash.txt on your desktop - remove the pause - it should contain the information if it gets printed to STDOUT. It can be parsed to extract just the hash: if you add that information to your question it should be in a format that we can read and see how to parse it.
start "" /b C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash% >"%userprofile%\desktop\hash.txt"

Slowing a batch file down to read errors

With batch, if you get an error, the most you see of it is a flash of text and then the program ends. Is there anyway to have it slow down? or to have it stop before closing when it hits an error?
Thanks
If you execute your Batch file from the command-line in a MS-DOS window and an error happens, you can just review the text in the window to see what happened.
On the other hand, if you execute the Batch file via a double click in the explorer you see nothing if the Batch file have an error. Is this your case? If so, the easiest solution is to test the Batch file in a MS-DOS window until it works ok.
However, if you still need a method to stop closing the DOS window when the Batch file ends, you can do that this way:
Right click on your Batch file and select Create shorcut, a Shorcut is created.
Right click on the Shortcut and select Properties
In Target, after the "C:\Path\filename.bat" string add: & PAUSE
Select OK
This way, when you execute the Shortcut via a double click, the DOS window will execute a PAUSE after the Batch file ends for any reason.
Redirect the output with > to capture it in a file.
You might need: command > file 2>&1
try this :
if NOT ["%errorlevel%"]==["0"] (
pause
exit /b %errorlevel%
)
Run the script from a present CMD.exe and add "exit /b 1" to the scripts end of file. Remove any simple "exit".
Open a new cmd window and execute your command there. The newly opened window will not be closed when an error occurs.
start cmd /k [command]
This works for me with basic commands. Not sure if it's useful for anything more advanced.
To stop a batch script before it ends, put the pause command on a new line, which will make the script wait for user input (like an enter key) before continuing (or closing).
for a second
PING -n 2 127.0.0.1 > NUL 2>&1
or for 10secs
timeout /t 10 /nobreak
This works for me. Similar to #Sri7's answer but you need the brackets and quotes:
if NOT ERRORLEVEL 0 (
pause
)

Need a line of code for a batch file to open another program and then close the original batch file

I am writing a series of batch files to demonstrate to my class what a computer virus can do. All I need is a SIMPLE line of code that will open another file. Any help is greatly appreciated.
Is this what you're looking for?
#echo off
echo One
start notepad.exe
exit
echo Two
It will echo "One", start notepad, then exit the Command Prompt in which it's running. It won't echo "Two".
If you want to open a document (or a web page, or whatever) rather than an executable, just specify its pathname in place of "notepad.exe" and start will do the right thing.
If you don't want to close the Command Prompt, but just want to end the execution of the batch script, do this:
#echo off
echo One
start notepad.exe
goto :eof
echo Two
That will return you to the prompt without echoing "Two".
Well here is how to open another executable but what do you mean close the original batch file?
start /d C:\Windows\System32\calc.exe
and then to exit early you could just use exit. but if you reach the end of the batch it may just close anyway.

Resources