create folder based on the end of the filename

We have a lot of files that have various dates appended to them. For Example, File_LocB_101108.txt. I want to archive those files based upon the last 6 characters this is always a date. I found this code which it seems will work but how do you get the last six characters instead of the first five??

@echo off
setlocal
set startDir=.
if not "%~1"=="" set startDir=%~1
pushd "%startDir%"
for /f "tokens=*" %%a in ('dir /b /a-d 2^>NUL') do call :PROCESS "%%a"
popd
goto :EOF
:PROCESS
set fileName=%~n1
set dirName=%fileName:~0,5%
if not exist "%dirName%" echo Creating %dirName%&mkdir "%dirName%"
(echo Moving %~1 to %dirName%)
move "%~1" "%dirName%">NUL



earngreenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

exx1976Commented:
This should do what you want.  I haven't tested it, so give it a shot with a small test group of files.  You'll need to modify lines 2 and 3 with the source folder and the root of the folder where you want the date folders stored.


Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder("My Folder")
TargetDirectory = "My Target"
For Each File In oFolder
	name = Split(file.name,".")
	FN = Right(name(0),6)
         destFolder = TargetDirectory & "\" & FN
	If Not oFS.FolderExists(destFolder) Then oFS.CreateFolder(destFolder)
         destFile = TargetDirectory & "\" & FN & "\" & file.name
	oFS.MoveFile file.path, destFile
Next

Open in new window

0
AmazingTechCommented:
Last 6 is -6.
@echo off
setlocal
set startDir=.
if not "%~1"=="" set startDir=%~1
pushd "%startDir%"
for /f "tokens=*" %%a in ('dir /b /a-d 2^>NUL') do call :PROCESS "%%a"
popd
goto :EOF
:PROCESS
set fileName=%~n1
set dirName=%fileName:~-6%
if not exist "%dirName%" echo Creating %dirName%&mkdir "%dirName%"
(echo Moving %~1 to %dirName%)
move "%~1" "%dirName%">NUL

Open in new window

0
joshlunsfordCommented:
the -10 is the starting location in the variable - means count from the end of the variable
6 is how many characters to include
set file=File_LocB_101108.txt
set folder=%file:~-10,6%
echo %folder%

Open in new window

0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

earngreenAuthor Commented:


This part of your code errors because there is no variable assigned to name. Sorry it has been a while since I coded anything. Can you help? Also I have one more thing how would I check to see if there are any files to be processed.

name = Split(file.name,".")
0
exx1976Commented:
Huh?  Can you please post the error?   name IS a variable, it's the array that's produced by the split function..

To check to see if there are files to be processed?  I'm not sure what you mean.  You want the script to run continually, or just run at an interval?  If an interval, then put it in Scheduled Tasks.  If continually. then use the code below.  It'll check every 10 seconds for new files, and just run forever.  This is dangerous, though, because you could run into open file issues with the process that is putting those files in that directory..  Use with caution..
Do
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder("My Folder")
TargetDirectory = "My Target"
Set oFiles = oFolder.Files
If oFiles.Count > 0 Then
For Each File In oFolder
	name = Split(file.name,".")
	FN = Right(name(0),6)
         destFolder = TargetDirectory & "\" & FN
	If Not oFS.FolderExists(destFolder) Then oFS.CreateFolder(destFolder)
         destFile = TargetDirectory & "\" & FN & "\" & file.name
	oFS.MoveFile file.path, destFile
Next
End If
wscript.sleep 1000
Loop While 1 = 1

Open in new window

0
earngreenAuthor Commented:
The error that I am getting is that "Object doesn't support this property or method"
0
exx1976Commented:
On what line?  Please paste the line that is generating that error.

Thanks!
0
earngreenAuthor Commented:
name = Split(file.name,".")
0
exx1976Commented:
Sorry about that, Try this.
Do
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder("My Folder")
TargetDirectory = "My Target"
Set oFiles = oFolder.Files
If oFiles.Count > 0 Then
For Each File In oFiles
         name = Split(file.name,".")
	FN = Right(name(0),6)
         destFolder = TargetDirectory & "\" & FN
	If Not oFS.FolderExists(destFolder) Then oFS.CreateFolder(destFolder)
         destFile = TargetDirectory & "\" & FN & "\" & file.name
	oFS.MoveFile file.path, destFile
Next
End If
wscript.sleep 1000
Loop While 1 = 1

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
AmazingTechCommented:
wscript.sleep 1000 Is 1 second. wscript.sleep 10000 is 10 seconds
0
exx1976Commented:
Yes, AmazingTech is right, that last wscript.sleep should read 10000 not 1000..  Sorry for the typo
0
jvprofitCommented:
Thanks exx1976, you have saved me from many hours of copying files to individual folders.
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
Windows Batch

From novice to tech pro — start learning today.