Alt+255 in a Batch File - batch-file

I'm sure you know of the special Alt+255 character which Windows renders as a blank character.
I am trying to use that Alt+255 character in a batch file, to create a file that contains this special character.
copy mybatch.txt "C:\Alt+255My Batch.bat" >nul
Result: Alt+255My Batch.bat
So I pasted the actual blank character into the batch file
copy mybatch.txt "C:\ My Batch.bat" >nul
Result: áMy Batch.bat
So I changed the batch file encoding from ANSI to UTF-8
Result:  My Batch.bat
Any ideas how I can refer to the blank character inside a batch file?

I suppose the hex/dec value of this symbol should be ff/255.
to create a file that contain this you can use certutil:
#echo ff>255.hex
#certutil -decodehex 255.hex 255.bin
or you can take a look at GenChr.bat or this or eventually this

Related

How do I pass the full file name, including spaces, from the windows registry as a parameter to a batch file?

What I'd like to do: Add an entry to a Windows 10 context menu for specific file types (e.g. .mp4) that allows me to search for the file name on a website (in my case, IMDB). I got the entry to show up fine, but file names cut off after any space character.
Question: How do I pass the full file name, including spaces, from the windows registry as a parameter to a batch file?
.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VLC.mp4\shell\Search IMDB\command]
#="\"C:\\test.bat\" \"%1\""
test.bat
SET a="%~n1"
SET z=https://www.imdb.com/search/title/?title=
SET zz=%z%%a%
start "" %zz%
For a file name like movie.mp4 this works. However, file name cool movie.mp4 will in this case open a search page for "cool" which I'm afraid does not help me.
Any help is greatly appreciated!
Replace the spaces with + signs.
According to https://ss64.com/nt/syntax-replace.html that should be something like
SET zzz=%zz: =+%
The batch file should be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Name=%~n1"
setlocal EnableDelayedExpansion
set "Name=!Name:%%=%%25!"
set "Name=!Name: =%%20!"
start "" "https://www.imdb.com/search/title/?title=!Name!"
endlocal
endlocal
First, read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains in detail what is the difference between set variable="value" and set "variable=value". The latter is the recommended syntax.
Second, a URL must be percent-encoded. A normal space character must be encoded with %20 in a URL. The batch file above does that. It percent-encodes first also % in name by %25.
Note: The entire URL is not correct percent-encoded if the string assigned to the environment variable Name contains other characters than and % which need to be percent-encoded for a valid URL too.
Third, the character % must be escaped in a batch file with one more % to be interpreted as literal character even within a double quoted argument string.

Copying csv in batch file causes file to be corrupted

I use the following command to copy a csv (with the date in the name) to a directory called "read":
copy "C:\Users\Brock\Documents\Dropbox\dir\test\file????????.csv" "C:\Users\Brock\Documents\Dropbox\dir\test\file\read\file.csv"
When it is copied over there is some sort of invalid character (looks sort of like "->") that is causing Salesforce DataLoader to be unable to read the file.
Why is my file being corrupted and how can I prevent this from happening?
copy "C:\Users\Brock\Documents\Dropbox\dir\test\file.csv" "C:\Users\Brock\Documents\Dropbox\dir\test\file\read\file.csv" /a /v
Try This...
The CMD environment is detecting that the file is an ASCII file and is adding the ASCII EOF character (→) [0x26d, 0x1Ah] to the end of the newly copied file.
If you add the /b switch to the copy command, it will copy the file using binary mode and won't add the ASCII EOF character to the end of the copied file.
copy /b "C:\Users\Brock\Documents\Dropbox\dir\test\file????????.csv" "C:\Users\Brock\Documents\Dropbox\dir\test\file\read\file.csv"

For statement don't works in batch file

for %%i in (foo bar) do #move %%i dir
This is the content of the batch file. It have to move the file to dir. But it returns error,
C:\>for %i in (foo bar) do #move %i dir
'for' is not recognized as an internal or external command, operable program or batch file.
What it the character  ? I checked with notepad++ and I don't find any hidden char in that batch file.
Most likely you have UTF-8 encoding set in notepad++
You will need to change it to UTF-8 without BOM or ANSI in order to work with cmd (batch)
Strange characters you see at the beginning is BOM - byte order mark code used by UTF
The only thing that comes to mind is encoding. Make sure it's saved in format that cmd will understand (got similar issues when my .cmd file was saved as Unicode Big Endian. Could reproduce your error by saving file as UTF-8. Tried to use ANSI instead.
HTH
Bartek
It looks like you have some hidden characters in it. Try retyping it into a new file. The  definitely shouldn't be there.

Dos Batches: write to files without a line ending

I have a situation while writing a dos batchs script. A tool I am using to calculate CRC (checksum) of a text string requires the text to be in a file. The data I am trying to get the CRC for is a filename, but when using a batch to put this filename into a text file to calculate CRC, the batch script naturally puts the line ending (CR/LF) and a blank line at the end. As this causes the CRC to be wrong, it is a problem.
Is there any way to get a batch script to write to a text file without appending a line ending? IE to output a single line unfinished to file?
-K.Barad
<nul set /p ".=text" > file
It's faster and safer than echo.|set /P ="text" > file
The nul redirection is faster than a pipe with echo. (btw echo.can fail).
The style of the quotes allowes to output also quotes.
But there are always restrictions!
Vista and Win7 have a "feature" to supress leading spaces, Tabs and CR's.
Xp can output text with leading spaces and so.
And it's not possible to begin the output text with an equal sign (results in a syntax error)
You could
echo.|set /P ="text" > file
source
Or directly pipe the text to the command line:
echo "text" | checksum_program.exe
edit:
if you're using CRC32DOS, then you can use its command line option -c to ignore CRs.

Read command-line parameters to .bat from file

I have a build.bat file which uses %1 internally... so you might call:
build 1.23
I wanted it to read the parameter from a separate file, so I tried putting "1.23" in version.txt and doing:
build < version.txt
But it doesn't work. Isn't this how piping works? Is what I want possible and if so how?
The FOR command in DOS has a form which parses files and assigns the tokens it finds to variables, which can then be used as arguments to other batch files. Assuming version.txt contains the single line "1.23", this batch file will open it, assign 1.23 to the variable, then call your original batch file, passing the variable's value as a command-line argument.
#echo off
for /f %%i in (version.txt) do call build.bat %%i
If version.txt contains more than one line, build.bat is called once for each line. Type help for at a DOS prompt to see what other features you might be able to use.
I think it would make more sense if you handle the file processing within the batch file on the off chance that version.txt is not edited correctly.
You should modify your script to parse the file to get the version if the .bat file is executed as:
build FILE version.txt

Resources