Batch chinese(?) symbols change in cmd - batch-file

Related to this problem: Error code 259 when trying to pair using Bluetooth Command Line Tools
I made a simple batch that pairs my bluetooth gamepad with my PC, but unfortunately the gamepads screen name is in chinese(?) and when I run the batch the symbols are different in the cmd than in the batch.
I'm using Bluetooth Command Line Tools
Here's the batch:
rem #echo off
btpair -p -n"小米蓝牙手柄"
if errorlevel 1 goto error
exit
:error
pause
exit
...and here's what it looks like in the cmd window:
C:\Windows\system32>rem #echo off
C:\Windows\system32>btpair -p -n"Õ░Åþ▒│ÞôØþëÖµë﵃ä"
Remote device "ıª┼■ªªÌ¶Ï■ÙÍÁÙ´Áâõ" not found.
C:\Windows\system32>if errorlevel 1 goto error
C:\Windows\system32>pause
Press any key to continue . . .
I tested it manually (copy-pasted the name to cmd) and it worked, but not from batch.
As you can see I have a problem here...
Ideas?

You may be able to use the command line tool devcon.exe from Microsoft to disable and re-enable just that piece of hardware - it should re-pair the device as a side effect I am guessing.
Changing the codepage with the chcp command in the batch file may help with the chinese characters.

You must change your system locale to have chinese character input/output.
Control Panel -> Region -> Administrative -> Change system locale... -> Current system locale (change it here)

Related

Opening project in VSCode using batch file

Disclaimer: I read this and this before, but it doesn't work as I want.
Description: I decided to create set of batch files for convenient way to run different projects in VSCode from desktop in one click(double-click). I want close cmd terminal after running a batch file, but terminal remains. I tried:
start code "C:\Users\MyUserName\path\to\my\project\directory"
and
cmd /c start code "C:\Users\MyUserName\path\to\my\project\directory"
It quickly runs command, runs code and opens my project, then, it seems to me, closes terminal and runs a new one in desktop directory.
I found solution with help of DavidPostill. This works fine for me:
start "" cmd /b /c code "C:\Users\MyUserName\path\to\my\project\directory" && exit 0
UPDATE:
There is a more simple way to run VSCode using command line interface:
cd path/to/my/project
code .
If anyone else comes across this in 2022, I found a solution that works great for me.
code "" "C:\path\to\folder\with\project" | exit
Also, below is my batch I made for a quick workspace that:
asks for a folder name, this will also be used as the project name
makes the vscode project
makes 2 text files, one for things I had to look up, and another for answers to my question
makes a png file called work.png that opens with paint for diagrams I might need for thinking through things
Lastly (the part I love the most) it CLOSES the command window once everything is opened!
Hope this helps someone like me who doesn't know everything about batch files!
#echo off &setlocal
set /p "folder=Enter the folder name to be created: "
md "%folder%" ||(pause &goto :eof)
cd %folder%
echo. > Things_I_had_to_look_up.txt
echo. > Answers.txt
dotnet new console
xcopy /s "C:\xPaintFileTemplate" "C:\Users\TBD\Documents\Program Work\%folder%"
start mspaint.exe Work.png
code "" "C:\Users\TBD\Documents\Program Work\%folder%" | exit
Rem not needed but to be safe
exit
The C:\xPaintFileTemplate is a folder in my C drive that contains only a png file named Work.png. The png file is blank, and sized to what I want mspaint to open with. If there are more files in that folder, it will copy all of them, so be careful with xcopy!
The Rem is a comment, saying the exit command doesn't seem to be required but I added it in anyways as I believe it is good practice.
Try using: start cmd /C code . :0 It should be able to close the cmd terminal. At least that worked for me on my Windows 10.
Another version:
start cmd /C code "C:\Users\MyUserName\path\to\my\project\directory" :0
Based on Blake Daugherty's answer, I found the first pair of double quotes seems unnecessary:
code "D:\proj\directory-1" | exit
code "D:\proj\directory-2" | exit
exit
None of the above worked for me; at worst one of the left-over CMD's needed its close button clicking three times before it would go away.
I tried the URL method and this worked just as I wanted: VSCode opened, and the cmd window went away; my batch file ("VSCode on project.bat") contains just one line:
start vscode://file/C:/path/to/project
or:
start "" "vscode://file/C:/path/to/project"
One line code:
code C:\Users\MyUserName\path\to\my\project\directory pause

Windows CE 5 batch IF EXISTS

