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 n files sort by Filename

Reply
 
Old 2 Weeks Ago   #1 (permalink)
Bals


 
 

Move n files sort by Filename

I have many files in a folder - I need to move only first 5 files
another folder sort by filename
ie in Source folder i have
1Filename.txt
2Filename.txt
3Filename.txt
4Filename.txt
5Filename.txt
6Filename.txt
7Filename.txt
8Filename.txt
9Filename.txt

I need to move only FIRST 5 files sort by Filename to destination
folder
1Filename.txt
2Filename.txt
3Filename.txt
4Filename.txt
5Filename.txt

Please give me a sample script for this

My System SpecsSystem Spec
Old 2 Weeks Ago   #2 (permalink)
Pegasus [MVP]


 
 

Re: Move n files sort by Filename


"Bals" <balamurugan.mr.kvp@newsgroup> wrote in message
news:b7cdd336-ef14-42bb-8cc5-979ecd986b49@newsgroup
Quote:

>I have many files in a folder - I need to move only first 5 files
> another folder sort by filename
> ie in Source folder i have
> 1Filename.txt
> 2Filename.txt
> 3Filename.txt
> 4Filename.txt
> 5Filename.txt
> 6Filename.txt
> 7Filename.txt
> 8Filename.txt
> 9Filename.txt
>
> I need to move only FIRST 5 files sort by Filename to destination
> folder
> 1Filename.txt
> 2Filename.txt
> 3Filename.txt
> 4Filename.txt
> 5Filename.txt
>
> Please give me a sample script for this
You could use this simple batch file:
@echo off
SetLocal EnableDelayedExpansion
set Source=d:\Source Folder
set Target=d:\Target Folder
set count=0
if not exist "%Source%" (
echo Cannot find the folder "%Source%"
pause
goto :eof
)
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('dir /on /b /a-d "%Source%"') do (
echo Moving "%Source%\%%a"
rem move /y "%Source%\%%a" "%Target%"
set /a count=!count! + 1
if !count! GEQ 5 goto :eof
)

Remove "rem" in the fourth line from the bottom to activate the batch file.


My System SpecsSystem Spec
Old 2 Weeks Ago   #3 (permalink)
mayayana


 
 

Re: Move n files sort by Filename

You didn't explain what makes those files "first".
If they're recognizable by name then you can just
use MoveFile. Are you familiar with the FileSystemObject?
It provides a number of file handling functions.
First create the object:

Set FSO = CreateObject("Scripting.FileSystemObject")

' The MoveFile method is like this:

FSO.MoveFile "C:\here\file1.txt", "C:\there\file1.txt"

However, MoveFile is prone will error out if the
desitnation file exists, so it's a good idea to do:

If FSO.FileExists("C:\there\file1.txt") Then FSO.DeleteFile
"C:\there\file1.txt", True

FSO.MoveFile "C:\here\file1.txt", "C:\there\file1.txt"

You can find all of this in the Windows Script
documentation.
If you need to first find the files that's a different
matter, but from your post it sounds like that's not
an issue.

--
--
Bals <balamurugan.mr.kvp@newsgroup> wrote in message
news:b7cdd336-ef14-42bb-8cc5-979ecd986b49@newsgroup
Quote:

> I have many files in a folder - I need to move only first 5 files
> another folder sort by filename
> ie in Source folder i have
> 1Filename.txt
> 2Filename.txt
> 3Filename.txt
> 4Filename.txt
> 5Filename.txt
> 6Filename.txt
> 7Filename.txt
> 8Filename.txt
> 9Filename.txt
>
> I need to move only FIRST 5 files sort by Filename to destination
> folder
> 1Filename.txt
> 2Filename.txt
> 3Filename.txt
> 4Filename.txt
> 5Filename.txt
>
> Please give me a sample script for this

My System SpecsSystem Spec
Old 2 Weeks Ago   #4 (permalink)
mayayana


 
 

Re: Move n files sort by Filename

Also, if you're new to this and trying to learn
VBScript, you need to understand that the code
from Pegasus is not VBScript. Apparently his code
will work, but it's actually DOS, which is an
entirely different thing.




