Skip to content

Instantly share code, notes, and snippets.

@janvhs
Created March 22, 2023 08:34
Show Gist options
  • Save janvhs/d507a238449375052d281bd76f9732f7 to your computer and use it in GitHub Desktop.
Save janvhs/d507a238449375052d281bd76f9732f7 to your computer and use it in GitHub Desktop.
Recycle File Winows
const std = @import("std");
const windows = std.os.windows;
/// Do a file operation using the Windows API.
/// It can, copy, move, rename, or delete a file or folder.
/// Since Windows Vista, it is recommended to use IFileOperation instead.
///
/// References:
/// - https://docs.rs/windows-sys/0.45.0/windows_sys/Win32/UI/Shell/fn.SHFileOperationA.html
/// - https://learn.microsoft.com/en-US/windows/win32/api/shellapi/nf-shellapi-shfileoperationa
/// - https://learn.microsoft.com/en-US/windows/win32/api/shobjidl_core/nn-shobjidl_core-ifileoperation
extern "shell32" fn SHFileOperationA(
lpFileOp: *SHFILEOPSTRUCTA,
) callconv(windows.WINAPI) i32;
/// Arguments for the SHFileOperationA function.
/// Attention: pFrom and pTo have to be absolute paths!
///
/// References:
/// - https://docs.rs/windows-sys/0.45.0/windows_sys/Win32/UI/Shell/struct.SHFILEOPSTRUCTA.html
/// - https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileopstructa
const SHFILEOPSTRUCTA = extern struct {
hwnd: ?windows.HWND,
wFunc: u32,
pFrom: ?[*]const windows.CHAR,
pTo: ?[*]const windows.CHAR,
fFlags: u16,
fAnyOperationsAborted: windows.BOOL,
hNameMappings: ?windows.LPVOID,
lpszProgressTitle: ?[*]const windows.CHAR,
};
/// Delete the file or folder.
///
/// References:
/// - https://docs.rs/windows-sys/0.45.0/windows_sys/Win32/UI/Shell/constant.FO_DELETE.html
/// - https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileopstructa#members
const FO_DELETE: u32 = 0x3;
/// Allow undoing of the operation.
///
/// References:
/// - https://docs.rs/windows-sys/0.45.0/windows_sys/Win32/UI/Shell/constant.FOF_ALLOWUNDO.html
/// - https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileopstructa#members
const FOF_ALLOWUNDO: u16 = 0x40;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment