Difference between revisions of "CMD Examples"

From ProVide DocWiki
Jump to: navigation, search
Line 1: Line 1:
 
==Example 1==
 
==Example 1==
The script used for this example will automatically remove a file once it's downloaded from the server, the script below is written in cmd but can be done in other languages if you wish.
+
For this example we shall make the server deletes a file from the server once it has been downloaded by a  user.
 +
 
 +
The script we will be using for this example is the following:
  
 
<syntaxhighlight lang="powershell">
 
<syntaxhighlight lang="powershell">
 
@echo off
 
@echo off
  
 +
#  set the FILE to the first argument we provided the script with
 
set FILE=%1
 
set FILE=%1
  
//Remove quotes
+
# Remove quotes
 
for /f "useback tokens=*" %%a in ('%FILE%') do set FILE=%%~a
 
for /f "useback tokens=*" %%a in ('%FILE%') do set FILE=%%~a
  
//remove file
+
# Remove file
 
DEL "%FILE%"
 
DEL "%FILE%"
  
Line 17: Line 20:
  
  
To get this script to trigger on the right time, for this example we will use it to trigger when a file is downloaded by a user it will be deleted from the server.
 
  
First we need to go on the admin interface for our ProVide server and on the first tab (Server configuration) on the far top left, we need to go to "Events and Messages" in the menu underneth whe we are on the server configuration.
+
1. The first step once we've done the script is to first save it down to a '.cmd' file (name it something like OnDownloadEndScript to easily keep track of it) and save it in a script folder.
 +
 
 +
2. To make this script trigger after a file has been downloaded from the server we first need to go on the admin interface for our ProVide server and find "Events and Messages" under the first tab.
 +
 
 +
3. In the "Events and Messages" window we click on the drop-down menu and select "OnDownloadEnd".
 +
Everything in the message field will either be written to the server logg or we can execute scripts and even send arguments with it.
 +
 
 +
The text we will write in the message area of the "Events and Messages" window is the following:
 +
<source lang="html5">%EXECUTE("C:\PATH\TO\SCRIPT\OnDownloadEndScript.cmd" "%LOCAL_FILENAME%")%</source>
 +
 
 +
 
 +
This will make the script that we just made execute after a file is done downloading, it will also provide the "%LOCAL_FILENAME%" which is the full path to the file that was used in the download.
 +
 
 +
 
 +
 
 +
==Example 2==
 +
 
 +
For the next example we will make a  script that check if the file is 0 bytes and then deletes the file if it is 0 bytes.
 +
 
 +
The script we will be using for this example is the following:
 +
<syntaxhighlight lang="powershell">
 +
@echo on
 +
 
 +
#  set the FILE to the first argument we provided the script with
 +
set FILE=%1
 +
 
 +
# Remove quotes
 +
for /f "useback tokens=*" %%a in ('%FILE%') do set FILE=%%~a
 +
 
 +
# Checks if file is equal to 0 bytes and deletes if the statement is true.
 +
if %~z1 == 0 (DEL "%FILE%")
 +
 
 +
exit 0
 +
</syntaxhighlight>
 +
 
 +
 
 +
 
 +
Follow step 1 and 2 from the first example (just change the name to something more fitting like "OnUploadEndScript") and on the 3rd step change "OnDownloadEnd" to "OnUploadEnd" so it will trigger after a file has been uploaded so we can check what size it is and delete it if it's 0 bytes.
  
In the window that pops up we can select what event we wish to put this in, for now we will go with "OnDownloadEnd" that triggers when a download is completed.
 
  
It should already have some texts in the "Message" window but we can leave that for now as it does not effect the trigger for the script.
+
To make the script run we need to write the following in the "Events and Messages" message area:
So to trigger the script from this "Message" field we need to add the following to the "Message" field.
+
<source lang="html5">%EXECUTE("C:\PATH\TO\SCRIPT\OnUploadEndScript.cmd" "%LOCAL_FILENAME%")%</source>
  
<source lang="html5">%EXECUTE("C:\PATH\TO\SCRIPT\SCRIPT.cmd" "%LOCAL_FILENAME%")%</source>
 
  
This will make the script specified from the path in the first quotes and in the secound quotes is the full path to the file that was just downloaded.
+
Now the script will run after a user has uploaded a file and it will provide the %LOCAL_FILENAME% (the full path to the file uploaded) to the script so that we can check if it's 0 bytes in the script.

Revision as of 09:46, 20 June 2018

Example 1

For this example we shall make the server deletes a file from the server once it has been downloaded by a user.

The script we will be using for this example is the following:

@echo off

#  set the FILE to the first argument we provided the script with
set FILE=%1

# Remove quotes
for /f "useback tokens=*" %%a in ('%FILE%') do set FILE=%%~a

# Remove file
DEL "%FILE%"

exit 0


1. The first step once we've done the script is to first save it down to a '.cmd' file (name it something like OnDownloadEndScript to easily keep track of it) and save it in a script folder.

2. To make this script trigger after a file has been downloaded from the server we first need to go on the admin interface for our ProVide server and find "Events and Messages" under the first tab.

3. In the "Events and Messages" window we click on the drop-down menu and select "OnDownloadEnd". Everything in the message field will either be written to the server logg or we can execute scripts and even send arguments with it.

The text we will write in the message area of the "Events and Messages" window is the following:

%EXECUTE("C:\PATH\TO\SCRIPT\OnDownloadEndScript.cmd" "%LOCAL_FILENAME%")%


This will make the script that we just made execute after a file is done downloading, it will also provide the "%LOCAL_FILENAME%" which is the full path to the file that was used in the download.


Example 2

For the next example we will make a script that check if the file is 0 bytes and then deletes the file if it is 0 bytes.

The script we will be using for this example is the following:

@echo on

#  set the FILE to the first argument we provided the script with
set FILE=%1

# Remove quotes
for /f "useback tokens=*" %%a in ('%FILE%') do set FILE=%%~a

# Checks if file is equal to 0 bytes and deletes if the statement is true.
if %~z1 == 0 (DEL "%FILE%")

exit 0


Follow step 1 and 2 from the first example (just change the name to something more fitting like "OnUploadEndScript") and on the 3rd step change "OnDownloadEnd" to "OnUploadEnd" so it will trigger after a file has been uploaded so we can check what size it is and delete it if it's 0 bytes.


To make the script run we need to write the following in the "Events and Messages" message area:

%EXECUTE("C:\PATH\TO\SCRIPT\OnUploadEndScript.cmd" "%LOCAL_FILENAME%")%


Now the script will run after a user has uploaded a file and it will provide the %LOCAL_FILENAME% (the full path to the file uploaded) to the script so that we can check if it's 0 bytes in the script.