for /f %%i in ('someprogram %1 2>&1 | find /c "some string"') do ...
it says
2>&1 was unexpected at this time
You will need to escape special characters within the for command:
for /f %%i in ('someprogram %1 2^>^&1 ^| find /c "some string"') do ...
cmd's parser is not the most robust one; this is unfortunately necessary.
Related
OK, so the below script removes the spaces from before and after, and it works now. I am sure there are much better ways to write this.
setlocal enableextensions enabledelayedexpansion
for /f "tokens=1-2 delims=:" %a in ('netsh wlan show interface^|find "Name"') do (echo %b >> c:\temp\WLANINT.txt)
for /f "delims=" %x in (c:\temp\WLANINT.txt) do SET WLANINT=%x
:SpaceX
echo "%WLANINT%"
IF "%WLANINT:~0,1%"==" " (SET WLANINT=%WLANINT:~1,-1%)
echo "%WLANINT%"
IF "%WLANINT:~0,1%"==" " GOTO SpaceX
echo "%WLANINT%"
for /l %a in (1,1,100) do if "!WLANINT:~-1!"==" " set WLANINT=!WLANINT:~0,-1!
echo."%WLANINT%"
netsh wlan set profileorder name="%WIFI%" interface="%WLANINT%" priority=1
echo "%WLANINT%"
Try next code snippet (save as 31194241.bat; running it might require administrative privileges):
#ECHO OFF >NUL
SETLOCAL enableextensions
for /f "tokens=1,* delims=:" %%a in ('
netsh wlan show interface^|find "Name"
') do for /f "tokens=*" %%x in ("%%b") do (
echo netsh wlan set profileorder name="%WIFI%" interface="%%x" priority=1
)
Changes made to your script (read more):
in all %a, %b, %c doubled percent signs as %%a, %%b, %%c for using in a .bat script;
nested loops instead of creating an auxiliary file;
for /f "tokens=*" will left trim blak space(s);
productive netsh command is merely echoed for debugging purposes only (replace echo netsh with netsh no sooner than debugged).
Instead of batch, one could use an one-liner from command line:
for /f "tokens=1,* delims=:" %a in ('netsh wlan show interface^|find "Name"') do #for /f "tokens=*" %x in ("%b") do #echo netsh wlan set profileorder name="%WIFI%" interface="%x" priority=1
I need a batch script to list .avi|.mp4 and run a command for all directories that contain such files:
mencoder.exe <some_arguments> -o "output/(name_of_directory).mp4" <list_of_files_in_directory_spearated_by_spaces_evey_file_quoted>
Could anybody help me with that? Is it possible at all?
I made a script in PHP, compiled it with BamCompile, but it seems to act totally different on Windows 8, arrays are being converted to strings and something really weird is goin on...
try this:
#echo off &setlocal
cd /d "%userprofile%\Videos"
for /f "tokens=1*delims=:" %%a in ('dir /b /a-d *.avi *.mp4^|findstr /n $') do set "$%%a=%%~b"
setlocal enabledelayedexpansion
for /f "tokens=1*delims==" %%a in ('set "$" 2^>nul') do set "line=!line! "%%~b""
echo mencoder.exe [some_arguments] -o "output/(name_of_directory).mp4" %line%
Look at the output and remove echo if it looks good.
See if this floats your boat. ! characters are verboten in paths and filenames.
#echo off
setlocal enabledelayedexpansion
cd /d "%userprofile%\Videos"
for /f "delims=" %%a in ('dir /s /b /a-d *.avi *.mp4') do (
for /f "delims=" %%b in ("%%~dpa.") do (
set "line="
for %%c in ("%%~dpa*.*") do set line=!line! "%%c"
echo mencoder.exe [some_arguments] -o "output\%%~nxb.mp4" !line!
pause
)
)
When I run the following in cmd prompt, it works fine
pushd "C:\Converter"
for /f "delims=" %g in ('dir "C:\Toconvert" /b /s /ad') do (powershell .\BatchConvert.ps1 "%g" 10)
popd
It executes the command successfully.
This is the output:
C:\Converter>(powershell .\BatchConvert.ps1 "C:\Toconvert\Prego!PDF" 10 )
When I tried running the same command in a .bat file, it fails. Using cmd line to run the bat file I find this is what it is generating:
C:\Converter>for /f "delims=" \Toconvert" /b /s /ad') do (powershell .\BatchConvert.ps1 "g" 10)
Why is it dropping off the "%g in ('dir "C:" before the brackets and why is it dropping off the % in the do command? How do I escape characters to get this to work as a .bat?
% must be doubled as %% when used in batch files – dbenham May 17 '13 at 12:39
Question: I've several text files containing sql create table/column/view/storedProcedure textfiles. Now I want to merge the textfiles into one textfile.
I go into the directory, and type:
type *.sql >> allcommands.sql
Now to problem is I should add the text ' GO ' after every file's content.
I can append Go by doing
type *.sql >> allcommands.sql & echo GO >> allcommands.sql
But this only inserts go once.
How can I accomplish this with DOS commands ?
You want something like this:
for %%f in (*.sql) do type %%f >>allcommands.sql & echo GO >> allcommands.sql
The %% is for use in a batch file. If you're not running it from a batch file you only need single % signs.
Try this one
for %%f in (*.sql) do (
type %%f >>allcommands.sql
echo. >> allcommands.sql
echo GO >> allcommands.sql
echo. >> allcommands.sql )
it adds newline then go for each SQL file. it works for me try it.
Use copy to concatenate the first file with a file with "GO" text, then concatenate again with the second file.
#echo off
CLS
::concat.bat outfile.sql
setlocal EnableDelayedExpansion
If EXIST GOTMP.TMP DEL /Q GOTMP.TMP
Echo GO>GOTMP.TMP
ECHO.>>GOTMP.TMP
If EXIST "%~1" DEL /Q "%~1"
Echo.>"%~1"
for /f "tokens=*" %%A in ('dir /a-d /on /b "*.sql"') do call :perfaction "%%A%" "%~1"
ECHO Done Generating Output "%~1"
ECHO.
pause
Goto :EOF
:perfaction
ECHO "%~1"
copy "%~2"+"%~1"+GOTMP.TMP "%~2"
GOTO :EOF
How can I customize the the FOR command below to loop through the files inside the Database folder following the order of the number prefixed in the file name?
FOR /R ../Database %%f IN (*.sql) DO sqlcmd -S %1 -d %2 -U %4 -P %5 -i "%%~f" >> Logs/%2_DBInstall.log || goto errors
Database folder contains:
001_usp_procedure1.sql
002_ups_procedure2.sql
Thanks very much,
Please try the following
del temp.txt
del temp1.txt
for /F "usebackq " %%F IN (`dir /b /s /o:n "../Database/*.sql"`) DO (
echo %%~nF / %%F >> temp.txt
)
sort temp.text temp1.text
for /F "tokens=1,2 delims=/" %%i IN (temp1.txt) DO sqlcmd -S %1 -d %2 -U %4 -P %5 -i "%%j" >> Logs/%2_DBInstall.log || goto errors
It should work for recursive directories too (process is sorted by global filename without taking account of the path ... did I understand the problem?)
Please note that two temp files are being used. Be careful with the names (or use real temp files)