Use batch file to launch ssdeep with file arguments - batch-file

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"

Related

Open, wait, save, close, and copy a file using Windows batch file commands

With my .bat I would like to:
open the xlsx file,
waiting 2 min,
close the file with save options
copy this file to another folder.
For now I can copy and paste the file, but I don't know how to open it, with a cmd function, and save it.
Thank you for your help.
My code is :
#echo off
cmd "O:\XXXX\*.*"
xcopy/y "O:\XXXX\*.*" "O:\XXX\"
pause
Marie (TooLong;ToRead) in disjointed comments
I suggested, A simpler alternative method to do what you need on this
occasion is to use a simple command line tool see Orlandos Sendkeys
Utility (the example is almost what you want to do)
download sendkeys from cpap.com.br/orlando
see how the demo runs
open excel with a blank sheet and at a CMD> run this demo string
SendKeys.exe 1.5 10 "Microsoft Excel" "Hello!~{PAUSE 2}After 2s.~{PAUSE 2}%(FS)~"
adapt to your own version of excel keys since the %(FS) is ALT File Save in English
you replied
#KJ Thank you, KJ, unfortunately I can't download Orlando with my PC.
So we continue to doing it in a more dirty fashion, but you still need a means to save the file by invoking an autosave which would most easily be done using an extended excel macro in your source .xlsm, anyway
after all these changes your non working file should now be replaced in your question as
#echo off
start "Excel Running" /MIN EXCEL.EXE "\\XXX\Fichier.xlsm"
REM add a delay of **2 minutes !** whilst sheet recalculates before saving a copy
timeout 120
REM copying a file that has NOT been saved using keys at this point will NOT
REM be what you really need to solve your problem unless you use a macro ?
REM see Later
xcopy/y "\\XXX\Dossier_avant*.*" "\\XXX\Dossier_apres\"
REM add a 3 second delay to check above worked but is not really needed
timeout 3
REM temporary for debugging. Later just REM it out
TASKLIST /M |Find /i "exce"
REM this line should be working with either a SUCCESS: or ERROR:
TASKKILL /T /F /IM excel.exe
REM keep this line for seeing errors above, once happy, it can become REM PAUSE
PAUSE
I think that IF you are constrained (by IT policy) to the command line it is best you write your own autosaving macro, however, MY problem is I dont know if you need it for more than one input.xlsm.
So save this as OpenRunSaveExit.vbs in your working folder where your .bat is. There is a reason I did NOT use spaces or & in the name for a later step.
Set WshShell = WScript.CreateObject("WScript.Shell")
' You may need to include the path to excel.exe if it is a portable version like mine
WshShell.Run "EXCEL.EXE "+"\\XXX\Fichier.xlsm", 9
' 120000 milli-seconds = 2 minutes
WScript.Sleep 120000
' These are the English key combinations for ALT+File+Save . SO alter or remove if not needed
WshShell.SendKeys "%FS"
' These are the English key combinations for ALT+File+eXit . SO alter if needed for french excel
WshShell.SendKeys "%FX"
' Lets us wait 2 seconds for clean closure
WScript.Sleep 2000
As Peter has pointed out in his answer you need to /WAIT before xcopy and depending on how your vbs file handling is set-up you may not need Wscript in the start line
NOW replace your .bat with this
#echo off
start /WAIT Wscript OpenRunSaveExit.vbs
xcopy/y "\\XXX\Dossier_avant*.*" "\\XXX\Dossier_apres\"
pause
And check it runs without the need for taskkill.
Finally why use a 2-4 line .bat since a desktop shortcut would potentially be easier to use. So make a shortcut for the .vbs file (right click the .vbs, and in English its Create Shortcut) and wherever it is built move it to your desktop.
Then change the properties like this (where & has a special meaning so the .vbs filename must NOT have spaces or &.)
%comspec% /c "start /wait wscript.exe OpenRunSaveExit.vbs & xcopy/y "\\XXX\Dossier_avant*.*" "\\XXX\Dossier_apres\" & pause"
P.S. I forgot to add Peters start / wait in this image until later
You can't interact with Microsoft Excel (or most of other programs) through Batch unless they provide such an interface. There is however an option to do it with VBS i.e. via an interface Microsoft Excel supports for interacting with that software.
For just opening the program check start command e.g.:
start /B excel.exe <filename>
then you can wait for the user to both edit and save the file for two minutes or also utilize pause if you don't want to introduce a race condition between saving and copying with xcopy.
Alternatively use start like this:
start /WAIT /B excel.exe <filename>
so the opened file blocks the operation and once it closes (no edit or saving by the user guaranteed) then it'll unblock and xcopy would take place without any time-dependent feature.

Batch (X) Key, Want to End Process

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.

open an unsupported file type with a program automatically in a batch file

I am trying to open a file using a program. I would like to automatically do this with a batch file. Everything I need to run is in the same folder. I am not that good at batch files or coding but I am trying to get better, if anyone knows how to do this that would be great.
I have tried
.\location\application .\location\file
exit
and
START /B /I ".\location\application" ".\location\file"
exit
i have tried switching the order of the app and file but it does not work, most of the time it does nothing, but sometimes it opens the app but does not open the file.
The approach:
START /B /I ".\location\application" ".\location\file"
will fail because Start takes the first argument in parentheses double quotes as the window title. Insert a dummy pair of double quotes to circumvent this.
START "" /B /I ".\location\application" ".\location\file"
The help start /? doesn't explicitly state this.
ss64.com only says:
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "" According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.

Windows server 2008 doesn't run .bat file

I have a problem about some commands in batch file with windows server 2008.
When i try to use clip command on command prompt, it works.
But when i use the clip command in a batch file and run the batch file, the clip command doesn't work. What should I do on this issue or where is my fault?
EDIT:
set wp=D:\workset\xxx\MC_DIM\exchange\
d:
cd %wp%
for %%i in (xxx_to_yyy*.zip) do (
echo %wp%%%i | clip
)
When I try to only echo I can take the full path with batch file, also when I use clip command from cmd it works but when i create a batch file and try to use it doesn't copy to clipboard. Should it be about privileges or sth ?
In my hands, your code works, except it only copies the last file listed to the clipboard. That is because each file listed gets its own pipe to a new CLIP command. Each CLIP command overwrites the result of the prior one.
You need to somehow list the full path to the files and pipe the entire result set to a single CLIP command. The easiest solution that does not use a temporary file is to put parentheses around your FOR statement. Each side of the pipe is executed in its own CMD.EXE process, with ECHO state defaulted back to on. So the command in the DO clause needs the # to suppress echoing of the command.
There is no need to include the %wp% in your output. The ~f FOR variable modifier will supply the full path.
#echo off
set wp=D:\workset\xxx\MC_DIM\exchange\
d:
cd %wp%
(for %%i in (xxx_to_yyy*.zip) do #echo %%~fi) | clip

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
)

Resources