Batch Script that saves log files - windows

I am currently using a program called USBDeviewer for auditing purposes. Basically what it does is it gets a list of USB devices that were used on a particular PC. The program can create a log file but you have to do it manually. The keyboard command to save a log file is ctrl+A to select all devices and then ctrl+S to save. After you select save you are prompted to enter a filename and location to save it.
I need a batch script that will automatically run this program and save the information on the log file to a folder automatically without any human intervention such as typing in the name (it needs a different name every time, perhaps the filename can include a different date every time).
This is a simple script but I am not familiar with batch scripting so any help would be appreciated.

Save Command-Line Options for usbdeview.exe
/stext Filename : Save the list of all USB devices into a regular text file.
#echo off
set Log=c:\temp\logfile.txt
usbdeview.exe /stext %Log%

Related

Can a file copy be detected on windows?

I want to trigger a piece of code to run as soon as a 'copy' command is executed on Windows. Let's say I want to perform some operations as soon as the user tries to copy a file. Is there any way to detect the 'copy' operation on Windows?
You may monitor the clipboard (see Creating a Clipboard Format Listener) and check if there is an object with format CF_HDROP in it. But it will not tell you when the clipboard paste operation actually started.
This also will not cover copying which does not involve clipboard, like direct file_read/file_write loop in a File Manager application. For this you may monitor changes in the file system (see How can I monitor a Windows directory for changes?) but it will not tell you where was the file copied from, just where was it pasted to.

Windows Batch File for Login Shortcut

I know you can use tsdiscon.exe and tscon.exe to connect to a user account via the command line.
I want to write a simple batch file to accomplish this, so I can have a shortcut on my desktop that will quickly and easily login to another user account on this system.
I share this computer with my in-laws, and they are not very computer literate. Currently there are many steps in switching to their account, and I want to make it more streamlined for them.
It's been awhile since I've written a batch file, but I am sure there is a way to run a simple IF/THEN statement, that would check to see if the user is logged in, then either log them in with the given credentials, or switch to that users profile.
I want to know the syntax to make this happen, so I can put the .bat file on the desktop for them to click on.
Is this possible?
Logging out to your account? Suppose impossible.
Suppose your user is usr1 and you save your batch file to, say, your desktop.
Then, by running this batch file, you run it as usr1.
If your batch file contains the log out statement, it therefore ends the user account, therefore terminating the batch file, thus not being able to run the batch file successfully.
However, switching to a different account could be possible.
Try this out, if it works.
Essentially, you're doing the following:
#echo off
taskkill /IM explorer.exe /F
runas /user:COMPUTERNAME\ACCOUNTNAME explorer.exe
(Replace COMPUTERNAME and ACCOUNTNAME with the current computer name and account name (i.e. C:\Users\ACCOUNTNAME) of your in-laws account)
If that's not what you wish, then, sorry, I can't help you.
The batch script I previously mentioned here
Just give your machine a simple name customise one line in script. Test from command prompt:
fus
If it works with win10 you should be able to create two shortcuts with commands:
fus.bat 1
and
fus.bat 2

Can a batch file write to a hidden notepad file?

Some context:
I have an Excel Macro (.xlsm) file I've created for my company, which I'm creating a batch file for that copies it from a Network folder to a local folder for each computer/user profile that clicks the batch file. What I'm looking to do is for every user that runs the batch file, I want to append their name to a hidden notepad file in the same network folder of the original macro file. That way, whenever I revise/fix/add features to the macro, I'll have a list of all the users that are using the macro file, so I can send an email letting them know of the updates.
Note: I know how to obtain the username of the user that runs the files.
I know that you can write and append to Notepad files using a batch file, but I can't find a source that verifies whether it's possible to write to an existing hidden Notepad file. I came upon this post in my search, but that's using the C# language.
So, my question(s) are:
Can a batch file write/append to a hidden notepad file?
If so, would I use the same syntax/method that you would when writing to a visible (non-hidden) file?
If not, would could I go about this feat? Could I temporarily unhide a hidden file, append to it, then rehide it? Or should I go about it using a different method?
Is this a good method, or is there a more efficient/better way to accomplish this (keeping a list of users that have the macro file)?
You could use stream redirection operator >> and system variable with login as i recall %username%. Do echo %username%>>hiddenfile. txt

Can you run Windows batch file not in a command prompt?

My programming teacher is requiring that everyone participate in the science fair this year, and now they accept computer-related projects. So, knowing only one language (Windows batch), I asked if I could make some kind of text based game in batch code. She said that I can only write the code, but not execute it.
She said that if we're even caught on the command prompt, we apparently get in big trouble and never get access to a school computer for the rest of the time I'm at the high school, or something like that... Even if all the code contains is a bunch of echos and simple variables, she won't let me and my partner run it. She also said that we must work on the project during school in her class.
So to sum all that up, she's letting me write a game/program, but I'm not allowed to test it to make sure it works in school, which will be when I'm writing it most...
So is there any way to run/test a batch script with simple echo and set /p commands without the "dangerous, black command prompt" showing up?
I think she has no idea what she's talking about, because on the board, she wrote "back script" lol
A batch file runs like any other executable file by double-clicking the file within Windows. However, because a batch file runs in a command line it immediately exits when done, so you may only see a black box for a second.
If the batch file is closing too fast, or you want to read the output from the batch file you can edit the batch file and add a pause to the end of the file. The pause command waits for user input before continuing.
A .BAT file can be made into a .EXE or .COM file to keep the program's source hidden from a user. You could use the .BAT to .EXE converter file to convert the batch files to .EXE file. In this program you can point to the batch file you want to convert into an executable file, include additional files, change the icon of the file, and add file details.
For Information on Batch files, this is one of the best resource.
Try this:
#echo off
color F0
mode con lines=25 cols=60
cls
title "Notepad - MyProgram.bat"
set /P "name=What is your name? "
echo Hello %name%, glad to see you...
pause
Execute it via a double click or enter in files browser...

