Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-26
4
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

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 85

Accepted Solution

by:
oBdA earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Configuring Remote Assistance for use with SCCM
Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

610 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