Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

moving files in a directory 1

Status
Not open for further replies.

savvyuser

Technical User
Apr 26, 2004
50
PH
Hi Folks,

Please help me in making a code which could move a files in a directory. The situation is like this, a folder has 800 or more files in it. All files in the folder have only two types of filename extension, these are scn & adt. Each of every two filenames have the same name and date modified. I need to move these files to a certain directory by 25 pairs each (25 scn & 25 adt). Then, it would move another set of 25 to the next folder until no files in the source folder is left. So, each folder contains 50 files with every two pairs have the same filename (this is regardless of its file extension & size).

Any help for this is appreciated. Thanks in advance.
 
Hello savvyuser,

This is brut-force solution.
[1] Get the folder and enumerate the files in it.

[2] During the enumeration, do three things.
[2.1] Put all filename with extension scn in an array.
[2.2] At the same time, count .scn and .adt file number see if they equal.
[2.3] Also, at the time a .scn is found, check existence of it .adt.

If pass all tests in [2], you can start moving files. Any anomaly detected, such as .scn count not equal to .adt count or corresponding file not found, you stop.

[3] With the array of .scn file paths, start a for loop (from 0 to ubound of the array).
[3.1] Whenever (index+1) mod 25 = 0, you switch to next directory. The available directory name can be put in an array or with a definite naming convention and created along the way.
[3.2] Making sure you can ensure a successful move with same named file pair does not exist at the destination folder, else delete them beforehand. Also, it goes without saying that the destination must exist else be created.
[3.3] Move file operation is on the .scn filename array and at the same time also on the corresponding named .adt. This re-construction of filename with different extension can be done easily.

These are the steps you have to take.

regards - tsuji
 
Hi Tsuji,

Nice suggestion, however, there are more than 800 files in it, some of them are in ascii. So, it is difficult for me to declare the files in an array. Also, the number of files in the folder are uncertain. Sometimes I got 1000 and sometime it would generate either more than or below 1000. Is there another way? Actually, I started a code however I ceased on this statement objFileLoop.Move(objFileADT), "Destination" @ countFolder

 
savvyuser,

You do _not_ need, of course, actually enum explicitly the filenames. It is done with fso:
[tt] fso.getfolder(sourcefolderpath).files[/tt]
You just feed the file's path, in particular with extension .scn, to a dynamic array.

You need nonetheless to establish the target directories' naming convention or the name array itself.

The switching target directory criteria is when 25 file pairs are copy switch to next target directories. The [(index+1) mod 25 = 0] I mentioned may be [index mod 25 = 0] depending where you put the test.

- tsuji
 
Further notes:

This will prepare the ground for the [1] and [2].
Code:
ssrcdir="d:\test\abc"    '<<<your input

isrccount=0
iadtcount=0
bpairing=true
dim ascn()
redim ascn(-1)
set fso=createobject("scripting.filesystemobject")
for each ofile in fso.getfolder(ssrcdir)
    if lcase(fso.getextensionname(ofile.path))="scn" then
        isrccount=isrccount+1
        if not fso.fileexists(fso.getbasename(ofile.path) & ".adt") then
            bpairing=false
            exit for
        end if
        redim preserve ascn(ubound(ascn)+1)
        ascn(ubound(ascn))=ofile.path
    end if
    if lcase(fso.getextensionname(ofile.path))="adt" then
        iadtcount=iadtcount+1
        if not fso.fileexists(fso.getbasename(ofile.path)) & ".scn") then
            bpairing=false
            exit for
        end if
    end if
next
if not bpairing then
    wscript.echo "Files are not paired up properly." & _
        vbcrlf & "Operation aborted."
    set fso=nothing
    wscript.quit (9)
end if

'Since the checking existence of mirror file is done, iadtcount should be equal to iscncount. No need to further check.
'Here you have an array of .scn file path with 100% certain of the existence of the corresponding .adt file
'You can now loop through ascn() index for copying.
'... etc
- tsuji
 
Hi Tsuji,

Thank you once again, in sharing your thoughts. That is certainly answered my needs. Hope I can ask you more in the next event if I have problem writing this code.
 
Corrections:

Thank for the feedback.

Just a correction to a careless mistake, vitally to its working, fear of misleading. The corresponding line should be read:
[tt] for each ofile in fso.getfolder(ssrcdir)[red].files[/red][/tt]

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top