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
Related
I have a project which builds an application and whenever it builds the application the names it generates are like this MyApp1.1.exe, myapp1.2.exe, myApp,1.3.exe etc. I would like to deploy the application in another environment whenever there is a new build. But the problem is that I'm using the following in command in the batch script, which is is keep throwing me an error
MyApp1.*.exe
But it always throws an error in the command line saying that 'MyApp1.*.exe' is not recognized as internal or external command, operable program or batch file. I know that this should be very simple but I could not seem to find any solution
cmd, which is used to run batch-files, requires that you run for loops to do something for each item. You therefore need to give for some criteria and it will return the list based on that. You can then do something with these metavariables.
#echo off
for %%i in (MyApp*.*.exe) do echo start "" "%%~i"
For the purpose of this demonstration, I am not actually running the executable's, instead I just echo the full command. If you feel that it is suitable for this purpose, then simpy remove echo from the echo start "".. section.
I have a script that calls other commands in a for loop:
for %%x in (%CMDS::= %) do (
call C:\%%x %1%
echo "%%x complete"
)
However, running this results the console spitting out :
'sleep' is not recognized as an internal or external command,
operable program or batch file.
This is because the files i loop through and run have these commands in them. Why is it that if i run these files one by one they work, but when chained using call they don't? I can sleep in my terminal outside of this script..
Regards
Thanks to another answer, I solved this error by replacing sleep 5 in my .bat file with:
powershell -Command "& {sleep 5}"
Works fine now. Better still, also tested Stephan's suggestion:
timeout 5
Simpler, and shows a nice message like
Waiting for 0 seconds, press a key to continue ...
Note that some Windows versions require the /t option to define the time.
timeout /t 5
There is no sleep command in batch. That's why you are getting this error.
EDIT:
There is no sleep command in Windows CMD or Batch. BUT: as you can use the command in your console, I suppose there might be a script or a program called sleep. This script or program might be situated in your working directory or in some other directory included in your %PATH% variable. If this is the case, it's possible that your script gives you this error because of a path issue.
Say, you are in C:\SomeFolder and there is a sleep.exe in there. You are calling another script or command which changes the current directory to D:\AnotherFolder. Now another script or command tries to execute your mysterious sleep command assuming the working dir to be C:\SomeFolder but as you are in a different folder (D:\SnotherFolder) now, sleep can't be found. Further, when using call the variable scope of the calling script becomes also the scope for the called script. So it's also possible that variables are being overwritten by different scripts. Such a variable might contain the path to your sleep command. This could also cause an error.
I am running the following command in cmd for which I am able to get the log successfully.
cd C:\Users\sriram\AppData\Local\Temp\license1.1.4
C:\Users\sriram\AppData\Local\Temp\license1.1.4>lsmon.exe testprovilic.muc.company> C:\Users\sriram\AppData\Local\Temp\license1.1.4\usage.log
Above command will generate the logs in the usage log file. But I created a batch file as below for which it is giving me an error testprovilic.muc.company not found
#ECHO off
SET variable=C:\Users\s.d.vaidyanathan\AppData\Local\Temp\usage.log
START "C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe testprovilic.muc.company> "%variable%"
Could you please help me solve this issue.
Thanks and Regards,
Sriram
You need to add the "cd C:\Users\sriram\AppData\Local\Temp\license1.1.4" to the batch file. Or use the full path to the file like:
C:\Users\sriram\AppData\Local\Temp\license1.1.4\testprovilic.muc.company
So you would have ..
#ECHO off
cd C:\Users\sriram\AppData\Local\Temp\license1.1.4
SET variable=C:\Users\s.d.vaidyanathan\AppData\Local\Temp\usage.log
START "C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe testprovilic.muc.company> "%variable%"
or you could
#ECHO off
SET variable=C:\Users\s.d.vaidyanathan\AppData\Local\Temp\usage.log
START "C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe C:\Users\sriram\AppData\Local\Temp\license1.1.4\testprovilic.muc.company> "%variable%"
This doesn't work:
"C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe
The whole path needs to be in quotes and there needs to be a backslash after the last folder name.
Change it to this:
"C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4\lsmon.exe"
Open a command prompt window, type set and press key RETURN or ENTER to run this command. You get displayed all predefined environment variables with their current values. You can see TEMP, USERPROFILE and USERNAME.
Windows command line has a help. The command to get help is help, yes really. Try it out! You need help on command CD, enter in command prompt window help cd or alternatively cd /?. You need help on command SET and START, run help set or set /? and help start or start /?. All internal commands of cmd.exe and nearly all console applications support the parameter /? and output 1 or more help pages on running the command with this parameter.
You need a better overview of standard Windows commands? See Microsoft's command-line reference and SS64's command line reference.
I assume that sriram is your user account and s.d.vaidyanathan is the user account of someone else. By default a standard user has no permissions to access folders and files in a different user's profile than the own profile since Windows Vista. That means, you logged in as sriram can't access the files and folders of C:\Users\s.d.vaidyanathan because of missing permissions to do so. It would be necessary to use command Runas to run the batch file with account s.d.vaidyanathan.
#echo off
rem Execute lsmon.exe from license1.1.4 in my folder for temporary
rem files and folders and write the log file also into this folder.
"%TEMP%\license1.1.4\lsmon.exe" testprovilic.muc.company >"%TEMP%\license1.1.4\usage.log"
Note: Double quotes must be used around path AND file name. Just double quoting parts of a file name with path may or may not work depending on error correction and how the application is written. For details see answer on set environment variables with spaces.
See also the Microsoft article Using command redirection operators.
And regarding right usage of command START not really needed here see for example answer on How to call a batch file in the parent folder of current 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)
For some strange reason this batch script is prepending an extra character to the filename while iterating over the non-hidden files in a directory.
See this example:
Plain-text:
FOR /F %i in ('dir "*.sql" /A-H /b') DO #echo %i
Output:
♀test.sql
This is an issue because when I try to open %i it says it doesn't exist.
I could replace the first character, but this script needs to work on multiple machines and not just mine. This only happens on my machine. Any idea how to fix it?
Update
So 0x0C (form feed?) seems to be the character inserted.
First, take a look on answer on Stack Overflow question Windows batch outputs a line feed female character.
Second, take a look on Microsoft's TechNet article Command Processor\AutoRun, or read in help output after entering cmd /? about AutoRun registry value.
It looks like on the computer on which a form-feed is written first to file on redirecting output of command dir, there is a batch file or another command configured to be automatically executed on start of command line processor. And one of those commands is responsible for the form-feed character.
This can be checked quickly without first looking into Windows registry by starting cmd.exe with option /D and then enter the commands as it can be seen on screenshot in question. If the file file.txt is now without a form-feed as first character, there could be indeed an autorun command responsible for this unwanted behavior.
Start now regedit.exe and check the value AutoRun under
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor
and
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
On my computer the value AutoRun does not exist at all in HKCU and is empty in HKLM.
Also run in a command prompt window on this computer just set and look on the environment variables
ComSpec ... should be C:\Windows\System32\cmd.exe
PATHEXT ... should be .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PATH ... should start with C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;
It could be that the command processor on this computer is not cmd.exe or PATH starts with a different folder than System32 directory in Windows directory and this folder contains also a cmd.exe or other console applications which can be found in System32 directory of Windows.
Those 3 very important environment variables can be configured in Control Panel - System - Advanced - Environment Variables according to Environment Variables documentation.