Pass Named Parameters to Batch Script with Special Characters - batch-file

I found and modified a code snippet to allow passing unlimited named parameters to a batch script.
Accessing unknown number of commands (parameters) in batch file
Everything was working great, but now I'm building in Wildcard checking into the script and I found if I pass a value like this "FILEPATH=C:\tmp\test *.txt" that FILEPATH doesn't get defined by my code snippet. As I didn't truly create it I am partly unaware of how it works and could be modified to allow special characters.
Here is the code snippet to allow named params that I'd like guidance on modifiying:
::Set Named Arguments
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
set %%x
)
Update:
I changed the for loop to for /F delims^=^"^ tokens^=* %%x in (%*) do ( and it will now define the FILEPATH with a WILDCARD, but it strips the first " and then makes all the arguments into one line and also strips the final ". Perhaps I need a way to use the argcount to correlate the alphanumeric position of the set %%x line?
Another thought, since the above change to the for loop does accept the wildcard, but creates a single long variable containing all params passed to script.cmd, perhaps I can loop over it (the long variable) again and split up the named arguments.
Update:
Example usage:
script.cmd:
#ECHO OFF
CLS
::Set Named Arguments
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
set %%x
)
ECHO %FILEPATH%
ECHO %VAR%
EXIT /B
test.cmd:
#ECHO OFF
CLS
::Doesn't Work
CALL "C:\tmp\script.cmd" "FILEPATH=C:\tmp\tes*.txt" "VAR=2"
PAUSE
::Works Fine
CALL "C:\tmp\script.cmd" "FILEPATH=C:\tmp\test.txt"
PAUSE

Using your current method by defining FILEPATH= as a parameter.
Note:
I need to express that this is trending a little on the dangerous side. Reason being, if any of the input variables contains something like PATH=Somepath it will break the immediate environment while the script is running. So ensure you check the input types that will be passed.
#echo off & setlocal enabledelayedexpansion
(set "%~1" & set "%~2" & set "%~3" & set "%~4")>nul
set argCount=0
if defined FILEPATH (
for %%x in ("%FILEPATH%") do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
echo argVec[!argCount!]
)
echo %FILEPATH%
) else (
echo FILEPATH not defined
)

My full solution based on #Gerhard's awesome answer. This still allows me to take an unlimited amount of variables input in unknown order in "VALUE=KEY" format, and not know the FILEPATH positional argument, but as batch has limitations on using only %1-->%9 I felt it easiest/best to handle/allow that FILEPATH be any of the first 9 PARAMS. This really taught me about the things you take for granted in shells like BASH and also, what BASH is doing "behind the scenes". The idea was to build in wildcard searching as my script.cmd will always be called by a "parent script" w/ params and I want it to be similar to BASH (allow end users to use wildcards).
script.cmd:
#ECHO OFF
CLS
::SET Named Arguments
SET argCount=0
for %%x in (%*) do (
SET /A argCount+=1
SET "argVec[!argCount!]=%%~x"
SET %%x
)
::Wildcards in FilePath?
(SET "%~1" & SET "%~2" & SET "%~3" & SET "%~4" & SET "%~5" & SET "%~6" & SET "%~7" & SET "%~8" & SET "%~9")>nul
SET argCount=0
IF DEFINED FILEPATH (
FOR %%x IN ("%FILEPATH%") DO (
SET /A argCount+=1
SET "argVec[!argCount!]=%%~x"
)
CALL :FindFileWildCard "%FILEPATH%" FILEPATH
) ELSE (
ECHO No "FILEPATH=C:\path\print.doc" Defined!
PAUSE
GOTO:EOF
)
ECHO %FILEPATH%
ECHO %VAR%
ECHO %VAR2%
ECHO %VAR3%
ECHO %VAR4%
ECHO %VAR5%
ECHO %VAR6%
ECHO %VAR7%
ECHO %VAR8%
ECHO %VAR9%
ECHO %VAR10%
GOTO :EOF
::Functions
:FindFileWildCard
::Does Path contain WildCards?
ECHO "%~1" | FIND /i "*" >nul
IF %ERRORLEVEL% EQU 0 (
FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
SET %2=%%F
EXIT /B
)
)
ECHO "%~1" | FIND /i "?" >nul
IF %ERRORLEVEL% EQU 0 (
FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
SET %2=%%F
EXIT /B
)
)
EXIT /B
:EOF
test.cmd:
#ECHO OFF
CLS
CALL "C:\tmp\script.cmd" "VAR=VAR" "VAR2=VAR2" "VAR3=VAR3" "FILEPATH=C:\tmp\tmp space\te*.txt" "VAR4=VAR4" "VAR5=VAR5" "VAR6=VAR6" "VAR7=VAR7" "VAR8=VAR8" "VAR9=VAR9" "VAR10=VAR10"
PAUSE
CALL "C:\tmp\script.cmd" "VAR=VAR" "VAR2=VAR2" "VAR3=VAR3" "FILEPATH=C:\tmp\tmp space\test with spa?*.txt" "VAR4=VAR4" "VAR5=VAR5" "VAR6=VAR6" "VAR7=VAR7" "VAR8=VAR8" "VAR9=VAR9" "VAR10=VAR10"
PAUSE
CALL "C:\tmp\script.cmd" "VAR=VAR" "VAR2=VAR2" "VAR3=VAR3" "FILEPATH=C:\tmp\test.txt" "VAR4=VAR4" "VAR5=VAR5" "VAR6=VAR6" "VAR7=VAR7" "VAR8=VAR8" "VAR9=VAR9" "VAR10=VAR10"
PAUSE
Result:
C:\tmp\tmp space\test with space.txt
VAR
VAR2
VAR3
VAR4
VAR5
VAR6
VAR7
VAR8
VAR9
VAR10
Press any key to continue . . .

