Solved

Batch File: Getting usernames from C:\Users with a specific suffix

Posted on 2013-11-26
4
371 Views
Last Modified: 2013-11-26
I have a directory structure on D: in WIndows Server 2008 R2 that is like this:
d:\MasterFiles\Users\<username>
Where <username> is  a subdirectory with a naming convention of <first initial><lastname>2013. (i.e. JSMITH2013)

I have user specific shortcuts in each users subdirectory (i.e. MyShortcut.lnk)  that I would like to place in their C:\User\<username> \desktop directory

There are users in the C:\User directory that do not have the "2013" suffix that should NOT get the shortcut.

How can I accomplish this in a .Bat file?

I was thinking of something like this but it returns the whole directory and doesn't look for the 2013:

for /d %%u in (C:\Users\*) do copy "D:\MasterFiles\Users\%%u\MyShortcut.lnk" "C:\Users\%%u\Desktop"

I cant seem to find the solution.
0
Comment
Question by:GNOVAK
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 39678262
Try
for /d %%u in ("C:\Users\*2013") do copy "D:\MasterFiles\Users\%%u\MyShortcut.lnk" "C:\Users\%%u\Desktop"

Open in new window

or
@echo off
setlocal enabledelayedexpansion
for /d %%u in ("C:\Users\*") do (
	set User=%%u
	if "!User:~-4!"=="2013" (
		copy "D:\MasterFiles\Users\%%u\MyShortcut.lnk" "C:\Users\%%u\Desktop"
	)
)

Open in new window

0
 

Author Comment

by:GNOVAK
ID: 39678386
While they filter out the correct "2013" user directories, the %%u is the complete directory.
So the result looks like:
D:\MasterFiles\Users\C:\Users\JSMITH2013\Desktop
                                       *****************

Anyway to get just the JSMITH2013 out and use just that?
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39678407
Sorry, should have thought of that. Use "%%~nxu" to access only the name and extension of the path in the loop variable:
for /d %%u in ("C:\Users\*2013") do copy "D:\MasterFiles\Users\%%~nxu\MyShortcut.lnk" "C:\Users\%%~nxu\Desktop"

Open in new window

or
@echo off
setlocal enabledelayedexpansion
for /d %%u in ("C:\Users\*") do (
	set User=%%nxu
	if "!User:~-4!"=="2013" (
		copy "D:\MasterFiles\Users\%%u\MyShortcut.lnk" "C:\Users\%%u\Desktop"
	)
)

Open in new window

0
 

Author Closing Comment

by:GNOVAK
ID: 39678477
Excellent - thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question