• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

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



0
earngreen
Asked:
earngreen
  • 5
  • 3
  • 2
  • +2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now