Related

Variables are not set properly inside nested loops

I have a similiar question like I did ask here: Nested loop
However, I still did not solve the "problem" which seems to be little. I already implemented the solution mentioned using [FindStr][2], however the runtime is much much longer than without FindStr. So I would like to keep the method using
IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Something
Below I post some runable code. In the beginning I just set my array with the values I would like to search for and then some exmaple "texts" in which I like to search my beforehand set searchvalues.
My strategy:
Loop through my file with the text lines. For each line test each SearchValue for presence.
After all Searchvalues tested, go to ne next line and check also for the presence of each searchvalue.
So I have two nested Loops. At every step inside the loop I output the current vaule of the variables and those seems to be correct. I really do not know how my fault in this line is as the searchfunction is not working correctly:
IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Searchword is in Textline
There may be just a little mistake in here? I am very thankful for every tipp in this issue.
#echo off
setlocal enabledelayedexpansion
set /a counter=0
set "searchValues=abc,xyz"
FOR %%G in (%searchValues%) do (
set arraySearchVal[!counter!]=%%G
set /a counter+=1
)
REM set arraySearchVal
set /a counter-=1
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR /l %%I in (0,1,%counter%) do (
set "searchValue=!arraySearchVal[%%I]!"
echo Searchword: !searchValue!
REM echo !stringToTest! found in Textline | findstr "!searchValue!"
IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Searchword is in Textline
)
echo/
)
endlocal
pause
This falls under the K.I.S.S. methodology. By making your search arguments array variables you have over complicated your code. You can simplify it like so.
#echo off
setlocal enabledelayedexpansion
set "searchValues=abc,xyz"
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR %%I in (%searchValues%) do (
echo Searchword: %%I
IF not "!stringToTest:%%I=!"=="!stringToTest!" echo Searchword is in Textline
)
echo/
)
endlocal
pause
If you really want to use your original code you can get the double variable expansion using a trick with the CALL command to set another variable. Then use that variable with the IF command.
#echo off
setlocal enabledelayedexpansion
set /a counter=0
set "searchValues=abc,xyz"
FOR %%G in (%searchValues%) do (
set arraySearchVal[!counter!]=%%G
set /a counter+=1
)
REM set arraySearchVal
set /a counter-=1
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR /l %%I in (0,1,%counter%) do (
set "searchValue=!arraySearchVal[%%I]!"
echo Searchword: !searchValue!
REM echo !stringToTest! found in Textline | findstr "!searchValue!"
call set "temptest=%%stringToTest:!searchValue!=%%"
IF not "!temptest!"=="!stringToTest!" echo Searchword is in Textline
)
echo/
)
endlocal
pause
Before getring into this too much, why are you creating a list of counter values if you don't need to? Why bot loop the 1st set and 2nd set wirhoutbhaving special variables for a for L loop, as you don't then assign the positional value.
Another bit, are all of the terms you are looking to see if exist in the text strings partials? Or are you always going to match the full string? If you are matching full strings you only need one loop.
IE if possible strings to match are the Full value, or "whole word" you want to find in the set to look through.
EG
REM this sees if any of the arguments match the list of options using only one loop.
Set "_OptionsToMatch= -D -Delete -R -Remove "
FOR %%_ in (%*) DO (
Echo=%_OptionsToMatch% | FIND /I " %%~_ " )
Assuming the above doesn't work for you in this scenario for some reason
Well lets address the other bits like why are you crwating a valued array at all?
#(echo off
setlocal enabledelayedexpansion
set "searchValues=abc,xyz"
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
)
CALL :Main
( Endlocal
Pause
Exit /B
)
:Main
FOR %%G in (%TextWhichNeedToBeSearched%) do (
SET "stringToTest=%%G"
FOR %%g in (%searchValues%) do (
IF /I "!stringToTest:%%g!" NEQ "%%G" (
Echo The string "%%G" contains "%%g"
) ELSE (
Echo The string "%%G" does not contain "%%g"
)
)
)
Goto :EOF
If all that were needed you could amend the above to make it work to have numbered arrays as it goes along I suppose.
Alternatively if you just want to address what is going on:
Delayed expansion isn't a complete replacement for calling a command or using a function.
This is one of the reasons Inused to only write scripts wirhout delayed expansion as even wirh it on mor complex work still requires calling a function to do the steps as part of the loop.
This is generally when doing lots of value man up such as here.
The alternative, which works as well is to create loops to do the next level of variable manipulation, which is faster? YMMV
Which is simpler - generally calling the sub function.
Which is easier to follow - YMMV.
Here is the sub function method:
#(echo off
setlocal enabledelayedexpansion
set /a "counter=0"
set "searchValues=abc,xyz"
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
)
CALL :Main
( Endlocal
Pause
Exit /B
)
:Main
FOR %%G in (%searchValues%) do (
set /a "counter+=1"
set "arraySearchVal[!counter!]=%%G"
)
FOR %%G in (
%TextWhichNeedToBeSearched%
) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR /l %%I in (1,1,%counter%) do (
Call set "searchValue=!arraySearchVal[%%I]!"
echo Searchword: !searchValue!
CALL :Fn_Compare
If !_CmpResult! == 0 (
Echo=the string did not exist.
Echo=Even in your loop you can use this result now.
) ELSE (
Echo=the string does exist.
Echo=Even in your loop you can use this result now.
)
)
echo/
)
Goto :EOF
:Fn_Compare
REM echo !stringToTest! found in Textline | findstr "!searchValue!"
IF /I "!stringToTest:%searchValue%=!" NEQ "!stringToTest!" (
echo Searchword is in Textline
Set "_CmpResult=1"
) ELSE (
Set "_CmpResult=0"
)
Goto :EOF
Is this not all you need?
#Echo Off
Set "TextWhichNeedToBeSearched=tzu,abc,qsd"
Set "SearchValues=abc,xyz"
Set "ResultString=Searchword is in Textline"
For %%# In (%SearchValues%)Do (
Echo Textline: %TextWhichNeedToBeSearched%
Echo Searchword: %%#
Echo("%TextWhichNeedToBeSearched%"|FindStr "\<%%#\>">NUL 2>&1&&(
Echo(%ResultString%)||Echo(%ResultString:is=is not%
Echo(
)
Pause

How can we split string using windows bat

How can we split string using windows bat script?
for below .bat code snippet
#echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
rem mkdir !a!
) || (
echo +)
rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause
below config file
Q
What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
How can I get the /path/to/case and value as a pair?
for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"
set j=0
for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
echo mkdir !a!
) || (
echo +)
for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q43407067.txt containing your data for my testing.
(These are setting that suit my system)
SO - to address your problems:
because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----
You can't delete that ) because it's participating in a parenthesis-pair.
You need a space between the in and the (.
I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)
In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.
Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.
I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:
#Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
Echo=MD %%A
Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
) Do (Set/A "i+=1"
Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF
remove Echo= on line 6 if you are happy with the output and really want to create those directories

How to get the name of calling bat script

I need to have my 2.bat script behaviour depending on the name of calling script.
Scenario:
2.bat is invoked from many other external scripts, which I am not entitled to change. Only 2.bat is under my thumb.
1.bat:
...
call 2.bat
2.bat:
...here place something extracting "1.bat"...
As you cant change the calling bat there will be almost impossible to get its name if it is triggered through the cmd console (may be a memory dump could help?) as then the ProcessId will hold information only for the cmd.exe. The command prompt history could give you some information but it would be unreliable (and requires a dump to a temporary file)
If the calling bat is double clicked you can use this:
setlocal enableDelayedExpansion
for /f "tokens=2* delims= " %%a in ("%cmdcmdline%") do (
if /i "%%~a" equ "/c" (
for %%# in (%%~b) do (
echo calling bat : %%~#
)
) else (
doskey /history >"%tmp%\cmd.history"
for /f "usebackq tokens=* delims=" %%# in ("%tmp%\cmd.history") do (
set "last_command=%%#"
)
echo probably this is the calling bat: !last_command!
del /q /f "%tmp%\cmd.history"
)
)
pause
You can get the name of a calling batch with a trick.
Assuming you have a first.bat(you can't controll) it could look like this
#echo off
set caller=empty
echo This is %~0
for /L %%n in (1 1 3) do (
echo(
echo #1 before calling, n=%%n
call second %%n
)
echo Back to %~0
And your second.bat detects the caller
#echo off
setlocal DisableDelayedExpansion
set "func=%~0"
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X"
if ":" == "%func:~0,1%" (
goto %func%
)
REM *** Get the name of the caller
(
(goto) 2>nul
setlocal DisableDelayedExpansion
call set "caller=%%~f0"
call set _caller=%%caller:*%%~f0=%%
if defined _caller (
set "callType=batch"
call "%~d0\:mainFunc\..%~pnx0" %*
) ELSE (
set "callType=cmd-line"
cmd /c "call "%~d0\:mainFunc\..%~pnx0" %*"
)
echo BACK
endlocal
)
echo NEVER REACHED
exit /b
:mainFunc
echo :mainFunc of %~nx0 arg1=%1 is called from '%caller%'/%callType%
exit /b
I've adjusted jikou's and jeb's code a little bit so it can also detect the caller function from the caller script.
detectCallerScript.bat:
#echo off
setlocal DisableDelayedExpansion
set "func=%~0"
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X"
if ":" == "%func:~0,1%" (
goto %func%
)
REM *** Get the name of the caller
(
(goto) 2>nul
setlocal DisableDelayedExpansion
call set "caller=%%~f0"
call set _caller=%%caller:*%%~f0=%%
if defined _caller (
set "callType=batch"
call "%~d0\:mainFunc\..%~pnx0" %%0 %*
) else (
set "callType=cmd-line"
cmd /c "call "%~d0\:mainFunc\..%~pnx0" %%0 %*"
)
endlocal
)
echo(NEVER REACHED
exit /b
:mainFunc
set "source=%~1"
shift /1
:mainFuncLoop
set args=%args%%1
if "%~2" neq "" shift /1&goto:mainFuncLoop
if defined args set "args=%args:~0,-1%"
echo(:mainFunc of %~nx0 source=%source% with args="%args%" is called from '%caller%'/%callType%
exit /b
scriptCaller.bat:
#echo off
set caller=empty
echo(%~0: call detectCallerScript hi there
call detectCallerScript hi there
echo(Back to %~0
echo(
call:someFunc
exit /b
:someFunc
set caller=empty
echo(%~n0 %~0: call detectCallerScript hi there
call detectCallerScript hi there
echo(Back to %~0
echo(
Output:
scriptCaller.bat: call detectCallerScript hi there
:mainFunc of detectCallerScript.bat source=scriptCaller.bat with args="hi there" is called from '{path}\scriptCaller.bat'/batch
Back to scriptCaller.bat
scriptCaller :someFunc: call detectCallerScript hi there
:mainFunc of detectCallerScript.bat source=:someFunc with args="hi there" is called from '{path}\scriptCaller.bat'/batch
Back to :someFunc

Batch file finding a filename which contains a substring

I want to write a batch file to find all .vsdm files and the file name must contain a substring "2.4". But my code is telling me that all my .vsdm files contains the substring "2.4" which is not correct.
FOR /R %completepath% %%G IN (*.vsdm) DO (
set file=%%~nG
If not "%file%"=="%file:2.4=%" (
echo Filename contains 2.4
) else (
echo Filename does NOT contains 2.4
)
)
Can anyone tell me where did I get it wrong?Thanks
If "%file%"=="%file:2.4=%" (
echo Filename "%file%" does NOT contain 2.4
) else (
echo Filename "%file%" contains 2.4
)
Including the filename in the echo may reveal more. I can see no reason for the double-negative approach. The way the code operates may depend on precisely where in code the instructions are located, for instance if these lines are contained within any variety of loop or code-block, operation may depend on other elements, so it's important to present the code in-context and with an example of what was expected and what actually happened.
correct fomatting makes all clear.
There are one or two SO articles about delayed expansion which OP should become familiar with.
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %completepath% %%G IN (*.vsdm) DO (
set "file=%%~nG"
If not "!file!"=="!file:2.4=!" (
echo Filename contains 2.4
) else (
echo Filename does NOT contains 2.4
)
)
ENDLOCAL
You can use the command Where /? that let you use wildcard characters ( ? * ) and UNC paths.
#echo off
Title Find the location of a file with substring by Hackoo
Color 0A
Call :inputbox "Enter the file name to search :" "Enter the file name to search"
If "%input%" == "" Color 0C & (
echo(
echo You must enter a filename to continue with this program
pause>nul & exit
) else (
Call :Browse4Folder "Select the source folder to scan %input%" "c:\scripts"
)
Set "ROOT=%Location%"
::We check whether the input string has an anti-Slach in the end or no ? if yes, we remove it !
IF %ROOT:~-1%==\ SET ROOT=%ROOT:~0,-1%
set whereCmd=where.exe /r %ROOT% %input%
for /f %%a in ('%whereCmd%') do echo %%~nxa --^> %%a
pause & exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
echo set shell=WScript.CreateObject("Shell.Application"^)
echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
echo if typename(f^)="Nothing" Then
echo wscript.echo "set Location=Dialog Cancelled"
echo WScript.Quit(1^)
echo end if
echo set fs=f.Items(^):set fi=fs.Item(^)
echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::***************************************************************************
:InputBox
set "input="
set "heading=%~2"
set "message=%~1"
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do (
set "input=%%a"
)
exit /b
::***************************************************************************

Xor a string in a bat file?

In short I want to give my friend a bat file which does the below (untested)
echo Your mother is so fat
pause
echo the recursive function computing her mass causes a stack overflow
I might copy/paste him the bat file so I don't want the punch line to be ruined. How can I hide the text? I was thinking I can store the string in a variable and before I echo it I should XOR each letter with 32. But I have no idea how to take a string, XOR each letter than echo it to display the joke. How might I hide the text? I could also BASE64 encode/decode it but IDK how to do that either if I am only using a bat file
1) Here's one way to hide the text using mshta as a command line tool (with ascii codes in this case) :
#echo off
mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(Chr(89) & Chr(111)& Chr(117) & Chr(114) & Chr(32) & Chr(109) & Chr(97) & Chr(109) & Chr(97) & Chr(32) ):Close")|more
2) You can use CERTUTIL to encode/decode base64/hex files but it requires a temporary file that can be silently deleted (more info ):
echo 796f7572206d616d6120697320736f20666174>"%temp%\fat.hex"
certutil -decodehex "%temp%\fat.hex" "%temp%\fat.txt" >nul 2>&1
type "%temp%\fat.txt"
del /q /f "%temp%\fat.txt"
3) Dbenham's hex print function
#echo off
setlocal
::Define a Linefeed variable
set LF=^
::above 2 blank lines are critical - do not remove.
::Create a string variable with encoded TABs
call :hexprint "0x790x6f0x750x720x200x6d0x610x6d0x610x200x690x730x200x730x6f0x200x660x610x74" var
echo %var%
exit /b
:hexPrint string [rtnVar]
for /f eol^=^%LF%%LF%^ delims^= %%A in (
'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"'
) do if "%~2" neq "" (set %~2=%%A) else echo(%%A
exit /b
4) carlos' genCar function that uses MAKECAB:
#echo off
setlocal
break>fat.txt
for %%# in (121 111 117 114 32 109 97 109 97 32 105 115 32 115 111 32 102 97 116) do (
call :genChar %%#
type %%#.chr>>fat.txt
del /q /f %%#.chr >nul 2>&1
)
type fat.txt
del /q /f fat.txt
goto :eof
:genChar
setlocal
set "USAGE=echo:Usage: Supply an integer 0-255& goto :EOF"
if "%~1" equ "" %USAGE%
set /a "val=%~1" 2>nul
if "%~1" neq "%val%" %USAGE%
if %~1 lss 0 %USAGE%
if %~1 gtr 255 %USAGE%
set tempfile=%~1.tmp
set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26 (type nul >"%tempfile%"
makecab %options% /d reserveperfoldersize=%~1 "%tempfile%" %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">"%tempfile%")
>nul copy /y "%tempfile%" /a %~1.chr /b
del "%tempfile%"
) else (copy /y nul + nul /a 26.chr /a >nul)
endlocal
for more cryptic script you can combine hem.Only MSHTA and MAKECAB solutions will work on every windows machine. FORFILES and CERTUTIL are default form Vista and above I think.It is possible to create a few more examples ...
This is a subject near and dear to my heart because I did something similar in my implementation of the classic Colossal Cave Adventure game as a Windows batch file.
Within the game script I selectively encrypt display text, variable names, and comments. The code to decode the encrypted text is embedded directly within the same script! I write the source code for the game normally, and use braces to denote what portion is to be encrypted. A function within the game is able to generated the encrypted form of itself!
I used a simple symmetric rotation cipher, so really it is more obfuscation than encryption. But that is all that is needed for both the game, and your situation.
I've extracted a simplified version of the routines and provide them below.
The first script is a standalone script that selectively encrypts text within a source file and writes the result to stdout. Simply redirect the output to a new file to get the encrypted version of the file.
selectiveROT13.bat
#echo off
:selectiveROT13 InFile
::
:: Selectively applies the simple "rotate alphabet 13 places" cipher
:: to the contents of file InFile. Only text between curly braces
:: is affected. The affected content can span multiple lines.
::
:: Writes the results to stdout.
:: Percent completion is continuously written to stderr.
::
setlocal enableDelayedExpansion
set "upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "lower=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
set /a "B=(%%A+13)%%26"
for /f %%B in ("!B!") do (
set "upper!upper:~%%A,1!=!upper:~%%B,1!"
set "lower!lower:~%%A,1!=!lower:~%%B,1!"
)
)
setlocal disableDelayedExpansion
>&2 cls
set "active="
for /f %%N in ('type %1^|find /c /v ""') do set /a "lnCnt=%%N, pct=-1"
for /f "skip=2 tokens=1,* delims=[]" %%a in ('find /v /n "" %1') do (
set "ln=%%b"
setlocal enableDelayedExpansion
set "str=A!ln!"
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!ln:~%%n,1!"
if "!c!" equ "{" set "active=1"
if "!c!" equ "}" set "active="
if defined active if defined upper!c! for /f %%c in ("!c!") do (
if "!upper:%%c=%%c!" equ "!upper!" (
set "c=!upper%%c!"
) else (
set "c=!lower%%c!"
)
)
set "rtn=!rtn!!c!"
)
echo(!rtn!
for %%A in ("!active!") do (
endlocal
set "active=%%~A"
)
)
exit /b 0
Below is your joke program with a simplified version of the code to decode encrypted text. My original code worked with string variables, but this version works with string literals. The source script is written normally, without encryption. Braces indicate which code is to be encrypted. Besides your joke, I've included documentation and examples to demonstrate some of the features.
joke_src.bat
#echo off
setlocal enableDelayedExpansion
call :init
:: Disable delayed expansion to protect ! within string literals
setlocal disableDelayedExpansion
:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::Line1
:::Line2
:::Line3
:: }
:: I defined a simple SHOW macro that expands to CALL :SHOW
:: Use the %show% macro to display encrypted text.
:: The braces can be hidden by using the undefined %{% & %}% variables
%show% %{%"Quote literals ("") must be doubled ("""") in the source"%}%
:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::
echo(
for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do %show% "%%A"
echo(
echo And now it is time for a little joke.
echo(
echo Your mother is so fat...
pause
%show% %{%"the recursive function computing her mass causes a stack overflow!"%}%
exit /b
:show Str
::{
:: Applies the simple "rotate alphabet 13 places" cipher to string Str
:: and writes the result to stdout. Consecutive quotes ("") are converted
:: into a single quote (").
::}
setlocal disableDelayedExpansion
set "str=%~1"
setlocal enableDelayedExpansion
set "str=!str:""="!^"
if defined {obfuscated} (
set "len=0"
set "str2=.!str!"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!str:~%%n,1!"
if defined {upper}!c! for /f %%c in ("!c!") do (
if "!{upper}:%%c=%%c!" equ "!{upper}!" (
set "c=!{upper}%%c!"
) else (
set "c=!{lower}%%c!"
)
)
set "rtn=!rtn!!c!"
)
) else set "rtn=!str!"
echo(!rtn!
exit /b 0
:init
set "}="
set "{="}
set "{upper}=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "{lower}=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
set /a "B=(%%A+13)%%26"
for /f %%B in ("!B!") do (
set "{upper}!{upper}:~%%A,1!=!{upper}:~%%B,1!"
set "{lower}!{lower}:~%%A,1!=!{lower}:~%%B,1!"
)
)
set "{obfuscated}="
set "{obfuscationTest}={A}"
if "!{obfuscationTest}:A=!" equ "!{obfuscationTest}!" set {obfuscated}=1
set "show=call :show"
exit /b
The following command will generate the encrypted version of the script:
selectiveROT13 joke_src.bat >joke.bat
Below is the encrypted form. This is what you would send to your friend. (Without the extra documentation and examples of course)
joke.bat
#echo off
setlocal enableDelayedExpansion
call :init
:: Disable delayed expansion to protect ! within string literals
setlocal disableDelayedExpansion
:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::Yvar1
:::Yvar2
:::Yvar3
:: }
:: I defined a simple SHOW macro that expands to CALL :SHOW
:: Use the %show% macro to display encrypted text.
:: The braces can be hidden by using the undefined %{% & %}% variables
%show% %{%"Dhbgr yvgrenyf ("") zhfg or qbhoyrq ("""") va gur fbhepr"%}%
:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::
echo(
for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do %show% "%%A"
echo(
echo And now it is time for a little joke.
echo(
echo Your mother is so fat...
pause
%show% %{%"gur erphefvir shapgvba pbzchgvat ure znff pnhfrf n fgnpx biresybj!"%}%
exit /b
:show Str
::{
:: Nccyvrf gur fvzcyr "ebgngr nycunorg 13 cynprf" pvcure gb fgevat Fge
:: naq jevgrf gur erfhyg gb fgqbhg. Pbafrphgvir dhbgrf ("") ner pbairegrq
:: vagb n fvatyr dhbgr (").
::}
setlocal disableDelayedExpansion
set "str=%~1"
setlocal enableDelayedExpansion
set "str=!str:""="!^"
if defined {boshfpngrq} (
set "len=0"
set "str2=.!str!"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!str:~%%n,1!"
if defined {hccre}!c! for /f %%c in ("!c!") do (
if "!{hccre}:%%c=%%c!" equ "!{hccre}!" (
set "c=!{hccre}%%c!"
) else (
set "c=!{ybjre}%%c!"
)
)
set "rtn=!rtn!!c!"
)
) else set "rtn=!str!"
echo(!rtn!
exit /b 0
:init
set "}="
set "{="}
set "{hccre}=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "{ybjre}=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
set /a "B=(%%A+13)%%26"
for /f %%B in ("!B!") do (
set "{hccre}!{hccre}:~%%A,1!=!{hccre}:~%%B,1!"
set "{ybjre}!{ybjre}:~%%A,1!=!{ybjre}:~%%B,1!"
)
)
set "{boshfpngrq}="
set "{boshfpngvbaGrfg}={N}"
if "!{boshfpngvbaGrfg}:A=!" equ "!{boshfpngvbaGrfg}!" set {boshfpngrq}=1
set "show=call :show"
exit /b
The beauty of this system is that both joke.bat and joke_src.bat generate the exact same output:
Quote literals (") must be doubled ("") in the source
Line1
Line2
Line3
And now it is time for a little joke.
Your mother is so fat...
Press any key to continue . . .
the recursive function computing her mass causes a stack overflow!
Another nice feature is that selectiveROT13.bat can be applied to joke.bat to regenerate the original un-encrypted source.
This code creates a base64 encoded file:
#echo off
set "var=the recursive function computing her mass causes a stack overflow"
>file.tmp echo %var%
certutil -f -encode file.tmp file.tmp2 >nul
echo file.tmp2|find /v "-----" >file.txt
del file.tmp?
pause
and you can use the file like so (adding echo at the start of each line of the encoded file):
#echo off
cls
echo Your mother is so fat
pause
(
echo dGhlIHJlY3Vyc2l2ZSBmdW5jdGlvbiBjb21wdXRpbmcgaGVyIG1hc3MgY2F1c2Vz
echo IGEgc3RhY2sgb3ZlcmZsb3cNCg==
)>file.tmp
certutil -f -decode file.tmp file.txt >nul
timeout /t 2 /nobreak >nul
type file.txt
timeout /t 5 /nobreak >nul
I think you can just replace some chars with others in pure BAT without any temporary files using following string replacing script.
%str:old_char=new_char%
For example, I have defined some encoding and decoding functions. The codes are attached here, and it will print what you want.
#echo off
set str1=Y urke ohtrkmsks kfao
set str2=ohtkrtcursmvtkfuncom nkc epuomngkhtrkeasskcaustskaksoacik vtrfl w
call :decode "%str1%"
call :decode "%str2%"
pause
goto :eof
:decode
set "str=%~1"
set str=%str: =#%
set str=%str:k= %
set str=%str:i=k%
set str=%str:m=i%
set str=%str:e=m%
set str=%str:t=e%
set str=%str:o=t%
set str=%str:#=o%
echo %str%
goto :eof
I also attached the encoding script below.
#echo off
set str1=Your mother is so fat
set str2=the recursive function computing her mass causes a stack overflow
call :encode "%str1%"
call :encode "%str2%"
pause
goto :eof
:encode
set "str=%~1"
set str=%str:o=#%
set str=%str:t=o%
set str=%str:e=t%
set str=%str:m=e%
set str=%str:i=m%
set str=%str:k=i%
set str=%str: =k%
set str=%str:#= %
echo %str%
goto :eof

Resources