Set batch file variable to the output of a Powshell script [duplicate] - powershell

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

Related

Get last line of a URL and save as integer variable in batch file

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"

Strip date from filename using batch script

I have multiple files with dates on them I would like to strip.
exOpTimer01232018.txt
exOpProcess01232018.txt
exOpFac01232018.txt
exOpProd01232018.txt
I would like to have a batch script remove the date and leave result such as
exOpTimer.txt
exOpProcess.txt
exOpFac.txt
exOpProd.txt
These are monthly file and the date stamp changes every month.
I have tried doing
RENAME C:\temp\*????????.txt *.txt
But wasn't successful.
Example based on my comment:
#Echo Off
For /F "Delims=" %%A In ('Where .:exOp*.txt 2^>Nul') Do Call :Loop "%%A"
Pause
Exit
:Loop
Set "fName=%~n1"
Ren %1 "%fName:~,-8%%~x1"

Use Variable insted of Get-Date in powershell

I am using PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')" in batch script.
I want to use a variable instead of Get-Date function. Is it possible?
ADate is the variable name!
Edited:
As suggested, my script is:
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"`
) Do Set "Freq=%%A"
Adate is simple string which comes from the file name, and has a value like 16112016.
You need to use a for loop to get the output of an external command in a batch variable:
#echo off
for /f "usebackq tokens=*" %%d in (`powershell "..."`) do set "adate=%%d"
echo %adate%
The usebackq and backticks are just so you don't need to escape the nested single quotes in your command string.
Ok. I got my crystal ball and asked it: "What is the solution here?", and it replied: "Change
PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"
by
PowerShell "(Get-Date -Date '!Adate:~4!-!Adate:~2,2!-!Adate:~0,2!').AddDays(-7).ToString('ddMMyyyy')"
", but I have no idea what it is talking about! ;)
You can store the output of the powerShell command into a file and then read that file after that delete that temporary file.
PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')" >temp.txt
set /p myVarDate= < date_Shell.txt
echo Date from Shell %myVarDate%
del temp.txt
The following takes the last modified time from a known file's properties and creates a variable with a date seven days earlier, (obviously changing C:\Test\TestFile.ext as necessary):
For /F UseBackQ %%A In (
`PowerShell "((gi 'C:\Test\TestFile.ext').LastWriteTime).AddDays(-7).ToString('ddMMyyyy')"`
) Do Set "ADate=%%A"
Edit
The following example takes a date string with a known format, (in this case provided in two variables). It then converts that string to a date object, subtracts seven days and sets it back to a string in the new %ADate% variable:
#Echo Off
Set "DateStr=16112016"
Set "DFormat=ddMMyyyy"
For /F UseBackQ %%A In (`Powershell^
"([datetime]::ParseExact('%DateStr%','%DFormat%', [System.Globalization.CultureInfo]::CurrentCulture)).AddDays(-7).ToString('%DFormat%')"
`) Do Set "ADate=%%A"
Echo(%ADate%
Timeout -1

Trim output of command

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
-------------------------------

bat file read in file

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