?
Solved

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

Posted on 2013-11-26
4
Medium Priority
?
379 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

765 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