Execute a bat file when an image is viewed

I want to hide a bat file or its commands in a image file so when the image is opened the commands in the bat file is executed.
Assuming you're speaking specifically for the windows platform (what with "batch" files), you can't.
Batch files are parsed, not compiled, so an image file with batch in it would still be read as an image file.
I really can't sum it up more clearly than things just don't work like this.
However, assuming you're doing things ethically, you can re-associate, say, a .jpg extension to be opened with the command prompt and put your batch script in a text file with a .jpg extension.
Just a final note: Questions like these are often looked poorly upon by the StackOverflow members simply because you're hardly ever trying to achieve something like this with good intentions. I won't be the first to flame you, but I definitely won't be the last.
There are a number of ways in which a single file can contain two distinct streams of data which are independent of, and don't interfere with, each other. One commonly cited example of this is how it is possible to append a .zip file to a .jpg file and still have the combined file function as both types of files, depending on what sort of application is opening it. This trick takes advantage of the specific way jpeg and zip files are structured. These sorts of files are akin to polyglot program.
Another common method of embedding unrelated information into a file is steganography, which is the concealment of messages within mundane objects (like an image file.)
You might also consider using an Alternate Data Stream, though in such a case the batch file would arguably not be embedded in the image.
Once you have your batch file embedded, by whatever means, you must then find a way to cause the batch file to be run instead of--or in addition to--whichever image-viewing application the user has configured.
It can be safe to assume that the average user does not expect a file ending in .jpg (for example) to open a cmd.exe window and start executing unknown instructions. Windows Explorer, the application which under most circumstances decides what program to run when a certain file type is double clicked, has sane defaults (i.e. a .jpg file gets opened with an image viewing application) though these can be changed (though users generally don't change these sane defaults to insane customizations like cmd.exe.)
The simplest method would be to write your own handler program and cause it to become the default handler for image file types. I believe that all handler applications must be compiled PE application or library files (.exe, .dll, .scr, etc.) so another batch file won't work here. Your custom handler must reverse the embedding to retrieve the embedded batch file and then execute it with cmd.exe.
But if you've already managed to supplant the default handler with your own code then the embedded batch file is moot since you've already got your code running.
I have to echo what #Di-0xide said about this question. If you're trying to write a legitimate application and this is part of your design plan, then you need to go back to the drawing board because no legitimate application should ever do this sort of thing.
Although there are no doubt many more malace reasons why someone would want to embed a batch file within an image,
and have it execute when the image is opened. I can see at least one legitimate reason for wanting to perform the task.
What if you have a family computer, shared with your 2 kids, the wife, and yourself. Lets say you have some files you don't
want your children to come across, or a directory full of journal entries. It would be pretty neat if a simple batch file that would
change the attributes settings of the directory, containing the sensitive files. This way a parent could rest assure that their
kids wont stumble upon dads nudie pics. LOL
IE: Lets say someone wrote 2 simple batch files s.bat(to show the directory) and h.bat(to hide the directory)
hide.bat
#echo off
REM Show the directory containing sensitive info:
attrib +a +s +h +r C:\path\to\the\directory\to\hide
REM Hide this batch file(or image containing the batch file):
attrib +a +s +h +r C:\path\to\the\batch\files\hide.bat
REM Show a batch file(or image containing the batch file) that will allow for showing the directory:
attrib -a -s -h -r C:\path\to\the\batch\files\show.bat
show.bat
#echo off
REM Show the directory containing sensitive info:
attrib -a -s -h -r C:\path\to\the\directory\to\hide
REM Show the batch file(or image containing the batch file) that will allow user to hide directory:
attrib -a -s -h -r C:\path\to\the\batch\files\h.bat
REM Hide this batch file(or image containing the batch file):
attrib +a +s +h +r C:\path\to\the\batch\files\s.bat
I think it would work better having images that would execute the show.bat and hide.bat batch files, rather than just changing the icon on each batch file.
Perhaps writing a handler program that would change the attributes of the directory then open the image and associate the handler to some unused extension, would work.
Just a thought.
Simplest way to do this is to just create your batch file as normal and then create a desktop shortcut of the same batch file. Finally right-click on the shortcut and change the icon to what ever image you want. Once all changes are made then anytime the image is opened, the batch file will run.
This is easy just use winrar archive to embed bat file in image. This will run bat file before image or you can change settings to run image file before bat.
Follow this tutorial to do so:
https://www.youtube.com/watch?v=ytvKVYYrqEw

Resources