I'm looking to make an exe or batch file that will delete a folder, however the folder location changes based on the User, so I'd like it to determine this based on what user is currently logged in, or by asking the person running the exe/batch to type in the user name so it knows where to look.

The folder I'd like to have deleted is in the example below, where "PC" is the Login name that needs to match the current user or allow it to be entered.

C:\Users\PC.digilabs\ubuildabook\data

Thanks

Dani AI

Generated

Good call from on using the current user’s profile location and from on the missing backslash — that fixed the path confusion reported by . For reliability and a safer workflow, prefer a small PowerShell script instead of a plain batch RD call: PowerShell handles quoting, recursion and force deletes more predictably, and it has a built‑in dry‑run option.

$target = Join-Path $env:USERPROFILE ".digilabs\ubuildabook\data"
if (Test-Path $target) {
  Remove-Item -LiteralPath $target -Recurse -Force -ErrorAction Stop
} else {
  Write-Output "Not found: $target"
}

Add -WhatIf to the Remove-Item line when testing so the command only simulates the deletion. When bundling into a batch wrapper or an EXE, call the PowerShell script rather than re-implementing the delete logic in plain CMD.

Common pitfalls and fixes: paths with spaces must be quoted (PowerShell’s Join-Path avoids manual quoting). If deletion fails due to permissions, run the process elevated or take ownership/adjust ACLs before retrying. If a file is locked by another process, identify the handle with Sysinternals tools and stop the locking process. Dot-prefixed folder names are valid on Windows and are not magically hidden by the shell; confirm the exact folder name before deleting. On older Windows builds, very long paths may require alternate handling.

Recommended routine: run the script in dry‑run mode first, confirm the exact resolved path, then run the real delete. For deployment as an EXE, package the signed PowerShell script or a small signed wrapper so execution policies and UAC are handled predictably.

Recommended Answers

All 4 Replies

C:\Users\PC.digilabs\ubuildabook\data

For some reason it's not showing the "\" between PC and .digilabs

For some reason it's not showing the "\" between PC and .digilabs

Directories are seperated by the backslash. The path you are showing indicates that PC.digilabs is the name of the folder.

Use the USERNAME environment variables. On my machine with my current account it is

USERPROFILE=C:\Users\Jim

You can see the resulting command by

echo rd %USERPROFILE%\.digilabs\ubuildabook\data

If you use two backslashes before the "." when you post you get

C:\Users\PC\.digilabs\ubuildabook\data

Thanks for the info on both why my post was missing the \ and about the RD command.

The only change I had to make was adding /s/q after RD so it would remove all the files it contains without asking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.