Can a batch file write to a hidden notepad file? - windows

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

Related

Opening a file **in** a batch file

I have a batch program that can be used for opening a specific file format I created. The file format is a actually a zip file with the .zip changed to .gcif.
When you run my btch file there is an input field for entering the location of the .gcif file you want to open. The .gcif is then renamed to .zip. The 7z.exe (which is in the same directory as the batch file) then unzips the file and it is processed by my batch file.
But I would like it to also work when the user finds any .gcif file, right clicks it, and selects Open With my batch file. Is there any way for batch file to detect that ithas been tasked to open that specific file?
I figured it out. When a file is opened with a batch file, the file location is passed on to the batch file as a parameter. The parameter can then be accessed using %1.
For example, if I opened the file C:/document.txt in a batch file containing this code:
#ECHO OFF
ECHO %1
The output would display C:/document.txt.
You can register a windows file handler in your system.
You might need admin rights and might need to modify the registry.
Essentially you "connect" your filextension .gcif with your batch file so windows knows that any file of this extension is connected to your batchfile
You can read something about it on the msdn site here: How to Register a File Type for a New Application
If you plan to associate one or more file types with a new
application, you must define a ProgID for each file type that you want
to associate with the application.
To create a ProgID for each unique file type that your application
handles, use these steps.
Instructions
Step 1: Note that some file types have multiple
extensions that point to the same ProgID; for example:
HKEY_CLASSES_ROOT\App.jpeg (your ProgID)
HKEY_CLASSES_ROOT\.jpg = App.jpeg (the file type mappings)
HKEY_CLASSES_ROOT\.jpeg = App.jpeg
Step 2:
Remove the ProgID values when you install and uninstall your
program.
Step 3:
Leave the file type mappings unchanged at uninstall time.
Doing so works because file type mappings are stored per user in
HKEY_CLASSES_ROOT.ext, and the system identifies the case where the
ProgID value is missing and ignores it. Leaving file type mappings
unchanged avoids the need to have conditional code that only removes
the file type mapping if the value still points to your ProgID. It is
important to avoid doing so in cases where it might have been changed
by another application and you thus cannot easily remove the value.
Step 4:
Specify a unique value for the file type description of each
file type ProgID by doing one of the following:
Leave the default value of the ProgID empty, in which case the system
uses the .ext file. Provide a localized value via FriendlyTypeName
and, for compatibility with old applications that read the registry
directly, be sure to provide the default value of the ProgID as the
file type description (that is, use the same value that is referred to
by the FriendlyTypeName in the English resource). Remarks If you plan
to associate the file with an existing application, locate an
application ProgID in the registry.
To accomplish something similar you can open your file once with Explorer and chose "Open with..." - locate your batch file and choose "always open with this application" checkbox.
The first option here is more for when you want to provide 1-click-open experience to customers for your application when they install it on their system.

How to manage files with a batchscript via "Send To"?

I want to improve my file managent and need some help to get started.
For this specific script, my idea is the following:
while in Explorer
rightclick a file -> Send To -> "Apps[.bat]"
Then the script starts and does the following:
moves the whole directory (and subfolders) of the selected file to a specified location (in my case C:\Users\Name\Desktop\Apps\Files)
so sending Name\Downloads\Coolportableapplication\Coolapp.exe to the batch results in Name\Desktop\Apps\Files\Coolportableapplication\Coolapp.exe
creates a shortcut of C:\Users\Name\Desktop\Apps\Files\Coolportableapplication\Coolapp.exe in C:\Users\Name\Desktop\Apps
Is this generally possible with a batchscript?
This isn't my first batch but I've never dealt with unspecified files and English isn't my native language so I couldn't really find anything useful for my cause, though I am sure my little script won't be too much of a problem for you. Based on that batch, I want to create similar scripts but I just have no idea how to begin with that one.
Thanks a lot!
No need for a batch file but anyway.
1: Type shell:sendto into your explorer window and copy the shortcut to your directory "C:\Users\Name\Desktop\Apps\Files". This directory will now show as default in your Send To options.

Batch Script that saves log files

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%

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

Redirect default program to another program when a file opens in Windows OS

This is only under windows env.
As I know windows os identifies associated application of a particular file by file extension.
Like wise each file (binary) starting with corresponding symbols ("starting symbols"). For an example .JPG starts with ÿØÿà. Let say I open this .JPG file in a Hex editor or a Text editor and then I change that starting symbols into another file type. for an example I can change ÿØÿà to .Eߣ (.mkv). So when I double click on the .JPG the Windows Photo Viewer says there are some errors or similar message. So I need to get some information about the application that tries to open that kind of a file. If I can, I need to open that file using the application that associated with "starting symbols".
Briefly when I open .JPG I need to open a default video player .mkv files. But It may not work for this example. Because I changed only the "starting symbols" of my .JPG.
Please give me any idea to do this.
Thanks!
When you encrypt the file, give it a new extension. e.g. Picture.jpg becomes Picture.encrypted-jpg. You then register as the handler for encrypted-jpg, decrypt the file, then launch the normal jpg handler.
When the shell is asked to perform a verb on a file, the shell does not use the contents of the file to determine which app to pass it to. The file extension is what determines how the file will be treated.
You wish to use the contents of the file to influence which app processes a shell verb. In order to do so you would need to create a launcher app that reads the file header and then decides which app to pass the file on to. You would assign your launcher app as the handler app for all file extensions that you were interested in.
Although you could do this, it would be much easier just to set the file extension appropriately.
The proper way to do this sort of thing is to replace the files with reparse points.
The downside is that this involves writing a file system filter driver, i.e., an operating system extension, which is a whole level of trouble above and beyond ordinary application programming. (Since Windows already does file encryption, I doubt it would be worth the effort.)

Resources