SVN Batch file output linebreaks and formatting - powershell

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

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"

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

Rename only a specific part of every filename in a folder

I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)
substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)

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

Incorrect encoding after redirecting `dir` output to a file

I run this code on Windows cmd.exe in Europe and I use local settings here, for my language. So I use diacritics in names of the directories.
I try to list names of the directories and they are displayed correctly. Then I save them into file, but when I open it in notepad, the diacritics is not readable: for example, instead of Střední Čechy I have Stýednˇ ¬echy.
What did I do wrong and how can I correct it?
#echo off
del directories.conf
FOR /F "delims=!" %%R IN ('dir * /b /a:d /o:n') DO (
IF EXIST "%%R\scenery" (
echo %%R
echo %%R >> directories.conf
) ELSE (ECHO NOT INCLUDED %%R)
)
Echo Directory list created...
pause
Try starting cmd.exe with /u switch. That will cause cmd to write in UTF-16.
Also you need to switch to code page 1250 (ANSI for Central Europe) using chcp 1250.
You can do it inside your batch script. I made one for you. The structure is:
.\Jižní Morava
.\Jižní Morava\scenery
.\Pelhřimov
.\Pelhřimov\scenery
.\Nic moc výlet
.\Střední Čechy
.\Střední Čechy\scenery
And the script:
#echo off
if _%1_==_main_ (
call :main
) else (
cmd /u /c "%0 main"
)
goto :eof
:main
chcp 1250
del directories.conf
for /F "delims=!" %%R in ('dir * /b /a:d /o:n') do (
if exist %%R\scenery (
echo %%R
echo %%R >> directories.conf
) else (
echo not included: %%R
)
)
echo Directory list created...
pause
goto :eof
Also I recommend you to read andrewdotn's great answer to a related question.
As an alternative solution (if the file is already generated) you can just re-encode your file.
Notepad++ has this feature:
Go to Encoding > Character sets
Select the appropriate character set that has a graceful render
Go back to Encoding > Character sets
Select Convert to UTF-8
Save your file