Just wondering why I get the "| was unexpected at this time." message when I run the following:
for /f "tokens=2 delims==; " %%a in (' (wmic process call create "%exec%","%workdir%") ^| find "test" ') do set test=%%a
but no error when I run the following:
(wmic process call create "%exec%","%workdir%") ^| find "test"
Thanks!
There are more characters to be escaped in parsed command:
(wmic process call create "%exec%","%workdir%") | find "test"
rem need to escape: ↑ ↑ ↑ ↑
Next command should work - albeit there is no ´test´ word to find in wmic output, probably:
for /f "tokens=2 delims==; " %%a in (' ^(wmic process call create "%exec%"^,"%workdir%"^) ^| find "test" ') do set test=%%a
rem properly escaped: ↑ ↑ ↑ ↑
Tested using
(wmic process call create "cmd.exe","%temp%") | find "ProcessId"
Then, next commands sets process ID of created cmd to variable test:
for /f "tokens=2 delims==; " %%a in (' ^(wmic process call create "cmd.exe"^,"%temp%"^) ^| find "ProcessId" ') do set test=%%a
Related
In my batch file, I am trying to convert a string (%input%) to binary and save the result in a batch variable %varBinary%. My attempt:
for /f "delims=" %%a in ( ' powershell " [System.Text.Encoding]::UTF8.GetBytes(/"%_input%"/) | %{ [System.Convert]::ToString($_,2).PadLeft(8,'0') } " ' ) do set "varBinary=%%a"
echo.%varBinary%
echo."%varBinary%"
echo "%varBinary%"
echo %varBinary%
Output of %varBinary% is always empty. What is wrong here? And how to convert back the Binary to the original string so that it works? Because that didn't work either.
For example, hello should be 0110100001100101011011000110110001101111.
You were close, but you missed a few characters that needed to be escaped.
First, powershell (and most other things) uses \ to escape quotes, not /. Second, in batch, plain % need to be escaped by using %% instead. (On a side note, you would normally have to escape the |, but you don't have to here because it's in a quoted string.)
Finally, if you want to build the string and not just return the binary of the last character, you're going to have to enable delayed expansion and concatenate each line of the output.
#echo off
setlocal enabledelayedexpansion
set /p "_input=Input: "
for /f "delims=" %%A in ('powershell "[System.Text.Encoding]::UTF8.GetBytes(\"%_input%\") | %%{[System.Convert]::ToString($_,2).PadLeft(8,'0')}"') do set "varBinary=!varBinary!%%A"
echo.!varBinary!
echo."!varBinary!"
echo "!varBinary!"
echo !varBinary!
I want to get last line of this link (https://pastebin.com/raw/s5BxuEEw) and +1 it and save as integer.
For example if the last line is 5 , put 6 in variable.
I can get content with this code but I dont know how to filter last line:
#echo off
for /f "tokens=*" %%i in ('powershell /command "(Invoke-WebRequest -Uri 'https://pastebin.com/raw/s5BxuEEw').Content"') do set return=%%i
echo "%return%"
pause
To select only the last line from url content use index [-1]
(but the for /f would nevertheless iterate ALL lines and only the last persists)
To add / increment a number use set /A
#echo off
set "URI=https://pastebin.com/raw/s5BxuEEw"
for /f "tokens=*" %%i in ('
powershell -NoP -C "(Invoke-WebRequest -Uri '%URI%').Content[-1]"
') do set /A "return=%%i+1"
echo "%return%"
pause
Sample output is
"6"
When I run the net view command, it will output similar to the following:
\\C66423
\\C66424
\\C66425
\\C66426
\\C66427
\\C66428
\\C66429
\\C66430
\\C66432
\\C66433
What I would like to know is if it is possible to trim out the \\ before each computer name?
PowerShell:
<new view command here> | ForEach-Object {$_.TrimStart('\')}
setlocal enabledelayedexpansion
for /f %%a in ('net view') do (
set "string=%%a"
echo !string:~2!
)
should do the trick (untested)
Similar to Magoo's answer but without the need for string manipulation, and therefore delayed expansion:
#echo off
for /f "delims=\" %%a in ('net view') do echo %%a
You could also add skip=2 to the for command to remove the first couple of lines of output - which is:
Server Name Remark
-------------------------------
I need to extract the service name only(basically display name) . I tried to use the following command but the command prompt is not showing any thing. Am i missing something?
FOR /F "delims= " %A IN ('sc queryex type= service state= all ^| find "WIN" ') DO #echo %A
Thanks in advance!!!
Writing the output of sc to a temporary file and search that file works well:
>%TEMP%\~sc.log (sc queryex type= service state= all)
for /F "tokens=1,* delims=: " %%I in ('type %TEMP%\~sc.log^|findstr "DISPLAY_NAME"') do echo %%J
To read lines from a file, in a batch file, you do :
for /f %%a in (myfile.txt) do (
:: do stuff...
)
Now suppose you file is in C:\Program Files\myfolder
for /f %%a in ("C:\Program Files\myfolder\myfile.txt") do (
echo %%a
)
Result :
C:\Program Files\myfolder\myfile.txt
This seems to interpret the given path as a string, and thus %%a is your given path.
Nothing about this in the documentation I have found so far.
Please someone help me before I shoot myself.
The documentation you get when you type help for tells you what to do if you have a path with spaces.
For file names that contain spaces, you need to quote the filenames with
double quotes. In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.
By default, the syntax of FOR /F is the following.
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
This syntax shows why your type workaround works. Because the single quotes say to execute the type command and loop over its output. When you add the usebackq option, the syntax changes to this:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
Now you double quote paths to files, single-quote literal strings, and put backticks (grave accents) around commands to execute.
So you want to do this:
for /f "usebackq" %%a in ("C:\Program Files\myfolder\myfile.txt") do (
echo %%a
)
Found it.
for /f %%a in ('type "C:\Program Files\myfolder\myfile.txt"') do (
echo Deleting: %%a
)
Don't even ask me why that works.
Just sharing the below code, hoping that somebody will get benefited.
The below code take both the path having spaces and also if the read lines has spaces, it wont cause any issue of the characters after space is missing;
FOR /f "tokens=* delims=," %%a in ('type "C:\Progrem File\My Program"') do (
echo %%a
)