Code: Select all
@echo off
::Init ADS filename
set "thisbat=%~dpf0"
set "ADS=%thisbat%:TestADS"
::create ADS & show contents
echo this is a test > %ADS%
more < %ADS%
:Retrieve ADS
FOR /F "delims=." %%a IN (%ADS%) DO set data=%%a
:Display results
echo.
echo ADS data=%data%
And here were the results:
Code: Select all
C:\Users\Marc\Desktop>testing
this is a test
ADS data=this is a test
C:\Users\Marc\Desktop>
For the most part you can do anything with an ADS that you would do with a normal file, in fact when you access a normal file in NTFS you're implicitly accessing the ":data" data stream, because it's the default when no "alternate" data stream is given. You're right that not all shell commands understand the ADS syntax with the colon, so don't try to get too crazy complicated, but just storing a variable value and retrieving it later is simple, and avoids the clutter of temp files or registry keys.
This is generally advertised as a malicious technique, used to store executable content or batch files in otherwise inconspicuous files, and later run said content with "start" or "call". I've been itching to put it to use for something practical, but my own scripts go on and off USB sticks too often which would strip the ADS...