Solved

batch to move folders based on folder name

Posted on 2013-11-11
2
770 Views
Last Modified: 2013-11-11
Hi I'm after a bit of batch scripting help..

I would like a script where that can move every single folder (that's got 2 more characters in it's name) into a folder that begins with the first letter of it's name.

EG.

J Bloggs
Joe Bloggs
Jane Bloggs
JA Bloggs

Will all be moved to a folder
J

If any folders already exist, it should overwrite the existing data.

Any help would be greatly appreciated.
0
Comment
Question by:antonioking
2 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39639500
That should do it; the script is currently in test mode and will only display the "move" commands it would normally run. Remove the uppercase ECHO in line 11 to run it for real:
@echo off
setlocal enabledelayedexpansion
REM The folder where the user folders are:
set UserFolder=C:\Temp
REM the folder in which the "Initial" folders will be created: 
set TargetRoot=D:\Temp
for /d %%a in ("%UserFolder%\*.*") do (
	echo Processing %%a ...
	set TargetFolder=%%~nxa
	set TargetFolder=%TargetRoot%\!TargetFolder:~0,1!
	ECHO move "%%~a" "!TargetFolder!"
)

Open in new window

0
 

Author Closing Comment

by:antonioking
ID: 39639556
Works perfectly! Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
USB backup batch file help needed. 9 99
Issue with cURL on Windows 7 96
bash script to unzip a file 5 54
Command prompt output to variable? 9 47
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

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