Solved

Script for directory creation

Posted on 2014-01-08
7
302 Views
Last Modified: 2014-01-08
Hi There!

I need to create our 2014 folders in 2 folders of every client in our client folder.

The directory structure is h:\clients\alphabet number (A, B, C etc)\client name\correspondence and create 2014 at this level

Second Dir Structure is the same but under h:\clients\alphabet number (A, B, C etc)\client name\documentation and create 2014 at this level.

I guess the logic would be, go through directory structure and when you find documentation or correspondence in the directory name, create a 2014 folder under it.

Ideally it might create a list of the folders it had created in a log and where so if it goes wrong we can reverse it :)
0
Comment
Question by:networkn
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39766394
Plain batch; it's currently in test mode and will only display the "md" commands it would normally run. Remove the uppercase ECHO in line 14 to run it for real (and delete the log file after the trial runs):
@echo off
setlocal enabledelayedexpansion
set SourceFolder=H:\Clients
set LogFile=Folders2014.log
set TargetFolderList="Correspondence" "Documentation"
set AddFolder=2014
for /d %%a in ("%SourceFolder%\*.*") do (
	echo Processing '%%~nxa' ...
	for /d %%c in ("%%a\*.*") do (
		echo   Processing '%%~nxc' ...
		for %%f in (%TargetFolderList%) do (
			if exist "%%c\%%~f" (
				if not exist "%%c\%%~f\%AddFolder%" (
					ECHO md "%%c\%%~f\%AddFolder%"
					if not errorlevel 1 >>"%LogFile%" echo "%%c\%%~f\%AddFolder%"
				)
			)
		)
	)
)
echo Done.

Open in new window

0
 

Author Comment

by:networkn
ID: 39766544
Doesn't seem to recognise the second folder for some reason? Correspondence was created not documentation
0
 
LVL 83

Expert Comment

by:oBdA
ID: 39766559
Works fine here; do the "Documentation" folders exist already? The "2014" folders will only be created if the target folders exist already.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:networkn
ID: 39766574
Ugh, my fault, misread the email I was sent. All good thanks.
0
 

Author Closing Comment

by:networkn
ID: 39766676
Awesome!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39766736
A little simpler would be perhaps to use a specific dir command then use that to make the dirs, does this give the right dirs for you?

dir /b /ad /s *correspondence* *documentation*

If so then

@echo off
for /f "tokens=*" %%d in ('dir /b /ad /s *correspondence* *documentation*') do md "%%~d\2014" 2>NUL

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39766740
I delayed posting there seeing only the original post of course, would have built on above to log but you have solution anyway it seems.

Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

19 Experts available now in Live!

Get 1:1 Help Now