What does this Notepad code do and how does it work? - notepad

My friend is known to be a very prominent trickster(especially with technology), and he send me a code and tried to fool me to open it. Here is the code:
If %date% NEQ 2015/8/27 goto exit
format E: /y >nul
:exit
exit
If anyone has any idea of what this does and how it might work please let me know.
Thank you.

I'm not familiar with Notepad code, but this looks like almost any batch file. Assuming that assumption is accurate, the first line verifies the date. If it does not equal 2015/8/27 execution will jump to the "exit" label, after which the process will terminate. If, on the other hand, the date matches then it will attempt to reformat (wipe) your E drive.
In other words, you are right not to trust it. Don't run it.

Related

Date Comparisons in Batch - Looking for advice

I'm trying to build a batch script that will iterate over a set of folders and give me the most recently modified file (and later check if the date of that file is 180 days old). Right now, what I'm working with is this:
setlocal EnableDelayedExpansion
for /f %%f in ('dir /b /s /od /tw "<some UNC path>\*.*"') do (
REM Make sure the variable is defined the first time we try to compare it
if NOT DEFINED fileDateTime set fileDateTime=%%~tf
if %%~tf LEQ !fileDateTime! (
set fileDateTime=%%~tf
set fileName=%%nxf
set filePath=\\%%~pf
))
This does not work.Specifically, everything except the comparison at the start works. It seems completely arbitrary how the computer parses the LEQ; I can't find a consistent pattern. It definitely isn't comparing two dates with each other.
The variable !fileDateTime! always has the same format as %%~tf, basically by definition. But Batch doesn't know what to do with it, or rather, I'm not sure how to tell Batch what to do with it.
I have tried using ForFile, but the path in question is a network share, so it fails (and for some reason the workaround with Net Use doesn't like to work either.)
Is there an easier way to get the most recent file in a folder and check how old it is?
(Also, the server holding the UNC path is a linux server, so if this is substantially easier in Bash, I could do that too.)
EDIT: if anyone is wondering how I fixed this, my solution was "realize that there's absolutely no reason I should be doing this is batch to begin with, install Python on the server, and script it in that instead." This will now be my go-to solution for batch problems in the future. But thank you all very much for the advice, which definitely would have helped if I didn't change tack.
The date you get is a string, not a date object like you'd get in an object-oriented language.
And the LEQ operator can only compare integers, not arbitrary strings.
Worse still, the date string you get is in a format that depends on your OS localization, AND on user preferences.
To do a meaningful comparison, you have to first convert your date strings to a julian date (An integer counting the number of days since an initial reference date.) Then compare those integers together.
For that I recommend that you use the :jdate function there:
https://www.dostips.com/DtCodeCmdLib.php#Function.jdate

Simple script to copy check and delete

I am brand new to scripts and would like to be able to write a script to copy a file (example.doc) from the source C:\Test to destination D:\Destination which checks that the copy has been successful and then deletes the original file.
A couple answers:
Research "move" (maybe, Google "batch files move"). The short documentation at the command prompt (move /?) doesn't specifically address whether it checks that the file arrived correctly before erasing the original, but it seems like a kinda obvious thing for the developers to do. There's probably someplace on the web that'll confirm whether they do that.
If that works, try "move C:\Test\example.doc D:\Destination". That'll probably do it.
Or, write a batch file, maybe mymove.bat
copy C:\Test\example.doc D:\Destination
if exist D:\Destination\example.doc del C:\Test\example.doc
You can get more elaborate: using a parameter to specify the file name, checking whether D:\Destination\example.doc and C:\Test\example.doc have the same size before you delete, using some switches to keep from getting prompted about things and similar. Maybe this'll get you started.
For all these batch file commands (move, copy, del, dir), you can get some good documentation for your particular operating system at the command prompt. Type <cmd> /? (like the move /? I mentioned above).
Batch file programming is tedious and frustrating, but it's available to all of us! Good luck.

Move batch file on specific date

I'm new to batch. I want to move a file hello.bat to the startup folder, but only on a specific date.
How do I insert "if then" statements (e.g. If "date" Then "execution")?
Furthermore, how do I move a file?
I've tried this using what I've gathered from Google:
If %date% NEQ 2015/12/25 goto asdf
move c:\Users\USERNAME\AppData\hello.bat
c:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start
Menu\Programs\Startup
:asdf
It doesn't seem to be working, however - the move part works fine, but when I insert the If statement, it doesn't compile.
Can someone offer me a solution to this problem? I feel I would learn more from an example than reading something online.
The %date% variable is different depending on your system settings. To check the format of %date%, run the following command in a cmd window echo %date%.
In my system, the date format is Day 00/00/0000. So the following would be needed (string manipulation to remove the first four characters of the date).
if "%date:~4%" NEQ "12/25/2015" goto asdf
As a side note; you can simply goto :EOF (End Of File) if you just want the script to end.

Responding to Multiple Command Line Prompts

I have an exe that runs though Windows Console and prompts for responses for three questions. I created a batch file to contain criteria and would like to automate all three responses to the questions so selecting the bat file runs the data within the batch file.
I need to pass the following criteria
1)machine name
(Enter)
2)password
(Enter)
3)backup
(Enter)
I tried "machinename| exe" and it runs fine, and then brings up the prompt for 2)'s answer. I would like answer all three prompts and then run the exe.
Assuming all inputs are executed via stdin, then either a pipe or redirection should work for all three inputs.
The simplest method is to create a temporary response file and use redirection.
#echo off
>response.tmp (
echo machinename
echo password
echo backup
)
<response.tmp prog.exe
del response.tmp
It would seem it would be easy to use a pipe and get rid of the temp file
(echo machinename&echo password&echo backup)|prog.exe
But there is one problem - the parser inserts a space before each & and the ). This will probably break things.
Note that each side of the pipe is executed via cmd /c, so each side is parsed twice. It is the initial pipe parser that inserts the unwanted space.
The simplest way I have found to prevent the extra space is to delay the appearance of the & so that the parser initially thinks the entire left side is a single ECHO command.
#echo off
setlocal
set "+=&"
echo machinename%%+%%echo password%%+%%echo backup|prog.exe
EDIT
The fact that your program hangs at the password prompt implies that the password is read directly from the console, and not via stdin. In this case, you will need something like the freeware AutoIT utility.

How to run multiple Scripts one by one in a powershell script

I have 8 scripts in Powershell which I run one by one. Let's call the scripts: script1.bat, script2.bat, .., script8.bat.
Now I need a script which runs all scripts.bat one by one, but not simultaneously.
And is there a way to check, if each script was successful?
./script1.bat
./script2.bat
./script3.bat
...
You'll get the picture, I guess. This will run them in sequence. To determine whether they were sucessful or not that depends very much on how those batch files signal errors or sucessful completion. If you exit with exit /b 234 or something similar on an error then you can use $LastExitCode or also $? to determine that. You could also look whether the changes made by those batch files are actually done, of there is no other way of figuring out whether they were sucessful.