fup and d
This is a pair of Windows Batch scripts that was initially created with the intention of uploading to and downloading from a fixed FTP address with pre-written sign-in credentials—based on a quick conceit that was originally conceived as a way of passing small files back and forth through a private FTP server. They were later both added with a switch to directly delete from the fixed FTP server, and the latter was then also subsequently expanded its download function to accept more general URLs.
To use this batch, the following parts of the script must be properly filled out first:
- In line 18 of fup.bat and line 35 of d.bat, the part ‘…
set "u=https://your.file-HTTP.com/%%a"’ shall be filled with your actual HTTP base link to your file, which may or may not be the same as the FTP address to where you upload your file based on your server-side settings; and - In lines 20 and 22 of fup.bat and line 96 of d.bat, you’ll need to change the part ‘
ftp://your.file-FTP.com/ --user USERNAME:PASSWORD’ to fill in your real FTP address and your sign-in credentials; and - the character count in line 96 of d.bat obviously needs to be changed accordingly to be consistent with your HTTP base link.
Then, simply use fup "file-to-upload.ext" to upload the file to the fixed FTP and get an HTTPS link (needs server-side support) to the file both on screen and in the clipboard, or use d "https://some.domain.here/link-to-the-file.ext" to download the file from the given link.
When uploading to the FTP using fup, the filename must be kept simple, preferably only consisting of Basic Latin letters ([A-z]) and digits ([0-9]) (with hyphens [-] and/or underscores [_] being used to separate words). Although it can process filenames with CJK characters located within the same code page as the system’s Locale (for reasons I cannot fully understand, chcp 65001 is not effective here), it might cause difficulties for the recipient while downloading. Similarly, files with spaces in their filenames can be successfully uploaded (and then be downloaded by the recipient with the auto-generated links); it is strongly discouraged to do so as files uploaded with spaces in names cannot be deleted from the FTP server with curl -Q. Additionally, filenames consisting of apostrophes (') will always fail to upload.
When downloading files using d, by default, the script will download the file from the given link to the current folder with the name in the link. But on many an occasion, the name in the link might have been per cent–encoded. To address this, a switch -n is created and can therefore be passed to the script to ‘normalise’ the name—it probably would be more appropriate to go by ‘decode’, and yet the -d switch has already been taken for ‘delete’.
Use -p to direct the location for the file to be saved (while still using the default name, whether normalised or not, which is controlled by the -n switch), and use -o to designate the path and the filename. These two switches should not be used together, but if they were to be used together, the script should normally still work. If a backtick (`) is detected after the -p switch, whether it is located at the beginning of the string after -p or whether it is the exact content of the entire string, it will be replaced with the default location for downloads (here in my case is D:\Omnigena\). If a backtick (`) is detected after the -o switch and if no -p switch is passed to the script, then the backtick at the beginning of the string after -o, if exists, will be replaced with the default location for downloads, with all the rest backticks (if exist) in the string being replaced with the normalised default name, with two more compound keywords for replacement are responsive: `b stands for the basename of the normalised default name and `x stands for the extension of the normalised default name. If -p and its value string are also present and are passed together with -o, then the backtick at the beginning of the string after -o (if exists) will not be parsed as the default location for downloads; rather, it will be replaced with the normalised default name, just as the other backticks (if exist) in the rest of the string.
Pass -d to either fup or d will all perform the same task—delete the file from the fixed FTP server. But for fup, the name of the file to be deleted from the FTP server must be passed as is (wrapping it in dumb double quotes if it contains space or spaces of course); it cannot accept links or per cent–encoded names; while the d deals with this more flexibly by allowing links. The reason for this difference and the reason why I added a -d switch to fup—a batch script that is created for the uploading of files, is that I can now comfortably press F3 to summon my last command and add a simple -d to it and then press RETURN to delete the file once the recipient finishes the downloading.
The -d switch for d obviously only works for those links derived from the fixed FTP server—after all, we cannot delete files from places over which we have no control. Additionally, if the name in the link consists of apostrophes ('), then it will always fail to download, too. The current script cannot address this problem.
A pair of untested Grok-translated Linux bash scripts that should function the same as these two Windows batches is enclosed at the end (months later, their zsh versions are also added). As explained in 7pk, what a backtick (`) represents in the Windows batches is represented by an equals sign (=) in the Linux bash scripts. And the Linux version of d diverts the default download path to simply the user’s download folder.
The batches
| These two batches use curl, which has only been shipped natively since Windows 10 1803; for earlier versions of Windows—as early as Windows Vista—you’ll have to download curl and put it into %PATH% first. |
@echo off
chcp 65001 >nul
setlocal
set "d="
set "i="
set "n="
set "u="
:a
if /i "%~1"=="" goto p
if /i "%~1"=="-d" (
set "d=1"
) else (
if not defined i set "i=%~1"
)
shift
goto a
:p
if not defined i goto e
for %%a in ("%i%") do set "n=%%~nxa"
for /f "delims=" %%a in ('powershell -NoProfile -Command "$e = [System.Uri]::EscapeDataString('%n%'); $e -replace '\(', '%%28' -replace '\)', '%%29'"') do set "u=https://your.file-HTTP.com/%%a"
if "%d%"=="1" (
curl -Q "DELE %i%" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
) else (
curl -T "%i%" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
if %errorlevel% equ 0 (
powershell -c "Set-Clipboard -Value '%u%'"
echo.
echo Done!
echo The following link has already been copied to the clipboard:
echo %u%
) else (
echo.
echo Something happened. The upload has FAILED.
)
)
:e
endlocal
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
set "a="
set "b="
set "d="
set "f="
set "i="
set "n="
set "o="
set "p="
set "x="
:a
if /i "%~1"=="" goto p
set "a=%~1"
if /i "!a:~0,1!"=="-" (
if /i "%a%"=="-d" set "d=1"
if /i "%a%"=="-n" set "n=1"
if /i "%a%"=="-o" set "o=%~2" & shift
if /i "%a%"=="-p" set "p=%~2" & shift
) else (
if not defined i set "i=%~1"
)
shift
goto a
:p
if not defined i goto e
echo %i% | find "/" >nul
if not errorlevel 1 (
echo %i% | findstr /i "http:// https:// ftp:// ftps://" >nul
if errorlevel 1 (
set "i=https://%i%"
)
) else (
for /f "delims=" %%a in ('powershell -NoProfile -Command "$e = [System.Uri]::EscapeDataString('%i%'); $e -replace '\(', '%%28' -replace '\)', '%%29'"') do set "i=https://your.file-HTTP.com/%%a"
)
for /f "delims=" %%b in ("%i%") do set "f=%%~nxb"
for /f "delims=" %%c in ('powershell -NoProfile -Command "[System.Uri]::UnescapeDataString('%f%')"') do set "f=%%c" & set "b=%%~nc" & set "x=%%~xc"
if defined p (
if /i "%p%"=="`" (
set "p=D:\Omnigena"
) else (
if /i "%p:~0,1%"=="`" (
set "p=!p:~1!"
set "p=D:\Omnigena!p!"
)
)
)
if defined o (
if not defined p (
if /i "%o%"=="`" (
set "o=D:\Omnigena\%f%"
) else (
if /i "%o:~0,2%"=="`\" (
set "o=!o:~1!"
set "o=D:\Omnigena!o!"
set "o=!o:`b=%b%!"
set "o=!o:`x=%x%!"
set "o=!o:`=%f%!"
)
)
) else (
set "o=!o:`b=%b%!"
set "o=!o:`x=%x%!"
set "o=!o:`=%f%!"
)
)
if not defined o (
if not defined p (
if /i "%n%"=="1" (
curl -L "%i%" -o "%f%" --output-dir .
goto d
) else (
curl -L "%i%" -O --output-dir .
goto d
)
) else (
if /i "%n%"=="1" (
curl -L "%i%" -o "%f%" --output-dir "%p%" --create-dirs
goto d
) else (
curl -L "%i%" -O --output-dir "%p%" --create-dirs
goto d
)
)
) else (
if not defined p (
curl -L "%i%" -o "%o%" --create-dirs
goto d
) else (
curl -L "%i%" -o "%o%" --output-dir "%p%" --create-dirs
goto d
)
)
:d
if /i "%i:~0,27%"=="https://your.file-HTTP.com/" if "%d%"=="1" curl -Q "DELE %f%" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
:e
endlocal
The bash scripts
The following bash script requires xclip, which can be installed with sudo apt install xclip on Debian-based Linux distros if not already pre-installed. |
#!/bin/bash
d=""
i=""
u=""
while [ "$#" -gt 0 ]; do
if [ "$1" = "-d" ]; then
d="1"
elif [ -z "$i" ]; then
i="$1"
fi
shift
done
if [ -z "$i" ]; then
exit 0
fi
u="https://your.file-HTTP.com/$(printf '%s' "$(basename "$i")" | perl -pe 's/([^a-zA-Z0-9_.!-])/sprintf("%%%02X", ord($1))/ge' | sed 's/(/%28/g; s/)/%29/g')"
if [ "$d" = "1" ]; then
curl -Q "DELE $i" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
else
curl -T "$i" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
if [ $? -eq 0 ]; then
echo "$u" | xclip -selection clipboard
echo
echo "Done!"
echo "The following link has already been copied to the clipboard:"
echo "$u"
else
echo
echo "Something happened. The upload has FAILED."
fi
fi
#!/bin/bash
a=""
b=""
d=""
f=""
i=""
n=""
o=""
p=""
x=""
while [ "$#" -gt 0 ]; do
a="$1"
case "$a" in
-d) d="1" ;;
-n) n="1" ;;
-o) o="$2"; shift ;;
-p) p="$2"; shift ;;
*)
if [ -z "$i" ]; then
i="$1"
fi
;;
esac
shift
done
if [ -z "$i" ]; then
exit 0
fi
if echo "$i" | grep -q "/"; then
if ! echo "$i" | grep -qi "^http://\|^https://\|^ftp://\|^ftps://"; then
i="https://$i"
fi
else
i="https://your.file-HTTP.com/$(printf '%s' "$i" | perl -pe 's/([^a-zA-Z0-9_.!-])/sprintf("%%%02X", ord($1))/ge' | sed 's/(/%28/g; s/)/%29/g')"
fi
f=$(basename "$i")
f=$(printf '%b' "$(echo "$f" | sed 's/%/\\x/g')")
b="${f%.*}"
x=".${f##*.}"
if [ -n "$p" ]; then
if [ "$p" = "=" ]; then
p="$HOME/Downloads"
elif [ "${p:0:1}" = "=" ]; then
p="${p:1}"
p="$HOME/Downloads$p"
fi
fi
if [ -n "$o" ]; then
if [ -z "$p" ]; then
if [ "$o" = "=" ]; then
o="$HOME/Downloads/$f"
elif [ "${o:0:2}" = "=/" ]; then
o="${o:1}"
o="$HOME/Downloads$o"
o="${o//=b/$b}"
o="${o//=x/$x}"
o="${o//=/$f}"
fi
else
o="${o//=b/$b}"
o="${o//=x/$x}"
o="${o//=/$f}"
fi
fi
if [ -z "$o" ]; then
if [ -z "$p" ]; then
if [ "$n" = "1" ]; then
curl -L "$i" -o "$f"
else
curl -L "$i" -O
fi
else
if [ "$n" = "1" ]; then
curl -L "$i" -o "$f" --output-dir "$p" --create-dirs
else
curl -L "$i" -O --output-dir "$p" --create-dirs
fi
fi
else
if [ -z "$p" ]; then
curl -L "$i" -o "$o" --create-dirs
else
curl -L "$i" -o "$o" --output-dir "$p" --create-dirs
fi
fi
if [ "${i:0:27}" = "https://your.file-HTTP.com/" ] && [ "$d" = "1" ]; then
curl -Q "DELE $f" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
fi
And a zsh diversion
(Translated by Grok, not fully tested, should be fine.)
#!/bin/zsh
d=""
i=""
u=""
while [[ $# -gt 0 ]]; do
if [[ "$1" = "-d" ]]; then
d="1"
elif [[ -z "$i" ]]; then
i="$1"
fi
shift
done
if [[ -z "$i" ]]; then
exit 0
fi
u="https://your.file-HTTP.com/$(printf '%s' "$i" | perl -pe 's/([^a-zA-Z0-9_.!-])/sprintf("%%%02X", ord($1))/ge' | sed 's/(/%28/g; s/)/%29/g')"
if [[ "$d" = "1" ]]; then
curl -Q "DELE $i" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
else
curl -T "$i" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
if [[ $? -eq 0 ]]; then
echo "$u" | pbcopy
echo
echo "Done!"
echo "The following link has already been copied to the clipboard:"
echo "$u"
else
echo
echo "Something happened. The upload has FAILED."
fi
fi
#!/bin/zsh
a=""
b=""
d=""
f=""
i=""
n=""
o=""
p=""
x=""
while [[ $# -gt 0 ]]; do
a="$1"
case "$a" in
-d) d="1" ;;
-n) n="1" ;;
-o) o="$2"; shift ;;
-p) p="$2"; shift ;;
*)
if [[ -z "$i" ]]; then
i="$1"
fi
;;
esac
shift
done
if [[ -z "$i" ]]; then
exit 0
fi
if echo "$i" | grep -q "/"; then
if ! echo "$i" | grep -qi "^http://\|^https://\|^ftp://\|^ftps://"; then
i="https://$i"
fi
else
i="https://your.file-HTTP.com/$(printf '%s' "$i" | perl -pe 's/([^a-zA-Z0-9_.!-])/sprintf("%%%02X", ord($1))/ge' | sed 's/(/%28/g; s/)/%29/g')"
fi
f=$(basename "$i")
f=$(printf '%b' "$(echo "$f" | sed 's/%/\\x/g')")
b="${f%.*}"
x=".${f##*.}"
if [[ -n "$p" ]]; then
if [[ "$p" = "=" ]]; then
p="$HOME/Downloads"
elif [[ "${p:0:1}" = "=" ]]; then
p="${p:1}"
p="$HOME/Downloads$p"
fi
fi
if [[ -n "$o" ]]; then
if [[ -z "$p" ]]; then
if [[ "$o" = "=" ]]; then
o="$HOME/Downloads/$f"
elif [[ "${o:0:2}" = "=/" ]]; then
o="${o:1}"
o="$HOME/Downloads$o"
o="${o//=b/$b}"
o="${o//=x/$x}"
o="${o//=/$f}"
fi
else
o="${o//=b/$b}"
o="${o//=x/$x}"
o="${o//=/$f}"
fi
fi
if [[ -z "$o" ]]; then
if [[ -z "$p" ]]; then
if [[ "$n" = "1" ]]; then
curl -L "$i" -o "$f"
else
curl -L "$i" -O
fi
else
if [[ "$n" = "1" ]]; then
curl -L "$i" -o "$f" --output-dir "$p" --create-dirs
else
curl -L "$i" -O --output-dir "$p" --create-dirs
fi
fi
else
if [[ -z "$p" ]]; then
curl -L "$i" -o "$o" --create-dirs
else
curl -L "$i" -o "$o" --output-dir "$p" --create-dirs
fi
fi
if [[ "${i:0:22}" = "https://your.file-HTTP.com/" ]] && [[ "$d" = "1" ]]; then
curl -Q "DELE $f" ftp://your.file-FTP.com/ --user USERNAME:PASSWORD
fi