There is a txt file which contains the values ​​shown below. File encoding 1251. How to remove a space character from each line of a file knowing its <A0> code in 1251 encoding? Using the bat file. Encoding windows 866 console.

Original file

The output should receive a file of this type.

enter image description here

Remove space by SET variable=!variable: =! Does not work.

Now the script has this form.

 @ECHO OFF chcp 1251 SetLocal EnableDelayedExpansion erase lb_2_task_1_boofer_1.txt erase lb_2_task_1_PID.txt erase lb_2_task_1_MEM.txt tasklist /NH /FO csv /FI "imagename eq chrome.exe" > lb_2_task_1_boofer_1.txt SET FILE=lb_2_task_1_boofer_1.txt SET MAX_PROGRAM_MEM=50000 FOR /F "tokens=1-5 delims=," %%a IN (%FILE%) do ( SET PID=%%b SET PID=!PID:~1,-1! :Убрал кавычки для PID SET MEM=!PID: =! :Убрал все пробелы @ECHO !PID!>>lb_2_task_1_PID.txt :Вывел в файл SET MEM=%%e SET MEM=!MEM:~1,-3! :Убрал кавычку в начале и кавычку КБ и пробел сзади :SET MEM=!MEM: =! :Для удаления пробела между 108 792 это не подходит :@ECHO !MEM! @ECHO !MEM!>>lb_2_task_1_MEM.txt ) PAUSE 
  • And powershell something to do with it? By the way, didn’t you think to rewrite your script? - Pavel Mayorov
  • SET MEM=!PID: =! :Убрал все пробелы SET MEM=!PID: =! :Убрал все пробелы - exactly SET MEM = ...? - Alekcvp
  • The purpose of the script is not clear. Need to make a list of ID-process and the amount of RAM it occupies? Or just a list of Chrome subprocess memory sizes expressed in KB (without spaces)? - Daemon-5
  • As I understand it - to get a list of memory taken, in the form of numbers without separators. - Alekcvp

3 answers 3

Actually, I guess why this whole whistle with files and encodings.

But I think that if such initial conditions are given:

  • use tasklist utility;

  • get memory parameters in KB (that is, strings with a length of no more than 15 characters.

That the creation of temporary files is not necessary in principle. Everything can be crammed into a pair of nested loops. In the first loop, the values ​​of the memory size column are retrieved. In the second, only numbers are extracted.

 @echo off @SetLocal EnableDelayedExpansion @for /f "delims=, tokens=1,2,3,4,5" %%a in ('@tasklist /NH /FO csv /FI "imagename eq chrome.exe"') do ( @set _str=%%e @set _str=!_str:~1,-3! @set str= @For /l %%g in (0,1,15) do ( @Set "_char=!_str:~%%g,1!" @set p=!_char! @if not defined _char @set "p=" @if 0 gtr !_char! @set "p=" @if 9 lss !_char! @set "p=" @set str=!str!!p! ) @echo !str! ) @setlocal DisableDelayedExpansion 
  • In general, the reason for the appearance of a question in this form is the incorrect display on the screen of the discharges of the number of memory by the tasklist utility (the FF symbol). If on the system being processed it is possible to use another utility. Then I suggest the following command instead: wmic process where (name = "chrome.exe") get WorkingSetSize - Daemon-5

The character with the code 0xA0 is not [normal] space, the space has the code 0x20.

Accordingly, you can try to make SET MEM=!MEM:<Alt+(0160)>=! where <Alt + (0160)> are the numbers 0160 dialed on the NumPad with Alt pressed.

Example:

 @echo off chcp 1251 SetLocal EnableDelayedExpansion set test=Demo<Alt+(0160)>String echo %test% set test=!test:<Alt+(0160)>=! echo %test% 

I have this:

 Текущая кодовая страница: 1251 Demo String DemoString 

    And if so:

     @echo off chcp 866>nul SET "FILE=lb_2_task_1_boofer_1.txt" del %FILE%>nul 2>&1 del lb_2_task_1_MEM.txt>nul 2>&1 tasklist /NH /FO csv /FI "imagename eq chrome.exe">%FILE% for /f delims^=^"^ tokens^=9 %%a in (%FILE%) do ( call :foo %%%a )>>lb_2_task_1_MEM.txt type lb_2_task_1_MEM.txt pause exit :foo set p=%1 :sh shift if %2.==. (echo %p% & exit /b) set "p=%p%%1" goto :sh 

    Well or so (with chcp 866 and SET MEM=!MEM:я=! ):

     @ECHO OFF chcp 866 SetLocal EnableDelayedExpansion erase lb_2_task_1_boofer_1.txt erase lb_2_task_1_PID.txt erase lb_2_task_1_MEM.txt tasklist /NH /FO csv /FI "imagename eq chrome.exe" > lb_2_task_1_boofer_1.txt SET FILE=lb_2_task_1_boofer_1.txt SET MAX_PROGRAM_MEM=50000 FOR /F "tokens=1-5 delims=," %%a IN (%FILE%) do ( SET PID=%%b SET PID=!PID:~1,-1! SET MEM=!PID: =! @ECHO !PID!>>lb_2_task_1_PID.txt SET MEM=%%e SET MEM=!MEM:~1,-3! SET MEM=!MEM:я=! @ECHO !MEM!>>lb_2_task_1_MEM.txt ) type lb_2_task_1_MEM.txt PAUSE