My System SpecsSystem Spec
Old 2 Weeks Ago   #5 (permalink)
Pegasus [MVP]


 
 

Re: Move n files sort by Filename


"mayayana" <mayaXXyana@newsgroup> wrote in message
news:uxrJlQvXKHA.1236@newsgroup
Quote:

> Also, if you're new to this and trying to learn
> VBScript, you need to understand that the code
> from Pegasus is not VBScript. Apparently his code
> will work, but it's actually DOS, which is an
> entirely different thing.
You're entirely correct: It is not VB Script. However, it is not DOS either
but a batch file. DOS is a legacy operating system (not a scripting tool)
that does not exist under the current 32/64-bit operating systems - unless
you choose to call any character based black screen "DOS". With this
measure, some Unix/Linux screens would equally qualify as "DOS". Now before
you rub my nose in it, yes, I know, a number of Microsoft programmers are
not aware of the distinction either: Their published documentation refers to
all character-based black screens as "DOS screens". They're probably the old
breed.


My System SpecsSystem Spec
Old 2 Weeks Ago   #6 (permalink)
mayayana


 
 

Re: Move n files sort by Filename

Quote:

> You're entirely correct: It is not VB Script. However, it is not DOS
either
Quote:

> but a batch file. DOS is a legacy operating system (not a scripting tool)
I got curious about this and looked it
up, since the topic of exactly what "batch"
code is keeps coming up. Maybe this will
provide some clarification for anyone who's
interested:

In general, batch refers to pre-GUI scripting
(as opposed to *Windows* scripting). It is
command-line, run by the local console
interpreter, though it can also be written to files.
Thus "batch" -- running numerous commands
in series.

On Win9x it's DOS commands, run by command.com.

The posted code will not run on Win9x. It's limited
to NT. On NT the system of DOS commands was
expanded and updated, intrerpreted by cmd.exe
instead of command.com. The newer "DOS+" is
now known as "NT Command Script". It appears
to be a superset, including all of the DOS commands
with more commands added. There is a Microsoft
newsgroup here:

microsoft.public.win2000.cmdprompt.admin

NT Command Script code is apparently best
saved with .cmd extension when used from a file
because if saved as a .bat file it will be misinterpreted
on Win9x. But from what I read it appears that
either extension will -- on NT systems -- be run
by cmd.exe.

Reference:
http://en.wikipedia.org/wiki/Batch_file


My System SpecsSystem Spec
Old 2 Weeks Ago   #7 (permalink)
Pegasus [MVP]


 
 

Re: Move n files sort by Filename


"mayayana" <mayaXXyana@newsgroup> wrote in message
news:O7jQUOxXKHA.4816@newsgroup
Quote:

>
Quote:

>> You're entirely correct: It is not VB Script. However, it is not DOS
> either
Quote:

>> but a batch file. DOS is a legacy operating system (not a scripting tool)
>
> I got curious about this and looked it
> up, since the topic of exactly what "batch"
> code is keeps coming up. Maybe this will
> provide some clarification for anyone who's
> interested:
>
> In general, batch refers to pre-GUI scripting
> (as opposed to *Windows* scripting). It is
> command-line, run by the local console
> interpreter, though it can also be written to files.
> Thus "batch" -- running numerous commands
> in series.
>
> On Win9x it's DOS commands, run by command.com.
>
> The posted code will not run on Win9x. It's limited
> to NT. On NT the system of DOS commands was
> expanded and updated, intrerpreted by cmd.exe
> instead of command.com. The newer "DOS+" is
> now known as "NT Command Script". It appears
> to be a superset, including all of the DOS commands
> with more commands added. There is a Microsoft
> newsgroup here:
>
> microsoft.public.win2000.cmdprompt.admin
>
> NT Command Script code is apparently best
> saved with .cmd extension when used from a file
> because if saved as a .bat file it will be misinterpreted
> on Win9x. But from what I read it appears that
> either extension will -- on NT systems -- be run
> by cmd.exe.
>
> Reference:
> http://en.wikipedia.org/wiki/Batch_file
>
I completely agree with everything you say. Let me add one minor point: The
newsgroup you quote (microsoft.public.win2000.cmdprompt.admin) is a sleepy
hollow. Batch action is really here: alt.msdos.batch.nt. It is where I
occasionally post VB Script code, eg. this one where a batch file solution
would have been extremely slow and quite fragile. VB Script is an excellent
solution in this case.
http://groups.google.com/group/alt.m...7df42?lnk=raot


