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
-------------------------------
Related
The following commnad:
$sun=PowerShell [DateTime]::Today.AddDays(-8).ToString('dd-MMM-yyyy')
echo %sun %
the output of the echo is
PowerShell [DateTime]::Today.AddDays(-8).ToString('dd-MMM-yyyy')
how do i get it to output something like
22-Sep-2013
You need to use for /f:
for /f "usebackq" %%x in (`powershell "(Get-Date).AddDays(-8).ToString('dd-MMM-yyyy')"`) do set sun=%%x
I have a Script that is searching for the newest file in the directory "test" and gives it the variable "Newest".
pushd c:\Test
for /f %%i in ('dir /b/a-d/od/t:c') do set Newest=%%I
Now let's assume that the name of the file is ThisFile.txt.
I now want to rename it to ThisFile_yyyymmdd.txt by using my variable %Newest%.
What I have so far is this:
Set TDate=%date:~6,6%%date:~3,2%%date:~0,2%
ren %Newest% "%Newest%%TDATE%"
This however renames my file to ThisFile.txt_yyyymmdd which obviously removes the extension and ruins the file.
Does anyone have a solution for this?
Keep in mind that I have to rename it by using the variable %Newest%.
Here you go . Also %%i are case sensitive.
#echo off
setlocal
pushd c:\Test
Set "TDate=%date:~6,6%%date:~3,2%%date:~0,2%"
for /f %%I in ('dir /b/a-d/od/t:c') do (
echo ren %%~fI "%%~nI_%TDATE%%%~xI"
)
exit /b 0
Must read FOR/?
i have a text file datefile.txt that contains
10-06-2013
and I tried to read it using the following bat file:
#echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ datefile.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!"
echo(!var!
ENDLOCAL
)
echo %var%
the output I got are these:
10/06/2013
1:10/06/2013
how come my %var% is different from above one.
Or how could I remove "1:" in the %var%?
thanks.
You got this type of output, as the first line is written by echo(!var!.
The second line by echo %var%, but in the second case the variable doesn't contain the same.
This is because the Setlocal/endlocal block inside the for loop.
In your case you can simply remove the block, as your date doesn't contains any exclamation marks nor carets.
#echo off
SETLOCAL EnableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ datefile.txt"`) do (
set "var=%%a"
set "var=!var:*:=!"
echo(!var!
)
echo %var%
And if your file contains only one line, the solution could be simplified by
<datefile.tx set /p var=
echo %var%
the solution is to remove the "1:" using dos substr replacement:
http://www.dostips.com/DtTipsStringOperations.php#Snippets.Remove
first of all im beginner. i want to create batch file to search through specific folder (including all it subfolder) and copy all file inside it except those which filename contain some specific string,this is what i have so far
set now=fish
set logDirectory="C:\Users\paiseha\Desktop\bb\"
for /r %logDirectory% %%i IN (*%now%*.*) do (
rem copy process goes here
)
let say i have 3 file in it
C:\Users\fareast\Desktop\bb\one.txt
C:\Users\fareast\Desktop\bb\twofishtwo.txt
C:\Users\fareast\Desktop\bb\three.txt
so i want to copy file one.txt and three.txt only, but instead it copy only the second one,i know its because of *%now%*.* so how can i invert it so that it does the other way around, help me pls, thanks in advance
try:
#ECHO OFF &setlocal
set "now=fish"
set "logDirectory=C:\Users\paiseha\Desktop\bb"
for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory%"^|findstr /riv "^.*[\\][^\\]*%now%[^\\]*$"') do (
rem copy process goes here
)
EDIT: The \ character is represented as [\\] instead of \\ because of a quirk on how Vista FINDSTR regex escapes \. Vista requires \\\\, but XP and Win 7 use \\. The only representation that works on all platforms is [\\]. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.
for /f "delims=" %%a in ('dir /a-d/s/b "%logDirectory%" ') do echo %%~nxa|findstr /i /L "%now%" >nul&if errorlevel 1 ECHO COPY "%%a"
should work for you.
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
)