Solved

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

Posted on 2013-11-26
4
369 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now