I am trying to get only one column from task list command in cmd prompt using below
FOR /F "usebackq tokens=5 delims= " %i IN (`netstat -ano ^|findstr /R /C:"\<:9000\>"`) DO FOR /F "usebackq tokens=2 delims= " %z IN (`tasklist /fi "pid eq %i" /fi "services ne MSSQLSERVER" ^|findstr "%i"`) do #echo %z
I am unable to figure out whats missing in my above command. I see a part of command as output instead of seeing the first column of tasklist output..
Any suggestions?
I was missing # before second FOR and my tokens in the second for loop was wrong.
Following worked out for me. Hope it helps others
FOR /F "usebackq tokens=5 delims= " %i IN (`netstat -ano ^|findstr /R /C:"\<:9000\>"`) DO #FOR /F "usebackq tokens=1 delims= " %z IN (`tasklist /fi "pid eq %i" /fi "services ne MSSQLSERVER" ^|findstr "%i"`) do #echo %z
Related
The code is like this but it doesn't work as expected right now:
#ECHO OFF
for /f %%i in ('dir *.mov /b') do call :test %%i
goto continue
:test
if "%1"=="*compressed.mov" goto :eof
echo "%~f1"
goto :eof
:continue
pause
I would like to solve two problems here:
How to escape spaces in file names? ie. I would like to include "video 1.mov" file for the test and not echo just the "Drive:\path\video" but instead "Drive:\path\video 1.mov"
How to not echo any file containing string "compressed" at the end of it's name?
Perhaps something like this would suit:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For %%G In ("*.mov") Do If /I "%%~xG" == ".mov" (Set "_=%%~nG"
SetLocal EnableDelayedExpansion
If /I Not "!_:~-10!" == "compressed" (EndLocal & Echo "%%~fG"
) Else EndLocal
)
Pause
Alternatively use findstr.exe:
#For /F "EOL=? Delims=" %%G In ('Dir "*.mov" /A:-D /B 2^>NUL ^| %SystemRoot%\System32\findstr.exe /LIE ".mov" ^| %SystemRoot%\System32\findstr.exe /VLIE "compressed.mov"') Do #Echo "%__CD__%%%G"
#Pause
I have following batch script to check the specific type of files, in entire directory structure from the current directory, if a string is found in those files or not:
#echo off
set RESULT_FILE="result.txt"
set /p STR="Enter the String to Find: "
pushd %~p0
type NUL > %RESULT_FILE%.tmp
for /f "delims=" %%a in ('dir /B /S *.phtml') do (
for /f "tokens=3 delims=:" %%c in ('find /i /c ".%STR:"=%." "%%a"') do (
for /f "tokens=*" %%f in ('find /i ".%STR:"=%." "%%a"') do if %%c neq 0 echo %%f
)
) >> "%RESULT_FILE%".tmp
move %RESULT_FILE%.tmp %RESULT_FILE% >nul 2>&1
:: Open the file
"%RESULT_FILE%"
popd
But my search string may contain spaces between multiple words and special characters too.
How can I modify the search string variable STR to allow spaces and special characters in the search string as literal?
Right now zero results are returned when the search string contains spaces and special characters.
The following should work. I remove the quotes before the for loop, but only the outer, how it works is explained here at ss64. To work with special characters like a carret (^) I enabled delayed expansion.
Further it won't work with prepending and appending . in the search string, I don't know why you've added it.
I've also added a /A-D so that the dir command will not list directory names.
#echo off
setlocal EnableDelayedExpansion
set RESULT_FILE="result.txt"
set /p STR="Enter the String to Find: "
:: Remove just outer quotes not quotes that are inside
set STR=###!STR!###
set STR=!STR:"###=!
set STR=!STR:###"=!
set STR=!STR:###=!
pushd %~p0
type NUL > %RESULT_FILE%.tmp
for /f "delims=" %%a in ('dir /B /S /A-D *.phtml') do (
for /f "tokens=3 delims=:" %%c in ('find /i /c "%STR%" "%%a"') do (
for /f "tokens=*" %%f in ('find /i "%STR%" "%%a"') do if %%c neq 0 echo %%f
)
) >> "%RESULT_FILE%".tmp
move %RESULT_FILE%.tmp %RESULT_FILE% >nul 2>&1
:: Open the file
"%RESULT_FILE%"
popd
I have windows batch code...
I want to run same code on Linux using sh script shell
#ECHO OFF
setlocal enabledelayedexpansion
color 0a
mode con:cols=68 lines=12
FOR /F "tokens=1 delims= " %%I IN (name.txt) DO FOR /F "tokens=1 delims= " %%E IN (job.txt) DO echo %%I %%E>name_job.txt & echo name_job: %%I %%E & FOR /F "tokens=*" %%S IN ('notepad job:%%E name:%%I') DO echo %%S>>names\%%I_1.txt
I have this sample.
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
Any help would be greatly apreciated.
I have a directory full of files that look like the following:
Codian1_OCT_14_2014_14_52_ccc145b1_WMV.asf
Codian1_OCT_14_2014_14_52_ccc145b1_ASF.asf
Codian1_OCT_13_2014_12_52_ccc145b1_WMV.asf
Codian1_OCT_13_2014_12_52_ccc145b1_ASF.asf
I'm trying to test if the file ends in either WMV.asf or ASF.asf. For some reason the following code doesn't work:
FOR %%i IN (*) DO (IF %%~i=="*WMV.asf" (echo %%~i IS wmv.asf) ELSE (echo %%~i isn't wmv.asf))
Any idea how to do this?
All comparisons should be case insensitive, since Windows doesn't care about case within file paths.
Option 1
#echo off
setlocal enableDelayedExpansion
for %%i in (*) do (
set "file=%%i"
if /i "!file:~-7!"=="wmv.asf" (echo %%i IS wmv.asf) else (echo %%i isn't wmv.asdf)
)
Option 2
for %%i in (*) do echo %%i|findstr /ile "wmv.asf" >nul && (echo %%i IS wmv.asf) || (echo %%i isn't wmv.asf)
A way :
#echo off
setlocal enabledelayedexpansion
FOR %%i IN (*) DO (
set $File=%%i
if /i !$File:~-7!==WMV.asf echo !$File! -^> WMV
if /i !$File:~-7!==ASF.asf echo !$File! -^> ASF)
I've got a script to retrieve PowerPath license information from arround 2k servers, I've automated this with simple script:
for /F %%A in (server_list.txt) do (
echo %%A >> PP_license.txt
psexec \\%%A powermt check_registration | find "Key" >> PP_license.txt
)
But I'm not happy with this output file, which now looks like this:
server1
Key XXXX-XXXX
server2
Key YYYY-YYYY
Is it possible to manipulate this to get output like:
server1 XXXX-XXXX
server2 YYYY-YYYY
?
If not then I'll try do this in PowerShell.
for /F %%A in (server_list.txt) do (
for /F "tokens=1*" %%B in ('psexec \\%%A powermt check_registration ^| find "Key" ') do (
echo %%A %%C>> PP_license.txt
)
)
Try this
ren PP_license.txt PP_license.tmp
3<PP_license.tmp (
:loop
set /p srv=<&3
set /p key=<&3
if "%srv%"=="" goto :end
<nul set /p=%srv% >> PP_license.txt
for /f "tokens=2" %%a in "%key%" do set key=%%a
Echo %key% >> PP_license.txt
goto :loop
:end
)
And that should do exactly what you want.
Mona
for /F %%A in (server_list.txt) do (
(echo|set /p"= %%A ")>> PP_license.txt
for /f "tokens=* delims=" %%x in ('psexec \\%%A powermt check_registration ^| find "Key" ') do (
(echo %%x)>>PP_license.txt
)
)