My System SpecsSystem Spec
Old 2 Weeks Ago   #8 (permalink)
Bals


 
 

Re: Move n files sort by Filename

Thanks Mayayana ,
Yeah I am new to VBScript , I know the basics n FSO on VBScript.
I can Use For each loop to move files .
All I want is to move First 5 files sort by filename


set fileSys=CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")
sIncomingFolder = "D:\Temp\BulkUpload\Incoming\"
sBulkFolder = "D:\Temp\BulkUpload\BulkUpload\"

Set folder = fso.GetFolder(sBulkFolder)
Set files = folder.Files
For each folderIdx In files
call fileSys.Movefile(sIncomingFolder &folderIdx.Name, sBulkFolder )
Exit For
End If



Say My sIncomingFolder has 9 files
HHHH.txt
JJJJJ.txt
AAAA.txt
BBBB.txt
CCCC.txt
DDDD.txt
EEEE.txt
FFFF.txt
GGG.txt


I need to move only 5 files sort by file name to sBulkFolder
AAAA.txt
BBBB.txt
CCCC.txt
DDDD.txt
EEEE.txt

My System SpecsSystem Spec
Old 2 Weeks Ago   #9 (permalink)
mayayana


 
 

Re: Move n files sort by Filename

OK, so you want to do it alphabetically.
I think if it were me I'd get the file names
into an array:

Dim AFils(), i2
i2 = 0
Set files = folder.Files
Redim AFils(files.count - 1)
For each folderIdx In files
AFils(i2) = folderIdx.Name
i2 = i2 + 1
next
Redim Preserve AFils(i2 - 1)
Set files = Nothing

' Then sort the array. (See QuickSort Sub below):

QuickSort AFils, 0, 0

' Now move the files:

If Right(sBulkFolder, 1) <> "\" then sBulkFolder = sBulkfolder & "\"

For i2 = 0 to 4
FSO.MoveFile sBulkFolder & AFils(i2), OtherPlace & AFils(i2)
Next

' The above is "air code" with no error trapping
' for things like an empty folder or an existing
' destination file.

' Here's the sorting routine. AIn is the array to
' sort. When the sub is finished the array is sorted.

Sub QuickSort(AIn, LBeg, LEnd)
Dim LBeg2, vMid, LEnd2, vSwap
If (LEnd = 0) Then LEnd = UBound(AIn)
LBeg2 = LBeg
LEnd2 = LEnd
vMid = UCase(AIn((LBeg + LEnd) \ 2))
Do
Do While UCase(AIn(LBeg2)) < vMid And LBeg2 < LEnd
LBeg2 = LBeg2 + 1
Loop
Do While vMid < UCase(AIn(LEnd2)) And LEnd2 > LBeg
LEnd2 = LEnd2 - 1
Loop
If LBeg2 <= LEnd2 Then
vSwap = AIn(LBeg2)
AIn(LBeg2) = AIn(LEnd2)
AIn(LEnd2) = vSwap
LBeg2 = LBeg2 + 1
LEnd2 = LEnd2 - 1
End If
Loop Until LBeg2 > LEnd2
If LBeg < LEnd2 Then QuickSort AIn, LBeg, LEnd2
If LBeg2 < LEnd Then QuickSort AIn, LBeg2, LEnd
End Sub





My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Sort files collection ? VB Script
deleting files where the filename is too long Vista file management
Move file to folder depending on last two characters of filename VB Script
How to rename files with brackets in the filename PowerShell
Vista Search doesn't find files by filename Vista file management


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