I'm trying to run a batch file on a WINCE device that will ping another networked device and capture the results in a text file.
However, whenever I try to execute the section that checks to see if the log file already exists, I get this error:
IF: incorrect command syntax
Can anyone shed some light as to why?
Here is the bit that creates the text file to log the results. It may or may not be worth mentioning, but it works fine on a windows 7 PC:
SET DATETIME=%date:~0,2%\%date:~3,2%\%date:~6,4% 0%time:~1,1%:%time:~3,2%:%time:~6,2%
if NOT exist %DATEVARIABLE%-log.txt type "New Log" > %DATEVARIABLE%-log.txt
Thanks
Doug
Something looks like this :
#echo off
SET "DATETIME=%date:~0,2%-%date:~3,2%-%date:~6,4%_%time:~1,1%-%time:~3,2%-%time:~6,2%"
echo "%DATETIME%"
pause
if NOT exist "%DATETIME%-log.txt" echo "New Log" > "%DATETIME%-log.txt"
pause

Windows Batch: Executing command with FOR /F -- "command not found"

I have a problem with executing an other command within a FOR-command on Windows 7 Ultimate.
The for-command is part of a batch-file and should parse the outputs of another command.
Usually this:
for /f %%a IN ('tasklist') DO echo %%a
should execute the command "Tasklist" and output its results via echo.
But I always get "command not found".
I tried to execute the command outside of the forloop and it works.
I also tried do execute lots of outer commands within the for, but every command said "command not found".
I also tried the examples of this post:
Batch: Execute command with quotes in for loop with piping to find
I`m sure
that the commands exists
that I have read and execute rights to it
that my User is in Administrator Group
that I run the commands with "Run as Administrator"
But nothing is working in this pc.
The same commands work on another PC wich is also running a windows 7 ultimate.
So has anybody an idea would could be wrong on the pc where all commands are not found ?
Here is an example of my console outputs when I try it with the command "ls".
ls.exe is a executable file from the gun4win project, and its located in the same folder where my batch-file is running.
The windows is in german, so the error output is also in german.
C:\test>test_for.bat
C:\test>rem --- test a command stand-alone ---
C:\test>ls
ls.exe test_for.bat
C:\test>rem --- test same command in a FOR-Loop ---
C:\test>for /F "delims=" %a in ('ls') do echo FOR-OUTPUT: %a
Der Befehl "ls" ist entweder falsch geschrieben oder konnte nicht gefunden
werden.
FINAL EDIT:
The problem was as wrong value for the system envoirement variable ComSpec.
I changed ComSpec in Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe and the problem was solved.
Thanks to #foxidrive and #jeb
If you use FOR /F and get the error 'something' is not recognized as an internal or external command, operable program or batch file for every program, even internal commands, the most common cause is a wrong ComSpec variable.
You can check the variable with set ComSpec, it should be C:\Windows\System32\cmd.exe.
It will not help to change the variable on the command line, the cause is described at DosTips: ComSpec strange behaviour
If the variable contains a different value, you should correct this under
For a German system:
Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen
For an English system:
Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables
There exists a second possible cause for strange FOR /F behaviour
If the AutoRun feature can be enabled in the registry ...\Command Processor\AutoRun, for more details see cmd /?.
The AutoRun feature can start a batch file each time a new cmd.exe instance is started.
This can be useful for ex. showing some data on opening a new cmd window or always change to a choosen directory.
But this batch will be also executed inside the FOR /F and normally causes unexpected results.
Pipes also start new cmd instances, but suppress the AutoRun script

Is there a way to check for return code using PSFTP?

I am creating a script file using windows BATCH and then calling it using PSFTP (using Putty from command line to call TransferScript.txt).
The requirement is to get a file from Unix box to windows and then delete it from Unix but after getting the return 0. My code looks like:
TransferScript.txt
lcd E:\Temp
cd /sap/xx/yy
get abc.dat
IF %ERRORLEVEL% EQ 0
rm abc.dat
quit
This doesn't work, however file is getting copied but not getting deleted. Can anyone help?
The command processor for the file is psftp, not cmd.exe. As such, IF is not a recognized command. You can, however, use the ! PSFTP command to pass commands to Windows. This is documented here. Here is a tested working version:
Command: psftp -b script.txt username#ftpserver.com -pw password
#script.txt
!echo Connected. Getting file...
lcd c:\temp
cd sap/xx/yy
get abc.dat
!IF EXIST c:\temp\abc.dat echo Success
!IF NOT EXIST c:\temp\abc.dat echo Failure
quit
Simply replace echo Success with rm abc.dat or whatever you want to happen if the file exists on the local file system.
Another option would be to do all the logic in an external batch file. Write 2 PSFTP scripts like script_get.txt and script_rm.txt and call the second one only if the file exists (like above).
I know neither of these solutions technically answer your question in that they don't capture an error state from PSFTP but hopefully it's an acceptable workaround.

Gracefully trap error on start cmd

On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.

Resources