Scripting a move of folders that do not end with two digits

Can someone provide me with a script that will only move folders that "do not" end with two digits to another folder?  Thanks!
BasssqueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITCommented:
- Make a .bat file of this.
- Make a test folder named c:\testdir and c:\targetdir
- Copy some folders to c:\testdir. Be sure to include a mix folders that do and don't end with 2 digits.
- Run the .bat in a cmd window

for /d %%c in (c:\testdir\*) do (move "%%c" "c:\targetdir" | findstr /v /r "^.*[0-9][0-9]")

Open in new window

0
Jose TorresSenior SQL Server DBACommented:
Powershell

This should do the trick
# Move Folders without ## at end of name

# Set folder source
$FolderSource = 'C:\MoveFolderTest\SourceFolders'
# Set folder target
$FolderTarget = 'C:\MoveFolderTest\TargetFolder'
# Get list of all folders in source
$FolderList = Get-ChildItem $FolderSource | Where-Object {$_.Mode -like 'd*'}
# Go thru each folder
ForEach ($folder in $FolderList){
    # Get folder name
    $FolderName = $folder.Name
    # Get last two characters of folder name
    $FolderNameSub = $FolderName.Substring($FolderName.Length - 2, 2)
    # Test if last two characters of folder name is numeric
    If ($FolderNameSub -match '^\d+$'){
        # True - Do Nothing
    } Else {
        # False - Move folders
        $FolderMovePath = "$FolderSource\$FolderName"
        Move-Item -Path $FolderMovePath -Destination $FolderTarget
    }
}

Open in new window

0
BasssqueAuthor Commented:
That seems to be formatted to move files opposed to folders.
I changed to to what I thought may work, but it doesn't move anything.
for /f %%c in (c:\folders\*) do (move "%%c" "c:\ARCHIVE\" | findstr /vc:"^*[0-9][0-9]")
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

NVITCommented:
> That seems to be formatted to move files opposed to folders.
It works fine here. Apart from folder names, your code differs from my posted one. It should be:
for /d %%c in (c:\folders\*) do (move "%%c" "c:\ARCHIVE" | findstr /v /r "^.*[0-9][0-9]")

Open in new window

0
NVITCommented:
Basssque.

My apologies. You are correct. Here is the corrected code:

@echo off
setlocal enabledelayedexpansion
REM Move folders that "do not" end with two digits to another folder
for /d %%c in (c:\folders\*) do (
  echo "%%c" | findstr /v /r "^.*[0-9][0-9]"
  if !errorlevel! equ 0  move "%%c" "c:\ARCHIVE"
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
Started this a little earlier, but never got back to it.  Here's how I was going to approach it.

@echo off
setlocal

set BaseDir=c:\base
set DestDir=c:\dest

for /f "tokens=*" %%A in ('dir /ad /b "%BaseDir%"^| findstr /r /e /v /c:"[0-9][0-9]"') do move "%BaseDir%\%%A" "%DestDir%\"

Open in new window

~bp
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The posted PS approach is bloated:
$BaseDir = 'c:\base'
$DestDir = 'c:\dest'

Get-ChildItem $BaseDir -Directory -Include *[0-9][0-9] | move-item $DestDir

Open in new window

assuming we are talking about a search one level deep only (do not search in subfolders).
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Oops, typo. Of course it is -Exclude, not -Include.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.