Pergunta

I'm trying to build out a script that will pull the list of users, grab each of their names and then do a directory list for each of their appdata folders.

So far I'm thinking that I could just use the net user > C:\userlist.txt to create the list of users, my real problem comes in grabbing the user's names and putting them into the next command.

By using a variable of some kind I would like to have findstr pull in each user so that I could get a list of all the users's directories.

The end result would look something like this:

dir C:\users\$variableforusers\AppData\Roaming > C:\directorylist.txt

I'd like to be able to get this command to run for each users appdata directory, so this command would need to repeat itself based on how many users the end machine has.

This will be running from the local system account so %appdata% will not work as it only gives you the currently logged in user's appdata folder.

If possible, I'd like for the directory list to all be dumped into one file, otherwise I'd have to introduce a variable to check multiple files for the results.

Thanks ahead of time for the help guys!

Foi útil?

Solução

I think you should not use user's name to obtain a path to user's profile directory. The directory name can be different than user's name - for example in case user name hsa been changed after the profile was created or if there is a user name conflict (a local user has the same username as a domain user).

You can do a dir of AppData\Roaming for each subdirectory of C:\Users using the following syntax:

for /d %A in (C:\Users\*) do if exist %A\AppData\Roaming dir %A\AppData\Roaming

But again this solution will omit any user profiles that are stored outside C:\Users.

To do a dir of AppData\Roaming directory of each user profile, you can get the paths of user profiles from registry:

for /f "tokens=3" %A in ('reg query "HKLM\Software\Microsoft\WindowsN T\CurrentVersion\ProfileList" /s /v ProfileImagePath ^| find "REG_EXPAND_SZ"') do @dir %A\AppData\Roaming

If you want to use these commands in a script, use double percent signs (%%A). To learn more about for command type for /?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top