Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
BoQsc
- Posts: 92
- Joined: 30 Jun 2014 04:10
#1
Post
by BoQsc » 17 Jul 2014 01:35
Hello everyone, i need help with converting/cleaning off junk from variable.
Code: Select all
@echo off
::name of process
set processname=@echo off
::name of process
set processname=chrome.exe
for /f "skip=1 tokens=1,4,* delims=," %%a in (
'tasklist /fi "memusage gt 100" /fo csv'
) do if %%a=="%processname%" (
echo ram-usage: %%c
::converting kilobytes format HERE I NEED HELP!
SET ram-usage=%%c
SET _remove_spaces=%_ram-usage: =%
SET _remove_k_letter=%_remove_spaces:k=%
SET _ram-usage=%_remove_k_letter%
ECHO %_ram-usage%
)
)
pause
ECHO %_ram-usage% The variable that should output something like that
outputs this kind of "error" instead:
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 17 Jul 2014 01:59
You need to use this before the loop
and inside the loop you need !variable! syntax instead of %variable%
-
BoQsc
- Posts: 92
- Joined: 30 Jun 2014 04:10
#3
Post
by BoQsc » 17 Jul 2014 02:15
foxidrive wrote:You need to use this before the loop
and inside the loop you need !variable! syntax instead of %variable%
It seems, now i'm getting this kind of output and it's not what i want:
Instead of (as mentioned before):
Thanx, for a replay, but what i'm doing wrong now?
Code: Select all
@echo off
::name of process
set processname=chrome.exe
set process.scan.range=100
::RandomAccessMemory loop
setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,4,* delims=," %%a in (
'tasklist /fi "memusage gt %process.scan.range%" /fo csv'
) do if %%a=="%processname%" (
echo ram-usage: %%c
::converting kilobytes format
SET ram-usage=%%c
SET _remove_spaces=%_ram-usage: =%
SET _remove_k_letter=%_remove_spaces:k=%
SET _ram-usage=!_remove_k_letter!
ECHO !_ram-usage!
)
)
pause
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 17 Jul 2014 04:23
All your environment variables within the loop need
!var! syntax
-
BoQsc
- Posts: 92
- Joined: 30 Jun 2014 04:10
#5
Post
by BoQsc » 17 Jul 2014 04:47
foxidrive wrote:All your environment variables within the loop need
!var! syntax
Nothing new at all. :\
Code: Select all
@echo off
::name of process
set processname=chrome.exe
set process.scan.range=100
::RandomAccessMemory loop
setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,4,* delims=," %%a in (
'tasklist /fi "memusage gt %process.scan.range%" /fo csv'
) do if %%a=="!processname!" (
echo ram-usage: !!c
::converting kilobytes format
SET ram-usage=!!c
SET _remove_spaces=!_ram-usage: =!
SET _remove_k_letter=%_remove_spaces:k=!
SET _ram-usage=!_remove_k_letter!
ECHO !_ram-usage!
)
)
pause
::LSS GTR
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#6
Post
by foxidrive » 17 Jul 2014 05:17
%%c is not an environment variable and should not be !!c
-
BoQsc
- Posts: 92
- Joined: 30 Jun 2014 04:10
#7
Post
by BoQsc » 17 Jul 2014 05:58
foxidrive wrote:%%c is not an environment variable and should not be !!c
Still not so much news:
I don't get it
Script:
Code: Select all
::name of process
set processname=chrome.exe
set process.scan.range=100
::RandomAccessMemory loop
setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,4,* delims=," %%a in (
'tasklist /fi "memusage gt %process.scan.range%" /fo csv'
) do if %%a=="!processname!" (
echo ram-usage: %%c
::converting kilobytes format
SET ram-usage=%%c
SET _remove_spaces=!_ram-usage: =!
SET _remove_k_letter=!_remove_spaces:k=!
SET _ram-usage=!_remove_k_letter!
ECHO !_ram-usage!
)
)
pause
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#8
Post
by foxidrive » 17 Jul 2014 06:52
See what this change prints. Show us the screen output - copy/paste into a code block.
Code: Select all
) do echo %%a,%%b,%%c& if %%a=="!processname!" (
-
BoQsc
- Posts: 92
- Joined: 30 Jun 2014 04:10
#9
Post
by BoQsc » 17 Jul 2014 07:22
foxidrive wrote:See what this change prints. Show us the screen output - copy/paste into a code block.
Code: Select all
) do echo %%a,%%b,%%c& if %%a=="!processname!" (
Here you go:
Code: Select all
"explorer.exe","1","128 752 K" if "explorer.exe"=="chrome.exe" (
"WWAHost.exe","1","122 176 K" if "WWAHost.exe"=="chrome.exe" (
Script:
Code: Select all
@echo off
::name of process
set processname=chrome.exe
set process.scan.range=100000
::RandomAccessMemory loop
setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,4,* delims=," %%a in (
'tasklist /fi "memusage gt %process.scan.range%" /fo csv'
) do echo %%a,%%b,%%c if %%a=="!processname!" (
::echo ram-usage: %%c
::converting kilobytes format
SET ram-usage=%%c
SET _remove_spaces=!_ram-usage: =!
SET _remove_k_letter=!_remove_spaces:k=!
SET _ram-usage=!_remove_k_letter!
ECHO !_ram-usage!
)
)
pause
::LSS GTR
-
Squashman
- Expert
- Posts: 4465
- Joined: 23 Dec 2011 13:59
#10
Post
by Squashman » 17 Jul 2014 07:44
Just for the sake of testing I put the memory usage to zero.
Code: Select all
echo off
::name of process
set processname=chrome.exe
set process.scan.range=0
::RandomAccessMemory loop
setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,4,* delims=," %%a in ('tasklist /fi "memusage gt %process.scan.range%" /fo csv') do (
if "%%~a"=="!processname!" (
echo ram-usage: %%~c
SET _ram-usage=%%~c
SET _ram-usage=!_ram-usage: K=!
ECHO !_ram-usage!
)
)
pause
Output
Code: Select all
ram-usage: 102,264 K
102,264
ram-usage: 109,436 K
109,436
ram-usage: 115,888 K
115,888
ram-usage: 10,032 K
10,032
ram-usage: 43,512 K
43,512
ram-usage: 54,984 K
54,984
Press any key to continue . . .
-
Squashman
- Expert
- Posts: 4465
- Joined: 23 Dec 2011 13:59
#11
Post
by Squashman » 17 Jul 2014 07:46
Your initial problem was your environmental variable name. You don't have an underscore when you set the token variable to the environmental variable.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#12
Post
by foxidrive » 17 Jul 2014 07:55
This seems to return the figure you want:
The issues were:
A) the if comparison was case sensitive
B) the first ram-usage set command variable was missing a _
C) using :: in a loop as a rem command can break the loop. Remove the rem below to see how
D) you had unbalanced parentheses - I added the if compare within a new loop
E) the delayed expansion you now know about
Code: Select all
@echo off
::name of process
set processname=chrome.exe
set process.scan.range=100000
::RandomAccessMemory loop
setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,4,* delims=," %%a in (
'tasklist /fi "memusage gt %process.scan.range%" /fo csv'
) do (
if /i %%a=="!processname!" (
rem ::ram-usage: %%c
::converting kilobytes format
SET _ram-usage=%%c
SET _remove_spaces=!_ram-usage: =!
SET _remove_k_letter=!_remove_spaces:k=!
SET _ram-usage=!_remove_k_letter!
ECHO !_ram-usage!
)
)
pause
::LSS GTR