Batch file detect if NTFS alternate stream can be written to - windows-7

Background
In Windows 7, when a file is downloaded from the internet, some browsers (e.g. IE and Firefox) flag it as coming from the internet. This is apparent in the properties dialog of the file, which will show a message and an "Unblock" button at the bottom of the properties window.
This property is stored as an alternate stream on the NTFS filesystem - specifically, a stream named "Zone.Identifier". So on a blocked file, you can run the command more < file.exe:Zone.Identifier and you get the output:
[ZoneTransfer]
ZoneId=3
You can clear this data with the command echo. > file.exe:Zone.Identifier. This overwrites the above data with simply a blank line, and while the Zone.Identifier stream still exists on the file, the file is no longer "blocked" as confirmed by the properties dialog.
Problem
FAT32 file systems obviously don't have NTFS alternate streams; so, the command echo. > file.exe:Zone.Identifier gives the output:
The filename, directory name, or volume label syntax is incorrect.
This is output to stdout, so adding 2>NUL on the end does not suppress it. Adding 1>NUL to the end DOES suppress it, however it also suppresses the command from doing anything useful; that is, if you run echo. > file.exe:Zone.Identifier 1>NUL, the Zone.Identifier stream remains.
How can I run the command echo. > file.exe:Zone.Identifier successfully on NTFS, and suppress its error output on FAT32?

Command echo. > file.exe:Zone.Identifier 1>NUL causes redirection of echo. to NUL, the first redirection is ignored.
Adding 2>NUL causes redirection of echo's stderr to NUL. The message you are trying to avoid is printed to stderr by failed redirection and not by echo command by itself.
The solution is to devide the command into two phases by using brackets:
(echo. > file.exe:Zone.Identifier) 2>NUL
This will cause echo. to be executed first and its output redirected to alternative file stream. If trying to write to alternative file stream on FAT filesystem failes, then it's output to stderr will be redirected to NUL.

Related

windows cmd dos not show complete output in a text file

if I open windows cmd and type this command:
copy D:\1.txt C:
then I see this result:
Access is denied.
0 file(s) copied.
but with this command:
copy D:\1.txt C:\ > d:\output.txt
output text file shows only this :
0 file(s) copied.
why "Access is denied." is not in output.txt? and how could I have complete output in text file?
Redirecting a command's output to a file via > somefile.txt only redirects the data written to "standard output" stream to the file, it does not include data written to "standard error" stream, which is where the "Access is denied." message is written in your case. To capture all output, you need to append 2>&1 to the end of the command, e.g.:
copy D:\1.txt C:\ > d:\output.txt 2>&1
This can be understood as an instruction to send data written to standard error (stream 2) to standard output (stream 1).
You can also redirect standard error to a different file to standard output with e.g.:
copy D:\1.txt C:\ > d:\output.txt 2> d:\errors.txt

Make System information Batch file

How to create a batch file about system info and ip address and domain server etc. _ which will generate a output as txt file
You can put whatever Windows commands you want in a batch file and then redirect the output to a text file using >. E.g., you could create a batch file called test.bat and put the following commands in it:
#echo off
systeminfo
ipconfig /all
Then you could run test.bat from a command prompt as follows:
C:>test >outfile.txt
The output of the batch file, which would consist of the output from the commands you placed in it, would be redirected to a file named outfile.txt by the redirect operator, >. Note: it can sometimes take awhile for the output of the systeminfo command to complete, so, depending on your system, you may need to give it a minute to complete.
Alternatively, you could add the >outfile.txt at the end of commands within the batch file itself, though for every instance after the first one you need to use double greater than signs, i.e., >>, to append the output rather than wiping out what was in outfile.txt previously and creating a new version of that text file. E.g., you could have the following in test.bat:
#echo off
systeminfo >outfile.txt
ipconfig /all >>outfile.txt
You would then just use the following at a command prompt:
C:>test

How do I store the output to a file AFTER cmd execution?

I have the below code that executes and stores the output to a text file, and then display the output to console.
robocopy %TOBEZIPPED% %TEMPDIR% *.* /E > Log.txt & type Log.txt
but since I'm using the robocopy command that shows progress while it is copying, I would like it to show as it was intended and then store the output (maybe history of the command) to a text file..
How can I do it? I've tried doskey /history from a google search but can't still solve my issue.
Hope someone can help me.. Thanks in advance..
EDIT: I have searched related questions but have not found the same with what I wanted.. please note that the result of output should be displayed first normally (not echoed or typed, see robocopy command) before redirecting it to the output file.. so it's like command will display first as usual, like a command history - after execution, will then be redirected to an output file..
For Shell only:
Use tee:
tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input.
e.g.
robocopy %TOBEZIPPED% %TEMPDIR% *.* /E | tee -a Log.txt
If you want to do it in windows you need to use PowerShell http://en.wikipedia.org/wiki/Windows_PowerShell . This will provide you tee command to be executable on windows.

batch command to log file

I wanted to capture the contents of DOS command file (.CMD) to a log file.
I can understnd I can capture the individual commands to log file using > or >>.
However I wanted to copy the contents of the complete dos screen into a log file as well.
I have also noticed when I use >> it removes the output message ( 1 file copied) from the dos console and moves in the log file. Is it possibl I can leave the message is displaied in both console and log file.
Any help will be appreciated.
However I wanted to copy the contents of the complete dos screen into a log file as well.
Make sure not to use "echo off" in your script.
it removes the output message ( 1 file copied) from the dos console and moves in the log file
Redirect stderr to stdout before appending:
myscript.bat 2>&1 >> myscript.log
See here a good resource on batch file redirection.
Is it possibl I can leave the message is displaied in both console and log file
You need "tee". Download a good one for Windows from here. Then do this instead of the above.
myscript.bat 2>&1 | tee -a myscript.log
myscript.bat >> myscript.log 2>&1

Create file command in batch files (*.bat)

I want to create a file named "new text document.txt" in the folder %tv% using a batch file ( *.bat). This is my batch file:
set tv=D:\prog\arpack96\ARPACK\SRC
cd "%tv%"
#CON >> "new text document.txt"
set tv=
Although I can really create the file in %tv%, but when I run the above batch file, I will get an error message saying
' ' is not recognized as an internal
or external command, operable program
or batch file.
Is there anyway to get rid of this error message? Or I am creating the file using the wrong command?
In order to get a truly empty file without having to provide user interaction, you can use the set /p command with a little trickery:
set tv=c:\documents and settings\administrator
cd "%tv%"
<nul >"new text document.txt" (set /p tv=)
The set /p asks the user for input into the tv variable after outputting the prompt after the = character.
Since the prompt is empty, no prompt is shown. Since you're reading from nul, it doesn't wait for user interaction. And you can store the empty string straight into tv thus removing the need to unset it.
Actually, after some more thought, there's an easier way. I've used that set /p trick in the past since it's the only way I know of echoing text without a newline being added (great for progress bars in the console). But if all you want is an empty file, you can probably get away with:
copy /y nul "new text document.txt"
The copy simply copies the contents of the nul device (effectively an empty file) to your new file name. The /y is to ensure it's overwritten without bothering the user.
type nul > file.txt
file.txt should be created empty
or paxdiablo's answer
Create an empty file from a *.bat file - this worked for me.
echo off > test.txt
con is the windows specialized file name and it should not be used.
copy con >> filename.txt
shall ask you to enter the text and you can save the text by typing Ctrl Z.

Resources