So, I'm trying to create a custom format image viewer in batch file witch reads from this file named image.ansii2 :
lines=18
line1=[15]---------------
line2=[15]----[160]------[15]-----
line3=[15]---[160]----------[15]--
line4=[15]---[94]---[223]---[0]-[223]-[15]----
line5=[15]--[94]-[223]-[94]-[223]----[0]-[223]---[15]--
line6=[15]--[94]-[223]-[94]--[223]----[0]-[223]---[15]-
line7=[15]--[94]--[223]-----[0]----[15]--
line8=[15]----[223]--------[15]---
line9=[15]---[160]--[26]-[160]--[26]-[160]-[15]-----
line10=[15]--[160]---[26]-[160]--[26]-[160]---[15]---
line11=[15]-[160]----[26]----[160]----[15]--
line12=[15]-[223]--[160]-[26]-[220]-[26]--[220]-[26]-[160]-[223]--[15]--
line13=[15]-[223]---[26]------[223]---[15]--
line14=[15]-[223]--[26]--------[223]--[15]--
line15=[15]---[26]---[15]--[26]---[15]----
line16=[15]--[94]---[15]----[94]---[15]---
line17=[15]-[94]----[15]----[94]----[15]--
line18=[15]---------------
and the program (ansii2.bat) looks like this :
#echo off
for %%a in (%1) do (set ext=%%~xa)
if "%1" == "" (echo No file was specified&pause&exit /b)
if not "%ext%" == ".ansii2" (echo The file specified didn't have the expected extension [%ext%] -^> [.ansii2]&pause&exit /b)
title Ansii2 %1
:0
echo [0m
cls
for /f "delims== tokens=1,2" %%G in (%1) do set "%%G=%%H"
set loop=0
:loop
set /a loop+=1
set line=line%loop%
set "image=echo %!line!:[=[48;5;%"
set "image=%image:]=m%"
set "image=%image:(=[38;5;%"
set "image=%image:)=m%"
set "image=%image:-= %"
%image%
echo %line%
if %loop% == %lines% (goto exitloop)
goto :loop
:exitloop
timeout /t -1 >nul
goto 0
I think that the bug comes from the line 14 but I don't know what to do to fix it...
Could someone help me?
Your attempt at substitution is off, and the method by which you read the file can be improved upon:
All your variables are formatted in the source file, so a for loop can be used to read and assign them by splitting the string at the = delimiter
Delayed expansion is used to perform the substitution, using:
!varname:search=replace! ; where varname is referenced using the meavariable of the for loops first token.
#Echo off & CD "%~dp0"
cls
Setlocal EnableExtensions EnableDelayedExpansion
For /F %%e in ('Echo prompt $E^|cmd')Do Set "\E=%%e"
(For /f "usebackq Tokens=1,2 delims==" %%G in ("%~$Path:1")Do (
Set "%%G=%%H"
Set "%%G=!%%G:[=%\E%[48;5;!"
Set "%%G=!%%G:(=%\E%[38;5;!"
Set "%%G=!%%G:]=m!"
Set "%%G=!%%G:)=m!"
Set "%%G=!%%G:-= !"
If not "%%G" == "lines" <nul set /p "=!%%G!%\E%[0m%\E%[E"
)) > Con
Endlocal
Other notes:
In the event the supplied file cannot be found the following will be output:
The system cannot find the file .
<nul set /p "=!variable!" ; allows safe output of poison characters
%\E%[E ; Equivalent to Outputting a linefeed.
Data structure for your source file:
using single characters for substitution prevents them from being used as characters in the ascii art. use paired characters or unique strings to prevent this. Ie :
\i=%\E%[48;5;
\e=%\E%[38;5;
\m=m
Related
I have an XML file (generated by a third party tool) which is of format
<?xml version="1.0" encoding="UTF-8"?><ROOT test_count="22" test_fail_count="1" test_pass_count="21".......</ROOT>
All the content is in one line of the file with name Report.xml.
I have been trying to write a batch script
#echo off
setlocal EnableDelayedExpansion
set "Report="
for /f "delims=" %%x in (Report.xml) do set "Report=!Report! %%x"
REm set /p Report=< %Results_File%
echo report is !Report!
call:parseVal !Report!
exit/b
:parseVal
setlocal enableDelayedExpansion
set val="%~1"
echo %val%
echo !val!|findstr /rc:"test_count=.[0-9]*." >nul || (
echo !val!
EndLocal
exit /b
)
rem ..
Basically I am trying to grab values for test_count(22), test_fail_count(1) and test_pass_count(21) from the XML file.
Any ideas on how to achieve this?
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q42057260.txt"
SET "testcount="
SET "testfailcount="
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
SET "xmlline=%%a"
CALL :process
)
ECHO test count=%testcount% test fail count=%testfailcount%
GOTO :EOF
:process
:: dispose of awkward characters
SET "xmlline=%xmlline:?= %"
SET "xmlline=%xmlline:>= %"
SET "xmlline=%xmlline:<= %"
CALL :select %xmlline%
GOTO :EOF
:select
IF /i "%~1"=="" GOTO :EOF
IF DEFINED testcount IF DEFINED testfailcount GOTO :EOF
IF /i "%~1"=="test_count" SET /a testcount=%~2
IF /i "%~1"=="test_fail_count" SET /a testfailcount=%~2
SHIFT
GOTO select
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q42057260.txt containing your data for my testing.
Read the file to %%a and transfer to xmlline.
Use :process to remove awkward characters (those with a special meaning to cmd) by replacing each with a space, then pass the resultant line to :select as a parameter-list.
look at each parameter in turn, stripping quotes. When the target strings appear, set their storage variables to the following value. When both values are assigned, bail out.
and report results.
Provided the structure and order of Report.xml is constant this could do (cmd line)
for /f "tokens=5,6 delims=<> " %A in (Report.xml) Do #Set %A&Set %B
Sample output with your Report.xml
> set test
test_count="22"
test_fail_count="1"
I am trying to create a batch file that will remove some special characters from a .txt file and append it to another file but the variable is unable to hold the full 1 million++ words in the text file.
Is there any way to make the variable hold all the words or at least split it into groups/sets?
The text look like this
{"One:1","two:2","three:4","four:3","five:5","EG:[512]","sets:{559,212,333,940}"};{"One:9","two:3","three:2","four:1","five:6","EG:[513]","sets:{551,215,331,944}"};...
So far I have tried using a FOR loop to append it to many different files then go through each files later using goto but it just doesn't work like it should.
edited - Previous answer at the bottom. As jeb comments this is faster, not sure if it will even be usable but faster (but, of course, being faster than the previous code is an easy task)
#echo off
setlocal enableextensions disabledelayedexpansion
<"in.txt" >"out.txt" call :process
goto :Eof
:process
set /p "buffer=" || goto :eof
set "buffer=%buffer:"=%"
set "buffer=%buffer:,= %"
set "buffer=%buffer:{=%"
set "buffer=%buffer:}=%"
set "buffer=%buffer:[=%"
set "buffer=%buffer:]=%"
set "buffer=%buffer:;=%"
set "buffer=%buffer::=%"
set "buffer=%buffer:0=%"
set "buffer=%buffer:1=%"
set "buffer=%buffer:2=%"
set "buffer=%buffer:3=%"
set "buffer=%buffer:4=%"
set "buffer=%buffer:5=%"
set "buffer=%buffer:6=%"
set "buffer=%buffer:7=%"
set "buffer=%buffer:8=%"
set "buffer=%buffer:9=%"
<nul set /p "=%buffer%"
goto :process
WARNING : This should not have ever been written. IT IS PAINFULLY SLOW.
Once it has been said,
#echo off
setlocal enableextensions disabledelayedexpansion
rem File configuration
set "inputFile=data.txt"
set "outputFile=out.txt"
rem Variable to hold a cariage return used to show that the
rem script is still working
set "CR="
for /f %%c in ('copy /Z "%~f0" nul') do if not defined "CR" set "CR=%%c"
rem A temporary file will be used.
for %%t in ("%temp%\%~nx0.%random%%random%.tmp") do (
echo Splitting input file into temporary file
> "%%~ft" (
( %= Split the input file into one character per line =%
cmd /u /q /c"type ""%inputFile%""" | cmd /a /q /c"find /v "" "
%= Ensure we have a terminator to empty buffer (keep reading...) =%
<nul set /p"=;"
)|( %= Remove non needed characters =%
findstr /i /r /c:"^[a-z,;]"
)
)
echo Splitting done
echo Starting to read temporary file
echo %time% Here we go ...
set "buffer=#"
rem All data to stdout will be placed into the output file
> "%outputFile%" (
for /f usebackq^ delims^=^ eol^= %%a in ("%%~ft") do (
rem Concatenate one character to the buffer
setlocal enabledelayedexpansion
for /f delims^=^ eol^= %%b in ("!buffer!") do (
endlocal
set "buffer=%%b%%a"
)
rem We will use the semicolon as a delimiter to do
rem partial processing of the input data
if "%%a"==";" (
rem Execute the required processing on the buffer
call :processSection buffer
setlocal enabledelayedexpansion
< nul (
rem Write the processed buffer to stdout
set /p "=!buffer!"
rem Show we are still working
>con set /p "=!time! ... still working ... !CR!"
)
endlocal
set "buffer=#"
)
)
)
rem Processed temporary file can be removed
) & del "%%~ft"
echo(
echo %time% ... Done
goto :eof
:processSection varName
setlocal enableextensions disabledelayedexpansion
rem Retrieve the data from the indicated variable
setlocal enabledelayedexpansion
for /f delims^=^ eol^= %%a in ("!%~1!") do (
endlocal
set "line=%%a"
)
rem The passed buffer includes an initial filler character
set "line=%line:~1%"
rem Process buffer
set "line=%line:,= %"
set "line=%line:;= %"
set "line=%line: = %"
rem Return buffer to the caller
endlocal & set "%~1=%line%"
goto :eof
It was written just for testing but, please, use any other thing.
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
I'm trying to read a file and output the lines of data into registry keys. The data collection works, but I don't understand the syntax required to increment the string values in the last loop.
#echo OFF
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq skip=1 delims=" %%a in (`"findstr /n ^^ C:\GetSID.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!" This removes the prefix
echo(!var:~76,63!>>C:\SIDoutput.txt
goto :EndLoop
)
:EndLoop
set /p SID= <C:\users\paintic\SIDoutput.txt
set KEY_NAME="HKEY_USERS\!SID!\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
set Counter=1
for /f %%x in (C:\users\paintic\Networkprinters.txt) do (
set "Line_!Counter!=%%x"
set /a Counter+=1
if !Counter!==3 (Echo %line_counter%)
)
set /a counter2=!counter!-3
set counter=1
The part below is what I can't get to work. I'm trying to write LINE_1, LINE_2 and LINE_3 values from the previous loop to increment via the loop below. So VALUENAME should equal LINE_1, TYPE should = LINE_2's value and DATA should = LINE_3 on the first run and keep going up by 1 until the loop finishes (end of the file read)
`for /L %%i in (1,1,%counter2%) do (
set ValueName=%Line_!counter!%
set /a counter+=1
set Type=%Line_!counter!%
set /a Counter+=1
set Data=%Line_!counter!%
set /a Counter+=1
echo !ValueName!
echo !Type!
echo !Data!
REG ADD %KEY_NAME% /v !ValueName! /t !Type! /d !Data! /f
)
ENDLOCAL
Pause`
On searching for errors in batch file it is always helpful to use in first line #echo on or remove #echo off or comment this line with rem to see what cmd.exe really executes.
Command line interpreter fails on lines with set VariableName=%Line_!counter!% as the interpreter does not know what to expand first. I think it is not possible to create dynamically the name of an environment variable and reference next the value of this environment variable. This approach most likely does not work ever.
However, what you want to achieve can be done much easier directly in second loop as the following example demonstrates:
#echo off
setlocal EnableDelayedExpansion
rem Create data for demo example.
set "KEY_NAME=HKEY_USERS\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
echo TestValue>"%TEMP%\Networkprinters.txt"
echo REG_SZ>>"%TEMP%\Networkprinters.txt"
echo Sample Data>>"%TEMP%\Networkprinters.txt"
echo AnotherValue>>"%TEMP%\Networkprinters.txt"
echo REG_DWORD>>"%TEMP%\Networkprinters.txt"
echo ^1>>"%TEMP%\Networkprinters.txt"
rem Now the loop follows which reads the data from the file line
rem by line and build the line for using command "reg.exe" to
rem add the data to registry of the user with the defined SID.
set Counter=1
for /f "usebackq delims=" %%x in ("%TEMP%\Networkprinters.txt") do (
if "!Counter!"=="1" (
set "ValueName=%%x"
) else if "!Counter!"=="2" (
set "ValueType=%%x"
) else (
set "ValueData=%%x"
rem Echo the command instead of really executing "reg.exe".
echo reg.exe ADD %KEY_NAME% /v "!ValueName!" /t !ValueType! /d "!ValueData!" /f
set Counter=0
)
set /a Counter+=1
)
rem Delete the text file created for demo example.
del "%TEMP%\Networkprinters.txt"
endlocal
This solution is much easier than what you have tried and can be maybe even more simplified.
I've got a problem with redirecting input from a file into set/p
Here is my script:
#echo off
echo Reading: {%1}
type %1
echo(
echo Starting...
set VAR=
set /P VAR=<%1
echo VAR is {%VAR%}...
I've read elsewhere (https://stackoverflow.com/a/7827243) that the syntax I am using will work. It does not!
Here is my output:
Reading: {Fruit.txt}
Pears
Apples
Oranges
Kiwi
Grapes
Kumquat
Starting...
VAR is { ■P}...
So - What gives?
Your file is in Unicode (UTF16) format, and SET /P does not work with Unicode. The TYPE command does work with Unicode, and it converts the output to ANSI. You can redirect the output of TYPE to a new text file, and then your SET /P will work.
#echo off
type %1>new.txt
set VAR=
set /P VAR=<new.txt
echo VAR is {%VAR%}...
EDIT
To get the second line instead of the first:
#echo off
type %1>new.txt
<new.txt (
set /P VAR=
set VAR=
set /P VAR=
)
echo VAR is {%VAR%}...
If the purpose is to read lines of text from a file, why do you need the set /p command?
for /f "delims=" %%a in ('type "file name.txt"') do echo %%a
If you type the command at the cmd.exe command line, you would write %a (one % symbol) rather than %%a (i.e., double the % symbol when using in a shell script [batch file]).
Bill
Here's another technique for reading only the first line of a file using for /f and type that doesn't rely on set /p:
#echo off
setlocal enableextensions enabledelayedexpansion
set VAR=
set CURRLINE=0
for /f "delims=" %%a in ('type "test.txt"') do (
set /a CURRLINE+=1
if !CURRLINE! EQU 1 (
set VAR=%%a
goto :DONE
)
)
:DONE
echo %VAR%
endlocal
The advantage here is there's no need to write out a separate file.
However, note that either of these approaches (set /p or for /f with type) will have problems with special shell characters in the input file (<, >, |, etc.). To get around this problem, one could use a small utility I wrote a while back called shellesc.exe (http://www.westmesatech.com/sst.html) to "escape" the special characters. But if you use these tools, then you can also use linex.exe to pick the line you want and get the result with a little bit less code:
#echo off
setlocal enableextensions
set VAR=
for /f "delims=" %%a in ('type "test.txt" ^| linex -l 1 ^| shellesc') do set VAR=%%a
echo %VAR%
endlocal
Note that this approach has the additional advantage of not "choking" on special shell characters (shellesc).
HTH,
Bill