How to read line from notepad.txt? - command-line

I have this query for AD:
for /f %i in (C:\user.txt) do dsquery user -name %i | dsrm -noprompt
in user.txt I have a name like:
"Jose Maria Alvaro"
When I run the query in command line, it can't pick up the whole name. It picks up only the first name.
Result eg: do dsquery user -name Jose. That is not what I want.
How can I pick the whole name which contains white space?

for /f "DELIMS=" %i in (C:\user.txt) do...
The "delims=" turns off delimiters which default to space and tab, and thus the entire line is delivered as the (default) first token.
See for /? from the prompt for documentation.

Related

Is there a way to capture the file path and name in the same batch function?

I am creating a batch script to perform robocopy functions. Currently I am having to call two different PowerShell selections, one for the file name and then one for the source folder, can I combine this?
Using the code below I can capture the file name, but can I capture both using one method?
echo Select your file
set pwshcmd=powershell -NoProfile -Command "&{[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null;$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $OpenFileDialog.ShowDialog()|Out-Null; $OpenFileDialog.SafeFileName}"
for /f "delims=" %%I in ('%pwshcmd%') do (
set "FileName=%%I"
)
echo %FileName%
pause
I wish for the user to make one selection with PowerShell and it set two variables %FileName% and %FilePath%, as this will be used in the robocopy script.
I appreciate everyone's input on this question. I had solved it shortly after posting, however I did run into the fact that robocopy wants a directory path without the ending "\". The method used to gather both paths were:
for /f "delims=" %%I in ('%pwshcmd%') do (
set "r_copy_file_source_path=%%~dpI"
set "r_copy_file_source=%%~nxI"
)
This provided me with both variables required, then I trimmed the ending "\" from the path with:
set r_copy_file_source_path=%r_copy_file_source_path:~0,-1%
Again, I appreciate the responses! Thank you!

for /f is not working in .bat file but it works with command prompt in windows 2003

for /f "tokens=1-7 delims=,: " %a in ('query user ^| find /i "disc"') do logoff %b
This above code is used for logoff remote desktop users where state is "Disconnected" in windows 2003.It will work perfect when I run in command prompt. But it will not run when I made a .bat file or .cmd file in windows 2003.so may know where i am going wrong?
Inside batch files the percent signs used in the for replaceable parameters need to be escaped
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b
User585,
Yes, inorder to implement the for loop inside a bat/cmd session, you need to place the variable with
%%a
like this
for /f %%a in (.\hosts) do quser /server:\\%%a

command on specific device

I am trying to write a batch script, which will always be executed at a specific location (in this case on my USB-Stick), so typically I would use D:, but sometimes the stick has another drive letter. Therefore I am trying to find the device via its name (USB_Stick).
I haven't found a way to do this via a batch command.
A PowerShell command would look like this:
#(get-wmiobject -query \"select deviceid from win32_logicaldisk where volumename='USB-STICK'\")[0].deviceid"
but I don't know how to use the result of this PowerShell command.
I tried things like this:
for /f "usebackq" %%x in (`powershell.exe -Command "#(get-wmiobject -query \"select deviceid from win32_logicaldisk where volumename='USB-STICK'\")[0].deviceid"`) do (
set res=%%x
)
#echo %res%
but the result of this would only be ommands.GetWmiObjectCommand and not the D:.
If you're going for a batch-script anyway, use the wmic commandline utility:
#echo off
for /f "delims== tokens=2" %%d in (
'wmic logicaldisk where volumename^="USB-STICK" get deviceid /value'
) do set "deviceId=%%~d"
echo %deviceId%
Using the wmic with the /value parameter creates name=value lines as the output, which you can split in the for loop by defining = as the delimiter.
If I understand correctly, the batch file is running on the USB stick? If so, then the drive letter of the stick (without a slash) can be obtained via %~d0

Using output from a PowerShell command in a windows batch file

I have a path in variable (script parameter) %2.
I need to do the following:
Extract the leaf (last folder from the path) to a variable.
Run the following command: robocopy %2 \\somepath\%leaf
I was told this could be done in PowerShell (cause I've tried going with batch file alone and failed miserably) Here's a pseudocode representation of what I'd like to achieve:
set leaf = powershell -command (split-path %2 -leaf)
robocopy %2 \\somepath\%leaf
Any idea how to write this correctly?
Thank you.
Whenever you want to set a batch variable to the output of a command, use for /f. Here's an example:
#echo off
setlocal
set "psCommand=powershell -command "(split-path '%~2' -leaf)""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"
echo %leaf%
But this is a terribly inefficient way to retrieve the last folder of a path. Instead of invoking PowerShell, what you should do is this:
#echo off
setlocal
for %%I in ("%~2") do set "leaf=%%~nxI"
echo %leaf%
The %%~dpnxI notation gets
d = drive
p = path
n = name
x = extension
It's traditionally intended for files, rather than directories; but it works just as well for directories anyway. See the last couple of pages of for /? in a console window for complete details.
FOR %%a IN ("%~2") DO FOR %%b IN ("%%~dpa.") DO ECHO %%~nxb
Batch one-liner. Take the parameter (second parameter here), remove any quotes and re-apply them. Select the drive and path, add '.' then select the name and extension of the result making leaf required.
Obviously, if you require this in a variable,
FOR %%a IN ("%~2") DO FOR %%b IN ("%%~dpa.") DO set "leaf=%%~nxb"

User SID copy/paste using command line?

Can user SID be copied from registry (or whatever) and pasted to a txt file using command line only (Windows 7)?
Via WMIC
wmic useraccount where name='%username%' get sid | findstr /b /C:"S-1" > file.txt
Via WHOAMI (duplicate percent signs if used in batch file)
for /F "tokens=2 delims=," %f in ('whoami /user /FO CSV /NH') do echo %~f > file.txt