Skip to content

Instantly share code, notes, and snippets.

@uriel1998
Created November 16, 2012 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uriel1998/4083393 to your computer and use it in GitHub Desktop.
Save uriel1998/4083393 to your computer and use it in GitHub Desktop.
Create a time/date stamped version of any inputted filename; bash and windows batch file versions. Moved to https://github.com/uriel1998/versionscripts
@echo off
REM This is the Windows batch file version of this script for NT/XP and later.
REM
REM By Steven Saus
REM Licensed under a Creative Commons BY-SA 3.0 Unported license
REM To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
REM
REM Creates time/date stamped version of file for collaborative work.
REM
REM Typically run with the filename (full path not needed, but will work with) as
REM the first argument from the commandline.
REM If run with no arguments (or argument that's not /z, /w, or a filename), gives usage
REM
REM ######################Graphical UI instructions#############################################
REM Please note that I have NOT tested Zenity/Wenity in a Windows environment.
REM I've used the example files from the programs to create this, though.
REM You do not need to alter the variables below if the files are in your path or you're just
REM using the command line interface.
REM
REM If Zenity is installed/desired, /z should be the first argument.
REM You should edit this file and put the path to zenity.exe in the line below!
set ZENITYPATH=c:\PATH\TO\zenity.exe
REM Get Zenity here:
REM Windows port of Zenity: http://www.placella.com/software/zenity/
REM
REM Get Wenity here:
REM http://kksw.zzl.org/wenity.html
REM If Wenity (a Java Zenity clone) is installed/desired, /w should be the first argument.
REM You should edit this file and put the path to the wenity.jar file in the line below!
set WENITYPATH=c:\PATH\TO\wenity.jar
REM Please remember that Java must be installed properly and found by your path to use Wenity.
REM You can specify the path to java.exe on the line below.
set JAVAEXE=c:\PATH\TO\java.exe
REM
REM
REM
REM datestamp from http://www.intelliadmin.com/index.php/2007/02/create-a-date-and-time-stamp-in-your-batch-files/
REM filename stripping from http://stackoverflow.com/questions/1472191/getting-the-file-name-without-extension-in-a-windows-batch-script
REM variable path stuff from http://stackoverflow.com/questions/654152/batch-files-get-absolute-path
REM and also from http://blogs.msdn.com/b/ben/archive/2007/03/09/path-manipulation-in-a-batch-file.aspx
REM timestamp from http://www.pcreview.co.uk/forums/batch-file-timestamp-t1466494.html
REM and http://justgeeks.blogspot.com/2008/07/getting-formatted-date-into-variable-in.html
REM argument catching from http://www.techsupportforum.com/forums/f10/check-existence-of-file-or-folder-in-batch-54164.html
REM setting var from output from http://www.tomshardware.com/forum/230090-45-windows-batch-file-output-program-variable
REM
REM Revision history
REM 20121116: uriel1998: Error checking around java, wenity, and zenity
REM 20121116: uriel1998: Use temp file to avoid environment expansion limitations
REM 20121115: uriel1998: Added code to use wenity
REM 20121115: uriel1998: Added code to use zenity
REM 20121115: uriel1998: Original code
REM initialization
set ORIGFILE=""
set ODRIVE=""
set OPATH=""
set ONAME=""
set OEXT=""
if EXIST %CD%v.tmp del %CD%v.tmp
REM setting time stamp properly so we have padding for hours
set /a UHH=%TIME:~0,2%
set OHH=0%UHH%
set HH=%OHH:~-2%
SET TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%HH%%time:~3,2%%time:~6,2%
REM echo %TIMESTAMP%
REM test if argument passed
if [%1] == [] (goto usage)
IF [%1]==[/w] (goto testjava)
if [%1]==[/W] (goto testjava)
if [%1]==[/z] (goto zenity)
if [%1]==[/Z] (goto zenity)
REM default, run from commandline
REM writing this to a temp file to get around variable expansion limitations
echo %1 > %CD%v.tmp
goto usage
:testzenity
REM Check if user set it in file correctly already
if EXIST %ZENITYPATH% (goto :zenity)
for %%i in (zenity.exe) DO (
set ZENITYEXE=%%~$PATH:i
)
REM If not set in file correctly, we will end up with null for ZENITYPATH
if [%ZENITYPATH%]==[] (goto :nogui)
:zenity
REM The Windows port of Zenity writes output to a tempfile
%ZENITYEXE% --file-selection > %CD%v.tmp
goto usage
:testjava
if EXIST %JAVAEXE% (goto :wenity)
for %%i in (java.exe) DO (
set JAVAEXE=%%~$PATH:i
)
if EXIST %JAVAEXE% (goto :testwenity)
@echo.
@echo Java not found in path. Please ensure you have Java installed correctly to use Wenity.
goto :explain
:testwenity
REM check if user set it in file correctly
if EXIST %WENITYPATH% (goto :wenity)
for %%i in (wenity.jar) DO (
set WENITYPATH=%%~$PATH:i
)
REM If not set in file correctly, we will end up with null for WENITYPATH
if [%WENITYPATH%]==[] (goto :nogui)
:nogui
@echo.
@echo Wenity or Zenity not found in path. Please put in path or edit script with
@echo full pathname to Wenity or Zenity.
goto :explain
:wenity
%JAVAEXE% -jar %wenitypath% -d fileSelector "Please select a file to version"
if %ERRORLEVEL%==0 (
copy %CD%wenity_response.txt %CD%v.tmp
del %CD%wenity_response.txt
) else (
rem this can also be an error, but that check is omitted
echo File selector is cancelled by user.
goto :explain
)
:usage
REM Extract file information and slice it into bits.
if EXIST %CD%v.tmp (
for /f %%i in (%CD%v.tmp) DO (
set ORIGFILE=%%~fi
set ODRIVE=%%~di
set OPATH=%%~pi
set ONAME=%%~ni
set OEXT=%%~xi
)
)
if EXIST %CD%v.tmp (del %CD%v.tmp)
REM test if argument is file, otherwise give help script
if EXIST %ORIGFILE% goto doit
:explain
@echo.
@echo This script copies the input filename with a time date stamp
@echo Usage: version.bat [filename OR /z OR /w]
@echo Use the /z option instead of a filename to use Zenity to choose the file
@echo Use the /w option instead of a filename to use Wenity to choose the file
@echo.
@echo Example:
@echo Running "version.bat myfile.txt" right now would result in you having
@echo myfile.txt and myfile_%TIMESTAMP%.txt
@echo in the same directory.
goto :eof
REM requirements fulfilled, perform actions
:doit
set STAMPFILE=%ODRIVE%%OPATH%%ONAME%_%TIMESTAMP%%OEXT%
echo Copying %ORIGFILE% to %STAMPFILE%
copy %ORIGFILE% %STAMPFILE%
:eof
#!/bin/bash
#
# This is the bash version of this script for *nix and Mac.
#
# By Steven Saus
# Licensed under a Creative Commons BY-SA 3.0 Unported license
# To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
#
# Creates time/date stamped version of file for collaborative work.
#
# Typically run with the filename (full path not needed, but will work with) as
# the first argument.
# If run with no arguments (or argument that's not -z or a filename), gives usage
#
# Optional -s switch to work on symlinks - it works on the LINKED file, not the link.
#
# Tested with bash; should work with zsh (for you OSX folks)
# If you are using a different shell than bash, you'll probably need to change
# the "/bin/bash" to your shell interpreter.
# To know what shell you're using, type
#
# echo $SHELL
#
# in your terminal.
#
######################Graphical UI instructions#############################################
#
# You do not need to alter the variables below if the files are in your path or you're just
# using the command line interface.
#
# If Zenity is installed/desired, -z should be the first argument.
#
# Get Zenity here:
# Zenity: https://live.gnome.org/Zenity
# For OS X: With MacPorts: sudo port install zenity
#
# If Wenity (java Zenity clone) is installed/desired, -w should be the first argument.
# The script tries to find it if it's executable and in your path, but you
# SHOULD edit this file and put the path to the wenity.jar file in the line below!
wenitypath="/PATH/TO/wenity.jar"
# Get Wenity here:
# http://kksw.zzl.org/wenity.html
#
# You can specify which java executable to use here
javapath="/PATH/TO/java"
#
# realpath answer from
# http://stackoverflow.com/questions/3915040/bash-fish-command-to-print-absolute-path-to-a-file
#
# Revision history
# 20121116: uriel1998: Error checking around java, wenity, and zenity
# 20121115: uriel1998: Added code to use wenity
# 20121115: uriel1998: Added code to use zenity
# 20121115: uriel1998: Original code
time=`date +_%Y%m%d_%H%M%S`
# avoiding goto statements or duplication of text here
usage=0
# test for arguments
if [ "#$" > 0 ]; then
# test our commandline arguments
case "$1" in
"-z" | "-Z" )
# Use zenity test (there will be no filename, so it's the first argument)
zenitypath=$(which zenity)
if [ -f "$zenitypath" ]; then
origfile=$(`$zenitypath --file-selection`)
else
echo "Zenity either not executable or not found in path."
fi
;;
"-w" | "-W" )
# doublechecking if executable and in path if not specified by user above
if [ ! -f "$wenitypath" ]; then
wenitypath=$(which wenity.jar)
fi
# ensuring java is present and in path if not specified by user above
if [ ! -f "$javapath" ]; then
javapath=$(which java)
fi
if [ -f "$wenitypath" -a -f "$javapath" ]; then
"$javapath" -jar "$wenitypath" -d fileselector "Please select an existing file"
if [ "$?" -eq 0 ]; then
# wenity_response is the tempfile output, in the $PWD.
origfile=$(cat $PWD/wenity_response.txt)
rm $PWD/wenity_response.txt
else
#file not selected, so setting this so it fails.
origfile=""
fi
else
echo ""
echo "Wenity or java not found properly. Please ensure you have java installed and"
echo "wenity either executable and in your path or edit the script with its location."
echo "Java path=$javapath Wenity path=$wenitypath"
fi
;;
*)
#used from the commandline
origfile="$1"
esac
# test if we have a file of some type
if [ -f "$origfile" ]; then
# Tests if not a symlink OR the -s flag is used
if [ ! -h "$origfile" -o "$2" = "-s" ]; then
fullpath=$(realpath "$origfile")
dir=$(dirname $fullpath)
filename=$(basename $fullpath)
ext=${filename##*.}
file=${filename%.*}
newname=$(echo "$dir/$file$time.$ext")
echo "Copying $fullpath to $newname"
cp $fullpath $newname
else
echo "Symlink detected and following symlinks not enabled."
usage=1
fi
else
usage=1
fi
else
# no arguments
usage=1
fi
# This is separate because I didn't want to write it as a function so others
# could easily learn from and modify this script.
if [ $usage = 1 ];then
echo " "
echo "This script copies the input filename with a time date stamp"
echo "Usage: version.sh [filename | -z | -w] [-s]"
echo "Use the -z option instead of a filename to use Zenity to choose the file"
echo "Use the -w option instead of a filename to use Wenity to choose the file"
echo "Use the -s option if you want it to follow a symlink, otherwise symlinks are ignored."
echo "Example:"
echo "Running \"bash version.sh $PWD/myfile.txt\" right now would result in"
newname=$(echo "$PWD/myfile$time.txt")
echo "$PWD/myfile.txt and $newname"
echo "in the same directory."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment