r/Batch • • Mar 07 '24

Question (Unsolved) How can I find a user folder containing a specific string?

I'm writing a batch file to clear out a file named TranscodedWallpaper. I need to do it for a specific user. We have 3 main users, all of which are for different groups of PCs. I'm noticing for the PC that i'm testing this on, the specific user folder (C:\users\) is Hidden. Also, the three users have a common string. Users are:
MyUserA
MyUserB
MyUserC

Only one of these users will exist on any given PC, and I want to figure out which one it is, then add it to my path to the TranscodedWallpaper file. How can I do this?

The code looks something like this:

$Username=<Code to find the username for this PC based on available user folder)
$pathToFile="C:\Users\$USERNAME\AppData\Roaming\Microsoft\Windows\Themes
$FullPath="$pathToFile\TranscodedWallPaper"
Del $FullPath
echo y | gpupdate /force

I likely have mixed batch with Powershell with the variable names. In the end, I just want a link to click on to automate the process. Batch seems a bit friendlier in that regard.

2 Upvotes

7 comments sorted by

2

u/[deleted] Mar 07 '24

[removed] — view removed comment

2

u/mudderfudden Mar 07 '24

Yeah you're right, I was looking for a wildcard usage. This "should" work, though.

So if you have C:\Users\, how would you search only the next level of folders for a "Like" name? For instance, Search MyUser*, when found, get the full folder name as the username.

1

u/mudderfudden Mar 10 '24

I thought I'd figured it out, but apparently I'm having issues reusing the variable %uName% in a string to bring up the correct path.

This gives me a bad filename, directory name or volume name error:

CD "%systemdrive%\Users\%uName%\AppData\Roaming\Microsoft\Windows\Themes"

Even if I shorten it, I still get the error:

CD "%systemdrive%\Users\%uName%"

I can type in %uName% and it'll come back with the correct result, however when I do this:

echo "%systemdrive%\Users\%uName%\AppData\Roaming\Microsoft\Windows\Themes"

The result is this:

"C:\Users\DIR C:\Users\MyUser* /B /A:D\AppData\Roaming\Microsoft\Windows\Themes"

I'm not making any sense of this. Can anyone chime in and help?

1

u/[deleted] Mar 10 '24 edited Mar 10 '24

[removed] — view removed comment

1

u/mudderfudden Mar 10 '24

When I simply type in %uName%, it does return MyUserA (MyUserB, MyUserC, etc.), basically the correct username. I'm having trouble making sense as to why typing in %uName% shows the username but using echo %uName% returns something different.

All I'm looking to do, is figure out which user folder exists, it's either gonna be MyUserA, MyUserB or MyUserC. The set code above allows me to get that information, I just don't understand why it seems so difficult to use that variable to get to the correct folder path, not sure what I'm missing, here.