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

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"

Related

Batch Script to check first 5 char vs MM-DD

I'm trying to build a script for one of our remote media players and am trying to get it to update once a file shows up in the dropbox. I need it to check the first 5 of the title against MM-DD and if they match then play the video in question. Playing the video is no issue, neither are the archives. My issue right now is that when I try to make a for loop for the files in the location I'm getting the syntax of the command is incorrect or "x" was not expected at this time. also, my date is being formatted like this: 05 -02, and I dont know why.
:::::::::::::::::::::::::::::::::::::::::::::: Get Date :::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
::Finalize Date
Set date=%_mm%-%_dd%
echo %date%
::::::::::::::::::::::::::: Check the downloads folder for completed video files :::::::::::::::::::::::::::::::
:: Loop through all files
:loop
for %%a IN ("dir \Dropbox\Trailer") DO (
::IF the name is not blank, get the first 5 characters of the video name
set first5=%a:~0,5%
::Compare those characters to the date
IF "%first5%" == "%date%" (
taskkill /im wmplayer.exe /t
::::::::::::::: Archive all previous Videos :::::::::::::
for /r %%i in ("dir \Dropbox\Trailer") do (
xcopy /s (%%i) "dir \Archived_Shows\"
)
ping localhost
"C:\Program Files\Windows Media Player\wmplayer.exe" "C:\Dropbox\Trailer\%%a" /fullscreen
:::::::::::::::::::::::::::::::: Exit if new video is running ::::::::::::::::::::::::::::::::::::::
exit
)
)
goto :loop
I'm not sure exactly what your script is supposed to be doing but here is an example based on my understanding.
#Echo Off
CD "Dropbox\Trailer" 2>Nul || Exit /B
For /F "Tokens=1-2" %%A In (
'WMIC Path Win32_LocalTime Get Day^,Month^|FindStr [1-9]'
) Do Set /A _dd=10%%A,_MM=10%%B
Set "DString=%_MM:~-2%-%_dd:~-2%"
:Loop
If Not Exist "%DString%*" (
Timeout 300 /NoBreak
GoTo Loop
)
TaskKill /IM WMPlayer.exe /T 2>Nul
Timeout 10 /NoBreak >Nul
XCopy "." "%~dp0Archived_Shows" /S /D /I
For %%A In ("%DString%*") Do (
"%ProgramFiles%\Windows Media Player\wmplayer.exe" "%%A" /FullScreen
)
GoTo Loop
If no matching file is found it currently checks for new ones every 300 seconds, (5 minutes). If a matching file is found, the loop only resumes once you close WMPlayer. You can change that timespan on line 11 as per your preference.
You are setting the variable %date%, which is a system dynamic variable. Attempting to modify the contents is an unrecommended act, so instead, use another name.
Also, I have simplified the get-and-trim MM-DD part, and it works properly for me.
for /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%~J
Set _dd=00%%~G
)
:s_done
::Finalize Date
Set titleDate=%_mm:~-2%-%_dd:~-2%
echo %titleDate%
pause

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"

SVN Batch file output linebreaks and formatting

I am creating a simple 'post-commit' svn batch script, which will run when a member of our development team commits a build. This batch file sends off the resulting output to a slack service which spits it out to our team channel.
This all works without any problems, but I'd like to put in some bespoke formatting such as new lines and maybe bold text. It's a bit hard to read at present. Being able to break to a new line is the main thing that I'm after. I've done a lot of searching, but can't seem to find the correct answer.
Does anyone have experience with this?
Here's the contents of my script currently:
set one=%1
set rev=%2
set changes=svnlook changed %one% -r %rev%
set whatchanged=nothing
for /f "delims=" %%a in ('%changes%') do ( set whatchanged=%%a )
for /f "delims=-" %%i in ('%rev%') do (
SET rev=%%i
)
for /f "delims=" %%g in ('svnlook log %one% -r %rev%') do (
SET comlog=%%g
)
for /f "delims=" %%i in ('svnlook author %one% -r %rev%') do (
SET "author=%%i"
)
for /f "delims=" %%i in ('svnlook changed %one% -r %rev%') do (
SET changed=%%i
)
powershell -Command "& Invoke-RestMethod -Uri 'our URL with a unique token goes here' -Method Post -Body (ConvertTo-Json #{text='%author% %rev% %one% %comlog% %whatchanged%'})"
REM All checks passed, so allow the commit.
exit 0
The output currently looks like this:
liam.mcdonald 171 D:\Repositories\company
Here is another one:
U Test/New Text Document.txt

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

Syntax of the "for" command: whats wrong with line 10 in following batch file?

1 echo off
2
3 echo Please enter a date
4 set /p a=
5 echo %a%
6 for /D %%d in (M:\Serienbriefauftrag\*) do (
7 if not exist %%d\Erledigt\*.bat echo %%d
8 )
9 pause
10 for /d %%b in (dir M:\Serienbriefauftrag\%%d /T:C)
11 echo %%b
12 pause
The Code works fine until line 10 (syntax error) the aim is, to get the creation date of the folder compare it with "%a%" and and if it's under the entered date "%a%", the folder should be moved. but somehow...
found a way to get the creation date
if not exist %%d\Erledigt\*.bat echo %%~td|findstr /i /l
the new task is to make the output "calculateable"
Two things:
1) In line 10 %%d is undefined (empty), since the scope of the first loop (where it is defined) is left in line 8.
You can set another variable to %%d to be able to use it after the loop ends, but be careful. In batch using set in for loops is a bit tricky. To get around the aweful use of delayed variable expansion, I would suggest to you staying inside of the loop while doing your work.
#echo off
set /p a=Please enter a date:
for /d %%d in (M:\Serienbriefauftrag\*) do (
if not exist %%d\Erledigt\*.bat (
:: Do whatever you want with %%d in here
echo "%%d"
)
)
pause
2) The syntax of your second loop is wrong. You are missing the do before the loop body and /d lets the loop iterate over all folders in a specified directory. dir M:\Serienbriefauftrag\%%d /T:C is not a directory but a command.
If you want to iterate over the output of that command, you have to use /f and put the contents of the paranthesis in single quotes, like that ('dir M:\Serienbriefauftrag\%%d /T:C').