Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

696 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