Solved

Script for directory creation

Posted on 2014-01-08
7
307 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article will help you understand what HashTables are and how to use them in PowerShell.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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