Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Move file to folder depending on last two characters of filename

Reply
 
Old 04-27-2009   #1 (permalink)
Oli M


 
 

Move file to folder depending on last two characters of filename

I need to write a script that looks through a folder and finds the last two
characters of every filename in that folder , and then moves them over to a
folder structure which corresponds to those last two digits. For example

I could have two files called
abcdefg23
qwerty56

I would then run the script and the two files would be placed in the
relevant folders

IE abcdefg23 would be placed in a folder called 23 and
qwerty56 would be placed in a folder called 56

There is no need to create the folders 23 and 56 as these would already
exist in a folder structure.

Anyone have any ideas of how I can script this rather than have to do this
manually as there are 100's of files I need to do this for.

Any help would be much appreciated.

Thanks

My System SpecsSystem Spec
Old 04-27-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Move file to folder depending on last two characters of filename


"Oli M" <OliM@xxxxxx> wrote in message
news:0FE663F5-9729-46E8-9E81-6FA7ED31B2B5@xxxxxx
Quote:

>I need to write a script that looks through a folder and finds the last two
> characters of every filename in that folder , and then moves them over to
> a
> folder structure which corresponds to those last two digits. For example
>
> I could have two files called
> abcdefg23
> qwerty56
>
> I would then run the script and the two files would be placed in the
> relevant folders
>
> IE abcdefg23 would be placed in a folder called 23 and
> qwerty56 would be placed in a folder called 56
>
> There is no need to create the folders 23 and 56 as these would already
> exist in a folder structure.
>
> Anyone have any ideas of how I can script this rather than have to do this
> manually as there are 100's of files I need to do this for.
>
> Any help would be much appreciated.
>
> Thanks
Here you go. I suspect, by the way, that your specification is inaccurate.
When you write that you have two files called
abcdefg23
qwerty56
then you probably mean something like this instead:
abcdefg23.mp3
qwerty56.mp3

I think it would be a useful exercise for you to adapt the code below so
that it can cope with the actual file names you have instead of the original
task specification that you posted.
sSource = "d:\Mon\"
sTarget = "d:\Tue\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oSource = oFSO.GetFolder(sSource)
For Each oFile In oSource.Files
sSuffix = Right(oFile.Name, 2) & "\"
oFSO.MoveFile oFile.Path, sTarget & sSuffix & oFile.Name
MsgBox "Moved " & oFile.Path & " to " & sTarget & sSuffix
Next

To test the code, do this:
1. Run the program.
2. Examine the message box that pops up.
3. Check if the program did what you expected.
4. Click OK.
5. Repeat Steps 2 to 4 a few times.
6. When you're satisified that everything is as it should be, remove the
MsgBox line from the code, then save the code and run it again.


My System SpecsSystem Spec
Old 04-28-2009   #3 (permalink)
Oli M


 
 

Re: Move file to folder depending on last two characters of filena

This code appears to error at line 7 character 1 path not found.

Not too sure about the syntax that is being used so unable to find out how
to resolve
My System SpecsSystem Spec
Old 04-28-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: Move file to folder depending on last two characters of filename


"Oli M" <OliM@xxxxxx> wrote in message
news:CEE4FE8A-F845-44F3-BF5E-042084B566F4@xxxxxx
Quote:

> This code appears to error at line 7 character 1 path not found.
>
> Not too sure about the syntax that is being used so unable to find out how
> to resolve
The message means that the folder or file you specified at the beginning of
the code do not exist, perhaps because of the file extension problem I
pointed out in my first reply. You must make sure that all names in the code
agree with the names that exist on the disk. The pop-up box I included gives
you the actual names - check them carefully!


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
I want to move my Offline File Folder Vista file management
VBScript to move file in one folder to many folder VB Script
Capture a string of characters and use as filename. PowerShell
Create New folder, move file, delete file. Explorer Freeze General Discussion
Creating folder based on filename of a file PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46