Batch File:: Write Special characters to text file - windows

I'm trying to print special characters to a text file.
Those are my problematic lines :
(
echo FOR /L %%A IN (1,1,3) DO (
echo echo "%~f0" myText
echo )
) > myFile.bat
I tried to escape the special characters with ^ and with \ but seems like it's not working properly
I tried :
1.
(echo FOR \/L \%\%A IN (1,1,3) DO \() >> myFile.bat
2.
(echo FOR ^/L ^%^%A IN (1,1,3) DO ^() >> myFile.bat
3.
(echo echo \"%~f0\" myText) >> myFile.bat
4.
(echo echo ^"%~f0\^" myText) >> myFile.bat
None of them is working properly.
I was wondering if there is a command like in php
$myText = <<<EOF
EOF;
That would magically do the task but if not ...

This is all you need to do:
(
echo FOR /L %%%%A IN (1,1,3^) DO (
echo echo "%%~f0" myText
echo ^)
)>myFile.cmd
Escape the intenal closing parentheses with the standard circumflex character, escape each percent with another.

Related

CMD ... IF !first_char! == "." ( echo ...) ELSE () (IF test character comparaison do not work !)

#echo off
cls
setlocal EnableDelayedExpansion
echo
for %%f in (*.lnk) do (
set x=%%f
echo !x!
set first_char= "!x:~0,1!"
echo !first_char!
IF !first_char! == "." (
echo "first char is a dot"
) ELSE (
echo "first char is NOT a dot"
)
)
PAUSE
My files start with a dot and this windows cmd .bat programme echo allway "first char is NOT a dot"
so there is a problem surely with the IF interpretation.
Can someone figure it out ?
Ok i understand now !
the correct way is :
rem no space after equal and no need double quote
set first_char=!x:~0,1!
rem and this IF will work perfect
IF !first_char! == . (
Fine thanks for your help.

Batch echo command to file doesn't escape

I am writing a script to automate backup setup on new servers. I am not the batch guru, so it is kept rather simple (See here in complete for reference http://pastebin.com/D1zEP8dj). Everything works to the bit where I try to write another batch script to a file:
(
echo del "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before\*.*" /S /Q
echo.
echo move /Y "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Full\*.*" "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backup.log "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backupdiff.log "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
echo.
echo.
echo C:\Scripts\snapshot.exe C: "%BACKUP_SERVER%Snapshot-$computername\Full\$disk-Partition-$type.sna" --LogFile:C:\Scripts\Backup.log -L50000 -GX --usevss --AllWriters
echo #echo off
echo if %%errorlevel%% NEQ 0 (
echo echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%% ^| C:\Scripts\blat\blat.exe -server %BLAT_MAILSERVER% -to info#abc.com -f backup#%BLAT_DOMAIN% -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backup.log
echo )
) >full-backup.bat
if %DO_DIFF_BACKUP%==y (
(
echo C:\Scripts\snapshot.exe C: %BACKUP_SERVER%\Snapshot-$computername\Diff\$disk-Partition-$type-$weekday.sna -h%BACKUP_SERVER%\Snapshot-$computername\Full\c-Partition-ful.hsh --LogFile:C:\Scripts\Backupdiff.log -L50000 -GX --usevss --AllWriters
echo #echo off
echo if %%%errorlevel%%% NEQ 0 (
echo echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%% ^| C:\Scripts\blat\blat.exe -server %BLAT_MAILSERVER% -to info#abc.com -f backup#%BLAT_DOMAIN% -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backupdiff.log
echo )
) >diff-backup.bat
)
So when executing the script, it stops on the last line of this excerpt. The file full-backup.bat doesn't exist at all, but won't throw an error at all, while the file diff-backup.bat is written with the following content:
C:\Scripts\snapshot.exe C: c:\test\Snapshot-$computername\Diff\$disk-Partition-$type-$weekday.sna -hc:\test\Snapshot-$computername\Full\c-Partition-ful.hsh -- LogFile:C:\Scripts\Backupdiff.log -L50000 -GX --usevss --AllWriters
#echo off
if %0% NEQ 0 (
The internet says, to escape percents double them, to escape pipes ^|-them, etc. But it does not seem to change anything if I quote things, use escape signs at all - batch won't listen to me :(. Any help would be greatly appreciated.
When you double the percent signs where you want to echo literally % and escaping all other special characters with ^ it should work.
As you can't know if your variables contains also special characters, it would be the best to use delayed expansion here instead of percent expansion.
This works as all content is safe when expanded by delayed expansion
setlocal EnabledDelayedExpansion
(
echo del "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before\*.*" /S /Q
echo.
echo move /Y "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Full\*.*" "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backup.log "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backupdiff.log "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
echo.
echo.
echo C:\Scripts\snapshot.exe C: "!BACKUP_SERVER!Snapshot-$computername\Full\$disk-Partition-$type.sna" --LogFile:C:\Scripts\Backup.log -L50000 -GX --usevss --AllWriters
echo #echo off
echo if %%errorlevel%% NEQ 0 (
echo echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%% ^| C:\Scripts\blat\blat.exe -server !BLAT_MAILSERVER! -to info#abc.com -f backup#!BLAT_DOMAIN! -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backup.log
echo ^)
)

Why this code says echo is off?

What is wrong with this code? It says ECHO is off.
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo %var2%
set var1=%var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del result
pause
Any help is appreciated.
If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo.
To avoid this, you should replace all your echo commands with something like this:
echo var2: %var2%
That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".
As Laurent stated, it's not a problem of the ECHO, it's a problem of your code.
In batch files, blocks are completely parsed before they are executed.
While parsing, all percent expansion will be done, so it seems that your variables can't be changed inside a block.
But for this exists the delayed expansion, the delayed expansion will be evaluated in the moment of execution not while parsing the block.
It must be enabled, as per default the delayed expansion is disabled.
#ECHO off
setlocal EnableDelayedExpansion
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo(!var2!
set var1=!var2:~5,3!
echo(!var1! > test.txt
echo(!var1!
) else (
echo error
)
del result
I used here the construct echo( instead of echo as this will ensure echoing an empty line even if the variable is empty.
Not sure, if this post is still read, but nevertheless.
You should try the following:
On top of the code right after #echo off you have to put in
setlocal enabledelayedexpansion
Additionally anywhere you want to use variables changed in a block of brackets (like For-Loops or If's) you have to change the %into ! to get
!varname!
This should be helping...
Greetings
geisterfurz007
First create a file a.txt in the same directory u have this batch file ... write some text in that...Note: only Windows 2000
Windows ME
Windows XP
Windows Vista
Windows 7 supports FINDSTR
set /p pattern=Enter id:
findstr %pattern% a.txt > __query.tmp
set /p result=<__query.tmp
if %errorlevel%==0 (
set var2= %result%
echo %var2%
set var1= %var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del __query.tmp
pause
run this bath file .. you will find a substring(start=5,length=3) of the first line of string you have in a.txt in a newly created file test.txt. Finally got it working !
The solution for your problem is to put the "echo"s after the if block is completed.
Try this:
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
set var1=%var2:~5,3%
goto print
) else (
echo error
goto result
)
:print
echo %var2%
echo %var1% > test.txt
echo %var1%
:result
del result
pause
This way you can see the solution as you wanted.
Cheers! ;]

Echo of String with Double Quotes to Output file using Windows Batch

I'm attempting to rewrite a configuration file using a Windows Batch file.
I'm looping through the lines of the file and looking for the line that I want to replace with a specified new line.
I have a 'function' that writes the line to the file
:AddText %1 %2
set Text=%~1%
set NewLine=%~2%
echo "%Text%" | findstr /C:"%markerstr%" 1>nul
if errorlevel 1 (
if not "%Text%" == "" (
setlocal EnableDelayedExpansion
(
echo !Text!
) >> outfile.txt
) else (
echo. >> outfile.txt
)
) else (
set NewLine=%NewLine"=%
setlocal EnableDelayedExpansion
(
echo !NewLine!
) >> outfile.txt
)
exit /b
The problem is when %Text% is a string with embedded double quotes.
Then it fails. Possibly there are other characters that would cause it to fail too.
How can I get this to be able to work with all text found in the configuration file?
Try replacing all " in Text with ^".
^ is escape character so the the " will be treated as regular character
you can try the following:
:AddText %1 %2
set _Text=%~1%
set Text=%_Text:"=^^^"%
... rest of your code
REM for example if %1 is "blah"blah"blah"
REM _Text will be blah"blah"blah
REM Text will be blah^"blah^"blah
Other characters that could cause you errors (you can solve it with the above solution) are:
\ & | > < ^
In a windows batch shell (command) double quote are escape with ^ on standard command line BUT with a double double quote inside a double quoted string
echo Hello ^"Boy^"
echo "Hello ""Boy"""
(remark: second line will produce the external surrounding double quote in the output also)

Remove first and last character from a String in a Windows Batch file

I have the following string inside my Windows batch file:
"-String"
The string also contains the twoe quotation marks at the beginning and at the end of the string, so as it is written above.
I want to strip the first and last characters so that I get the following string:
-String
I tried this:
set currentParameter="-String"
echo %currentParameter:~1,-1%
This prints out the string as it should be:
-String
But when I try to store the edited string like this, it fails:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%
Nothing gets printed out. What do I do wrong?
This really is strange. When I remove the characters like this it works:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%
it prints out:
-String
But actually my batch is a bit more complicated and there it does not work. I will show what I programmed:
#echo off
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=%currentParameter:~1,-1%
echo String WITH stripping characters: %currentParameter%
echo .
)
exit /b
:end
And the output is:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters:
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: ~1,-1
.
But what i want is:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters: -String
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: -String2
.
Hope this will help you.
setlocal enabledelayedexpansion
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter!
You are modyfing a variable inside a parenthesized block. Watch out - the new value will not be used within the same block (unless you delimit the variable with ! instead of % - and running in the enabledelayedexpansion mode).
Or just extract the couple of lines into another sub-function, using a plain sequence of lines insted of ( )
greets, Stach
This script takes advantage of ENABLEDELAYEDEXPANSION.
If you don't know, batch scripts execute for and if commands all in one; hence if you do:
if true==true (
#echo off
set testvalue=123
echo %testvalue%
pause >NUL
)
You wont output anything, because when echo %testvalue% is executed, it has not recognized the testvalue has been changed.
Using delayedexapnsion allows the script to read that value as it is now, and forget the problem I stated before. You use it just like %testvalue%, but you may do !testvalue! to fix this:
if true==true (
#echo off
set testvalue=123
echo !testvalue!
pause >NUL
)
Would echo 123.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter!
echo .
)
exit /b
:end
~ Alex
I had similar problem but it solved by removing spaces between
Ex : set FileName=%Name:~0,11% # Working as No space before and after '='
Ex : set FileName = %Name:~0,11% # Not Working as space before OR after '='
So please try by remove spaces, It should work
Note: command line should be reopen to get refresh the background values else it shows the same output as it is stored in temp

Resources