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
Related
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 am writing a batch script where i call a matlab function i wrote to process some file
it was working fine in the simpler case but when i added in the for /r and passed the parameters it's giving me
The syntax of the command is in correct pointing to the first line calling the matlab
i commented that line now the same error is pointing to the start command
i know it's probably something simple i am missing but i can't figure out my mistake
C:\Cygwin64\bin\bash "/cygdrive/f/database/sfs1.sh" &
for /r F:\database\trimmed %%A in (*.txt) do (
echo %%~dpnxA
matlab /nosplash /minimize /nodesktop /r "editEnergyfile('%%~dpnxA')";quit &
:loop
tasklist /fi "imagename eq matlab.exe" | C:\windows\system32\find /i /n ":" > nul
if errorlevel 1 goto loop
start "" /b /wait /separate proc_eng_track.exe "%%~dpnA" &
matlab /nosplash /minimize /nodesktop /r "Sail_speechrate('%%~dpnA')";quit
)
for the record the code now works and doesn't give any syntax error
::echo off
C:\Cygwin64\bin\bash "/cygdrive/f/database/sfs1.sh"
for /r F:\database\trimmed %%G in (*.txt) do (
echo %%~dpnxG
matlab /wait /nosplash /minimize /nodesktop /r "editEnergyfile('%%~dpnxG')";quit
:loop
tasklist /fi "imagename eq matlab.exe" | C:\windows\system32\find /i /n ":" > nul
if errorlevel 1 goto loop
start "" /b /wait /separate proc_eng_track.exe "%%~dpnG"
matlab /nosplash /minimize /nodesktop /r "Sail_speechrate('%%~dpnG')";quit
)
problem now is that it won't always expand the parameters in the last couple of lines
so sometimes it gives me
start "" /b /wait /separate proc_eng_track.exe "%~dpnG"
the file couldn't be opened no such file or directory
I need to scan through a drive and list all the folders containing only a single file, entitled "Thumbs v0.1.db". I have cobbled together the following code but it doesn't seem to work. Either the batch file exits prematurely, or it completes without listing any such subdirectories! I would be very thankful if someone could point out the problem.
#echo off
SET /P folder="Please enter root directory to seach in: "
SET writefile="C:\Users\MYNAME\Desktop\Thumbs.txt"
SET tmp="C:\Users\MYNAME\Desktop\rowcounttmp"
Echo Searching for directories, please wait...
echo Thumbs v0.1.db-only directories in %folder% > %writefile%
cd /D %folder%
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | find /c /v "~StringWhichWillNotAppear~" > %tmp%
set var=<%tmp%
if [%var%] == 1 dir /a /b "%%~fA" 2>nul | findstr /i "Thumbs v0.1.db" >nul && echo %%~fA >> %writefile%
)
del %tmp%
Pause
Thanks
EDIT:
Thanks to #peter-wright, I now have this: Have I implemented his suggestions incorrectly? It seems to crash for certain filepaths (specifically the read-only drive I am trying to scan through) and there are still no results appearing.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Color 0A
SET /P folder="Please enter root directory to seach in: "
SET writefile="C:\Users\MYNAME\Desktop\Thumbs.txt"
SET tempfile="C:\Users\MYNAME\Desktop\rowcounttmp"
Echo Searching for directories, please wait...
echo Thumbs v0.1.db only directories in %folder% > %writefile%
cd /D %folder%
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | find /c /v "~StringWhichWillNotAppear~" > %tempfile%
set var=<%tempfile%
if !var! == 1 dir /a /b "%%~fA" 2>nul | findstr /i "Thumbs v0.1.db" >nul && echo %%~fA >> %writefile%
)
del %tempfile%
Pause
The variable var is being set within a BLOCK (parenthesised series of statements.)
Any %var% within a block is replaced by the value of that var WHEN THE BLOCK IS PARSED, not when it is EXECUTED.
To access the RUN-TIME value of var use !var! AFTER having invoked delayedexpansion with a SETLOCAL ENABLEDELAYEDEXPANSION statement (probably best implemented immediately after your #echo on.
Danger, Will Robinson : DO not use tmp as a variable name - tmp is a magic variable with special meaning to batch. Others may be listed by invoking
SET
from the prompt. Also avoid DATE, TIME, RANDOM, CD and a few others. And really not a good idea to use executable names or batch keywords either...
tip:
try using
FOR /F "delims=" %%i in ('dir /s /a /b /a-d "Thumbs v0.1.db" ') do (
and then %%~dpi will be assigned the names of the directories that CONTAIN the target file. If the count-of-files in these directories is not ==1 then it is not alone...
I've been tasked with updating an existing windows scheduled task. The task simply calls a windows console based FTP command using an existing script text file using the -s:ftpScript.txt syntax.
The problem is that the filename has changed and will now be based on the current date such as filename20110101.txt.
How can I get the -s:ftpScript.txt to understand that there is a dynamic filename now required? Do I have to recreated the "ftpScript.txt" file dynamically each time the task fires to then include a new static file containing the current date based filename?
I've now since followed my initial suggestion and it works perfectly. I've posted the following below in case it helps anyone else;
echo Generating New FTP Request
set request=Request.txt
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET month=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET day=%%B
FOR /F "TOKENS=3,4 DELIMS=/ " %%A IN ('DATE /T') DO SET year=%%B
set /A day=%day%-1
set yesterday=%year%%month%%day%
set file=<filename>
(
echo open <server>
echo <pass>
echo get %file%%yesterday% %file%%yesterday%
) > %request%
ftp -i -s:%request%
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
)