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.
Related
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
Here is the situation. I need to "type" a .sj files content, then save it as an .js file. This is not the same as a rename, as the encoding is different. I am very new to Batch File syntax, but proficient with other programming languages. Here is what I have tried:
call for %%i in (.\*.js) do type %%i > %%i.js
But this is giving me the "%%i is unexpected at this time" Error.
If you need me to provide more insight, I will be happy to.
BACKGROUND: Trying to use JSDoc3 on .sj files but the encoding is not compatible. Using an encoder did not work either. What did work is copying and pasting the contents into a new file with encoding UTF8. But like I stated, a program like UTFCast did not work.
Two percent signs without anything in between (in a batch file) are treated like a single percent sign in a command (not a batch file).
Moreover CALL command enables a user to execute a batch file from within another batch file.
So from cmd you need to run only below..
for %i in (.\*.js) do type %i > %i.js
if you want to learn more...
for command
call command
I have a bunch of dynamically created *.BAT files. These BAT files are used to create folders in a server. Just one line in each BAT file, such as: MKDIR \NetworkShare\abc\123
This "abc\123" string is from a database.
It runs OK for a while to create thousands subfolders on demand until today it stopped creating a special subfolder which has a "close single quote" (Alt + 0146 if typing from dos prompt) in the string.
I did some research and found that this "close single quote" is an extended ASCII character. It can't be saved properly in ANSI BAT file (end up as something else). I tried UNICODE and UTF-8 BAT file, but it doesn't work.
The only near-close solution is that I tried a binary editor to make sure it's code 146, but code 146 gives me Æ (ALT-146) not "close single quote" (Alt + 0146).
I know I can manually type special characters in DOS prompt (by using keyboard Alt + ).
But is there a way to properly save this "close single quote" (Alt + 0146) in BAT file so I can execute them dynamically?
The host system is Windows Server 2003 US-English.
Thank you for this CHCP 65001 trick. It leads to proper solution:
I took follow steps to resolve the issue:
+++++++++++++++++++
Prepare the BAT Text File (either manually or dynamically)
+++++++++++++++++++
(1) Make the first line blank (this is necessary, because there are hidden chars in the first line for UTF-8 text file)
(2) Put CHCP 65001 as second line
(3) main line here: MKDIR \networkshare\abc(right single quote-->this is special extended ASCII char)\123
(4) make sure the BAT file saved as UTF-8
+++++++++++++++++++
Now it's the CMD.EXE trick
+++++++++++++++++++
(1) Start cmd.exe
(2) open cmd.exe black screen property
(3) make sure the black screen font is "true type" i.e. "TT" like. By default, it is raster font, can not handle special ascii code properly. (This is the key step)
(4) now I can run my BAT to handle those extended ASCII chars properly.
Try changing the code page of your batch file to UTF-8: Insert this line at the top of your batch file and save the file as UTF-8:
chcp 65001
Be careful though: Creating folders with non-ASCII letters can break some programs that may rely on older API of libraries, or just assume that all folder and file names are ASCII.
I have a Windows batch file which has an instruction to execute an EXE file in a location whose path contains accented characters. Following are the contents of the batch file.
#echo off
C:\español\jre\bin\java.exe -version
C:\español\jre\bin\java.exe - This path exists and is proper. I can run this command directly on cmd.exe. But when I run the command from a bat/cmd file it fails saying "The system cannot find the path specified"
One way to fix this is by setting code page to 1252 (that works for me). But I'm afraid we'd have to set code pages for any non-English locale and figuring out which code page to use is pretty difficult.
Is there an alternative approach to fix this problem? Maybe a command-line option or something else?
Another way of doing this, in Windows, is by using wordpad.exe:
Run wordpad.exe
Write your script as you usually do, with accents
Choose Save as > Other formats
Choose to save it as Text document MS-DOS (*.txt)
Change the file extension from .txt to .bat
I had the same problem, and this answer solved it. Basically you have to wrap your script with a bunch of commands to change your terminal codepage, and then to restore it.
#echo off
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul
:: your stuff here ::
chcp %cp%>nul
Worked like a charm!
I'm using Notepad++ and it has an option to change "character sets", OEM-US did the trick. ;)
Since you have #echo off you can't see what your batch is sending to the command prompt. Reproducing your problem with that off it seems like the ñ character gets misinterpreted since the output I see is:
C:\espa±ol\jre\bin\java -version
The system cannot find the path specified.
I was able to get it to work by echoing the command into the batch file from the command prompt, i.e.
echo C:\español\jre\bin\java.exe -version>>test.bat
This seems to translate the character into whatever the command prompt is looking for, though I've only tested it with English locale set so I don't know if it'll work in all situations for you. Also, if you open the batch in a text editor like notepad it looks wrong (C:\espa¤ol\jre\bin\java.exe)
Use Alt + 0164 for ¤ instead of Alt + 164 ñ in a batch file... It will look odd, but your script should run.
You can use Visual Studio Code and it will let you select the encoding you want to use. Lower right corner, you select the encoding and will display option "save with encoding". Select DOS and will save the accented chars.
I also had the same problem. I was trying to create a simple XCOPY batch file to copy a spreadsheet from one folder to another. Its name had the "é" character in it, and it refused to copy.
Even trying to use Katalin's and Metalcoder's suggestions didn't work on my neolithic Windows XP machine. Then I suddenly thought: Why not keep things as simple as possible (as I am myself extremely simple-minded when it comes to computers) and just substitute, in the batch file code, "é" with the wildcard character "?".
And guess what? It worked!
I am writing a batch script to go through some directories doing an specific task, something like the following:
set DBCreationScript=//Here I set the full path for the script
echo %DBCreationScript%
Problem is the path has got some latin characters (ç, ã, á) and when I run the script, the output shows strange characters, not the ones I typed in. The batch script is in ANSI encoding.
I already tried to set the script encoding to UTF-8, but apparently the batch interpreter can't handle the control characters that appear on the beggining of the file.
Any thoughts?
Save the batch file in OEM encoding (a decent editor should allow this) or change the code page prior to running it with
chcp 1252
You can also save it as UTF-8 without signature (BOM) and use
chcp 65001
but down that path lies peril and dragons await to eat you (in short: It's usually painful and has a few weird side-effects).