Solved

Script for directory creation

Posted on 2014-01-08
7
300 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 82

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 82

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article will help you understand what HashTables are and how to use them in PowerShell.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

17 Experts available now in Live!

Get 1:1 Help Now