Windows registry entry / Powershell / Copy file to subfolder (and append timestamp)

Hi there.

I have what I think will be a one-time need to code in Powershell. I hope someone here can help me with the last bit of code to make my dream come true.

It would help my daily work to have a simple (semi-)manual backup procedure that allows me to:
Right-click any file and click an option in the menu to make a copy of that file in a subfolder (named _backup) with a timestamp appended to the filename.

Helped by the rather old discussion here:
https://www.tenforums.com/general-support/166646-adding-date-name-files-folders-3.html

… I have managed to make the copy and append timestamp work with the following code imported as a .reg-file as a registry entry:
//START of CODE
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\CopyAppendDate]
@=“Copy and Append Date”

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\CopyAppendDate\Command]
@=“PowerShell.exe -WindowStyle Hidden -Command \”( Get-Item -LiteralPath ‘%1’ ) | %%{ Copy-Item -LiteralPath $.FullName -Destination ( ‘{0} {1:yyyy-MM-dd_HH.mm.ss}{2}’ -f $.BaseName, $.LastWriteTime, $.Extension ) }\“”
//END of CODE (a few \ is removed by the system here, but I hope it’s readable anyway)

I have tried adding .\_backup in various positions and with/without backslash, quotation marks, etc. but with no luck.

How do I refer to a specific subfolder in the code (and ideally create it, if it doesn’t exist already)?

Thanks!

hello,

  1. you should use foreach instead of %%
  2. everywhere you use $. , replace with $_.
  3. i recommand you to put commands in a script in a shared folder and call this script in the commandline in the registry. This is easyer to debug.
param( $file )
try {

	#-- get file information --
	$item = get-item -literalpath $file -ea stop

	$directory  = $item.directory
	$backuppath = "$directory\_backup"
	$date       = $item.lastwritetime.ToString('yyyy-MM-dd_HH.mm.ss')
	$destfile   = "$($backuppath)\$($item.basename) $($date)$($item.extension)"

	#-- create subfolder --
	if (-not (get-item -literalpath $backuppath -ea silentlycontinue) ) {
		$null = new-item -itemtype directory -path $backuppath -erroraction Stop
	}

	#-- copy file in backup folder --
	Copy-Item -LiteralPath $item.FullName -Destination $destfile
	write-host "file saved successfully" -foregroundcolor green
} catch {
	write-host "ERROR at line n°$($_.invocationinfo.scriptlinenumber). $($_.exception.message)" -foregroundcolor red
}

suppose you save the script in “c:\shared tools\backupfile.ps1”. Then, in the registry, you can have something simpler :
powershell -NoLogo -ExecutionPolicy Bypass -command “& ‘c:\shared tools\backupfile.ps1’ -file ‘%1’”
and this give the registry file :

Thanks a lot. It works. However, not until I manually copy the command line into the command field in the registry. Initially after importing, it’s empty (value not set).
Any simple explanation for that?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.