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
>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.
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")
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....@gmail.com> wrote in message
> 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
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.
> 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.
> You're entirely correct: It is not VB Script. However, it is not DOS either > but a batch file.
A "batch" file in what language? A powershell file? An updated DOS that still uses .bat extension?
Frankly I don't care what it is. It's not VBScript and this is a VBScript group. I have a lot of experience with VBScript, I'm here in the VBS group, and I only vaguely understand the code you posted. I have no idea what file extension it should have, or what dependencies it has. And if I look up your code in the WSH Scripting help file I don't see it. If I save it in a .vbs file and run it it doesn't work.
Yet you posted it here with no explanation.
I don't understand why you feel a need to systematically post in the wrong group. But more than that, I don't understand why you don't at least explain what you're posting. Bals appears to be a new scripter. Imagine if you were new to C++, asked a question in a C++ group, and got a code sample in Java with no explanation. Don't you think that might be awfully confusing and time-wasting for you?
How can you think that your DOS-esque batch code is more relevant in a VBS group than, say, javascript or perl? (At least those languages are well suited to *Windows* scripting. But they're still completely OT.)
If you'd at least explain it clearly then I wouldn't have to, and then you won't need to get annoyed that I call it DOS. I'm just asking that you have some regard for what the OP actually needs. (Sticking to on-topic responses would be very nice, too, but that's probably asking too much. You seem oddly intent on selling DOS-esque to anyone and everyone, without regard to common courtesies or newsgroup rules.)
>> You're entirely correct: It is not VB Script. However, it is not DOS > either but a batch file.
> A "batch" file in what language? A powershell > file? An updated DOS that still uses .bat extension?
> Frankly I don't care what it is. It's not VBScript > and this is a VBScript group. I have a lot of experience > with VBScript, I'm here in the VBS group, > and I only vaguely understand the code you posted. > I have no idea what file extension it should have, or > what dependencies it has. And if I look up your code > in the WSH Scripting help file I don't see it. If I save > it in a .vbs file and run it it doesn't work.
*** Have you thought of broadening your horizon a little?
> Yet you posted it here with no explanation.
> I don't understand why you feel a need to > systematically post in the wrong group. But > more than that, I don't understand why you > don't at least explain what you're posting.
*** I occasionally post batch files here and I occasionally *** post VB Script files in a batch group. The reason is *** that sometimes batch files are more effective than *** VB Script files and sometimes it's the other way *** round. This is one of them: The OP wants the first *** few files moved, where "first" means by file name *** (which he said but which you appear to have missed). *** In a batch file a file sort is effected with a simple *** switch. In VB Script you have to write a sort routine. *** *** If the OP would like an explanation of the code then *** I'll gladly give it to him.
> Bals appears to be a new scripter. Imagine > if you were new to C++, asked a question in > a C++ group, and got a code sample in Java with > no explanation. Don't you think that might be > awfully confusing and time-wasting for you?
*** You're clutching at straws. Batch files will run on any *** Windows machine. They are universal. For Bals you *** probably need to install some interpreter.
> How can you think that your DOS-esque batch > code is more relevant in a VBS group than, say, > javascript or perl?
*** I don't but I think it's a very effective solution in this *** particular case. And it does not like an add-on *** interpreter like perl does.
> (At least those languages are > well suited to *Windows* scripting. But they're > still completely OT.)
> If you'd at least explain it clearly then I wouldn't > have to, and then you won't need to get annoyed > that I call it DOS.
*** I'm not getting annoyed - I simply and factually pointed out *** that in current usage nmy file is a batch file, not a DOS file. *** In view your lengthy response I suspect that it's you who's *** getting rather rattled about this minor issue.
> I'm just asking that you have some > regard for what the OP actually needs.
*** He got a solution that is universal and that works.
> (Sticking to on-topic responses would be very nice, > too, but that's probably asking too much. You seem > oddly intent on selling DOS-esque to anyone and > everyone, without regard to common courtesies or > newsgroup rules.)
*** I'm pragmatic and I use the solution that I consider *** most effective. Look at the size of your code after *** you have included the sort routine, then compare *** it with the batch file. The batch file will be much tighter, *** yet perfectly understandable if you have a basic knowledge *** of console command (with the exception of one line of code). *** Same for the respective code explanations. The fact is that *** when it comes to copying or moving files, there is a lot of very *** powerful stuff built into standard console commands. With VB *** Scripts you often have to write your own code to achieve the *** same aim. I call it "re-inventing the wheel".
*** Sometimes posters ask specifically for a VB Script solution, *** e.g. as a learning experience. In those cases I agree with you: *** It would be totally inappropriate to give them a batch answer.
> 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)
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.
>> 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)
> 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.
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.msdos.batch.nt/browse_thread/threa...
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
> 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
Very nice dialog so far - I learnt a few things.
Now, for moving first 5 files (sorted by name order) from C:/ sIncomingFolder to C:/sBulkFolder, here is a script - Err, I wrote a more general script - it moves n files from infolder to outfolder.
# Script MoveNFiles.txt # Input arguments var str infolder, outfolder var int n # Get list of all *.txt files, sorted by name var str list ; lf -n "*.txt" $infolder > $list # Get first n files one by one and move them var int count while ($count < $n) do # Get the next top file from the list var str file ; lex "1" $list > $file # It is possible there may not be n files in the list. # In that case, $file will be empty. if ($file <> "") do # Move file. system move $file $outfolder done # Increment $count set $count = $count + 1 done
Script is in biterscripting. Save it in file C:/Scripts/ MoveNFiles.txt. Call it as follows.
> Script is in biterscripting. Save it in file C:/Scripts/ > MoveNFiles.txt. Call it as follows.
And apparently one has to pay for a 3rd-party library to use this biterscripting? I would be a bit leery of using a Windows scripting tool written by people who think Windows uses Unix syntax in file paths.
But your post has cheered me up, anyway. It makes NT Command Script posts seem relevant and useful by comparison. :)
>> Script is in biterscripting. Save it in file C:/Scripts/ >> MoveNFiles.txt. Call it as follows.
> And apparently one has to pay for a 3rd-party > library to use this biterscripting? I would be > a bit leery of using a Windows scripting tool > written by people who think Windows uses Unix > syntax in file paths.
> But your post has cheered me up, anyway. > It makes NT Command Script posts seem > relevant and useful by comparison. :)
>>> You're entirely correct: It is not VB Script. However, it is not DOS >> either but a batch file.
>> A "batch" file in what language? A powershell >> file? An updated DOS that still uses .bat extension?
>> Frankly I don't care what it is. It's not VBScript >> and this is a VBScript group. I have a lot of experience >> with VBScript, I'm here in the VBS group, >> and I only vaguely understand the code you posted. >> I have no idea what file extension it should have, or >> what dependencies it has. And if I look up your code >> in the WSH Scripting help file I don't see it. If I save >> it in a .vbs file and run it it doesn't work. > *** Have you thought of broadening your horizon a little?
>> Yet you posted it here with no explanation.
>> I don't understand why you feel a need to >> systematically post in the wrong group. But >> more than that, I don't understand why you >> don't at least explain what you're posting. > *** I occasionally post batch files here and I occasionally > *** post VB Script files in a batch group. The reason is > *** that sometimes batch files are more effective than > *** VB Script files and sometimes it's the other way > *** round.
This may be true, however, I agree with mayayana that, when posting a solution based on a language other than the one on which this newsgroup is based, you should put your OT response in a proper context. And this is even more true when dealing with newbies, some of whom I have seen trying to run provided batch solutions in .vbs files.
> *** This is one of them: The OP wants the first > *** few files moved, where "first" means by file name > *** (which he said but which you appear to have missed). > *** In a batch file a file sort is effected with a simple > *** switch. In VB Script you have to write a sort routine.
True enough. But I disagree with your interpretation of what the OP actually wanted.
> *** If the OP would like an explanation of the code then > *** I'll gladly give it to him.
>> Bals appears to be a new scripter. Imagine >> if you were new to C++, asked a question in >> a C++ group, and got a code sample in Java with >> no explanation. Don't you think that might be >> awfully confusing and time-wasting for you? > *** You're clutching at straws.
No he's not, what he says makes perfect sense to me.
> *** Batch files will run on any > *** Windows machine. They are universal. For Bals you > *** probably need to install some interpreter.
Bals is the OP, not some alternative scripting language. Bals asked for a solution in a vbscript group. I assume that he is more interested in learning how to do this in vbscript than in getting a solution handed to him in some other language. In that scenario, no optional interpreter installation would be required.
And although batch files do run on all windows versions, the batch language itself differs from windows version to windows version, to the extent that a batch file that works on, say, NT2000, could easily fail to give the same results on a Windows 98 system.
At the very least, I think it fair to assume that someone posting in a vbscript group is using an operating system where that is applicable.
>> How can you think that your DOS-esque batch >> code is more relevant in a VBS group than, say, >> javascript or perl? > *** I don't but I think it's a very effective solution in this > *** particular case. And it does not like an add-on > *** interpreter like perl does.
If you don't think your batch contribution is more relevant here than a vbscript one, why insist on posting it here?
>> (At least those languages are >> well suited to *Windows* scripting. But they're >> still completely OT.)
>> If you'd at least explain it clearly then I wouldn't >> have to, and then you won't need to get annoyed >> that I call it DOS. > *** I'm not getting annoyed - I simply and factually pointed out > *** that in current usage nmy file is a batch file, not a DOS file. > *** In view your lengthy response I suspect that it's you who's > *** getting rather rattled about this minor issue.
Who is more rattled is not the issue. The issue is OT posting.
>> I'm just asking that you have some >> regard for what the OP actually needs. > *** He got a solution that is universal and that works.
Correction. You provided a solution that you think he wants. His next response would seem to indicate more of an interest in learning how to do this in vbscript.
>> (Sticking to on-topic responses would be very nice, >> too, but that's probably asking too much. You seem >> oddly intent on selling DOS-esque to anyone and >> everyone, without regard to common courtesies or >> newsgroup rules.) > *** I'm pragmatic and I use the solution that I consider > *** most effective. Look at the size of your code after > *** you have included the sort routine, then compare > *** it with the batch file. The batch file will be much tighter, > *** yet perfectly understandable if you have a basic knowledge > *** of console command (with the exception of one line of code). > *** Same for the respective code explanations. The fact is that > *** when it comes to copying or moving files, there is a lot of very > *** powerful stuff built into standard console commands. With VB > *** Scripts you often have to write your own code to achieve the > *** same aim. I call it "re-inventing the wheel".
I use batch and vbscript. A comparison of their relative merits is generally OT in a newsgroup devoted to one of them, except when that has been specifically asked for.
> *** Sometimes posters ask specifically for a VB Script solution, > *** e.g. as a learning experience. In those cases I agree with you: > *** It would be totally inappropriate to give them a batch answer.
I would propose that the default solution language here should be assumed to be vbscript, and that alternate solutions should be provided only in those cases where the OP specifically states that he is interested.
> > It might be useful to give a summary of this intriguing thread in which > Bals > > asked for a sample script to move some files.
> > Pegasus supplied a 17-line batch file solution even though this is a VB > > Script forum.
> > Mayana attacked Pegasus for posting a batch file in a VB Script group, > then > > supplied a 54-line VB Script solution.
> > Richard Williams proposed a 22-line solution that requires the > installation > > of biterscripting.
> > Al Dunbar did not supply any solution but attacked Pegasus for posting > batch > > files in a VB Script newsgroup.
> > Bals acknowledged the help he had received.
> That would be par for the course. > You learnt well grasshopper.
> -- > Todd Vargo > (Post questions to group only. Remove "z" to email personal messages)
Very interesting topic - does anyone know why "For each folderIdx In files" doesnt process files in alphabetical order? What order does it process them in?
> Very interesting topic - does anyone know why "For each folderIdx In > files" doesnt process files in alphabetical order? > What order does it process them in?
Yes, the FOR command processes files in the order in which they are stored. To force processing in alphabetical order, you can use the extended /F feature of FOR command to process a DIR/A-D/ON directory listing.
FOR /f "delims=" %%g in ('DIR/A-D/ON/B') do echo %%g
See the FOR/? and DIR/? help for more information.
-- Todd Vargo (Post questions to group only. Remove "z" to email personal messages)
>> Very interesting topic - does anyone know why "For each folderIdx In >> files" doesnt process files in alphabetical order? >> What order does it process them in?
> Yes, the FOR command processes files in the order in which they are > stored. > To force processing in alphabetical order, you can use the extended /F > feature of FOR command to process a DIR/A-D/ON directory listing.
> FOR /f "delims=" %%g in ('DIR/A-D/ON/B') do echo %%g
> See the FOR/? and DIR/? help for more information.
> -- > Todd Vargo > (Post questions to group only. Remove "z" to email personal messages)
I thought that this was a VB Script group, that "bigroy" asked a VB Script question and that "for each" requires not a switch but a separate sort process? :-)
Very interesting topic - does anyone know why "For each folderIdx In files" doesnt process files in alphabetical order? What order does it process them in?
It's a collection. I think they're listed in order by date, but I'm not certain. It's returning them the way the file system sees them.
> >> Very interesting topic - does anyone know why "For each folderIdx In > >> files" doesnt process files in alphabetical order? > >> What order does it process them in?
> > Yes, the FOR command processes files in the order in which they are > > stored. > > To force processing in alphabetical order, you can use the extended /F > > feature of FOR command to process a DIR/A-D/ON directory listing.
> > FOR /f "delims=" %%g in ('DIR/A-D/ON/B') do echo %%g
> > See the FOR/? and DIR/? help for more information.
> I thought that this was a VB Script group, that "bigroy" asked a VB Script > question and that "for each" requires not a switch but a separate sort > process? :-)
But I though this was a post whatever code you want thread. ;-)
BTW, as long as we are discussing posting habits, when did it become customary to quote sigs when responding? :-O
-- Todd Vargo (Post questions to group only. Remove "z" to email personal messages)
>> >> Very interesting topic - does anyone know why "For each folderIdx In >> >> files" doesnt process files in alphabetical order? >> >> What order does it process them in?
>> > Yes, the FOR command processes files in the order in which they are >> > stored. >> > To force processing in alphabetical order, you can use the extended /F >> > feature of FOR command to process a DIR/A-D/ON directory listing.
>> > FOR /f "delims=" %%g in ('DIR/A-D/ON/B') do echo %%g
>> > See the FOR/? and DIR/? help for more information.
>> I thought that this was a VB Script group, that "bigroy" asked a VB >> Script >> question and that "for each" requires not a switch but a separate sort >> process? :-)
> But I though this was a post whatever code you want thread. ;-)
> BTW, as long as we are discussing posting habits, when did it become > customary to quote sigs when responding? :-O
> -- > Todd Vargo > (Post questions to group only. Remove "z" to email personal messages)