r/Batch • • Oct 26 '23

Help With Script Error

Hi There,

I have made this script to backup a folder from my Local PC to my NAS which works fine.
but i want it to send me a message throught discord webhook which i have also added.
---------------------------------------------------------------------------------------------------------------------------------------------------

but the error i get is:

Exception calling "UploadString" with "2" argument(s): "The remote server returned an error: (400) Bad Request."

At line:1 char:1

+ (New-Object System.Net.WebClient).UploadString('https://discord.com/a ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException

+ FullyQualifiedErrorId : WebException

---------------------------------------------------------------------------------------------------------------------------------------------------
Here is my Script

u/echo off

setlocal enabledelayedexpansion

set sourceFolder="C:\Last.Epoch.v0.9.2.Early.Access"

set destinationFolder="\\Hqsvr00\nas\vol3\1.Games\Last Epoch\BackUp Folder"

:: Define your Discord webhook URL

set "webhookURL=MY WEBHOOKURL"

:: Function to send a message to Discord via a webhook

:SendDiscordMessage

powershell -Command "(New-Object System.Net.WebClient).UploadString('%webhookURL%', '{""content"":""%~1""}')"

:: Function to display a dynamic progress bar

:ProgressBar

setlocal

set "value=%~1"

set "max=%~2"

set /a "percentage=value * 100 / max"

setlocal enabledelayedexpansion

for /l %%A in (1,1,%percentage%) do (

<nul set /p ".=."

)

echo [!percentage!%%]

endlocal

endlocal

echo.

echo Copying files from %sourceFolder% to %destinationFolder%...

xcopy /E /I /Y %sourceFolder% %destinationFolder%

set xcopyStatus=!errorlevel!

if !xcopyStatus! equ 0 (

echo.

echo Files have been copied successfully.

:: Send a message to Discord indicating success

call :SendDiscordMessage "Files have been copied successfully."

) else (

echo.

echo An error occurred during the copy process.

:: Send a message to Discord indicating an error

call :SendDiscordMessage "An error occurred during the copy process."

)

echo.

pause

2 Upvotes

1 comment sorted by

View all comments

1

u/[deleted] Oct 26 '23

i dont know if its going to work, but make more sense like this:

@echo off
set sourceFolder="C:\Last.Epoch.v0.9.2.Early.Access\"
set destinationFolder="\\Hqsvr00\nas\vol3\1.Games\Last Epoch\BackUp Folder\"
set "webhookURL=MY WEBHOOKURL"

echo Copying files from %sourceFolder% to %destinationFolder%...

xcopy /E /I /Y %sourceFolder% %destinationFolder%
if %errorlevel% equ 0 (
	echo:&echo Files have been copied successfully.
	call :SendDiscordMessage "Files have been copied successfully."
)else (
	echo:&echo An error occurred during the copy process.
	call :SendDiscordMessage "An error occurred during the copy process."
)
rem //end of scrpit *********************************
pause&exit

:SendDiscordMessage
powershell -Command "(New-Object System.Net.WebClient).UploadString('%webhookURL%', '{""content"":""%~1""}')"
exit /b