C++/VB - Moving files from one directory to another based on the file name contains specified string by using vbscript

Asked By Sarvesh on 25-Jan-09 01:04 AM
Hi,

I am having different kind of files need to move from one drive to
another based on the file name contains specified string by using
vbscript. For example, i need to move the file that the filename
contains string "mylog". Please help me to write vbscript to do this
job.

Thanks,
Sarvesh


noon replied on 25-Jan-09 05:07 PM
Il giorno Fri, 23 Jan 2009 06:29:18 -0800 (PST), Sarvesh <sarveswara.p@gmail.com> ha
scritto:


This asks dos to write a file with the pathnames of all the files you are looking for
(maybe).

drive="c:\"
cmd = "dir "& trim(drive) & "*mylog* /s/b "
tmp = "c:\data\pathnames.txt"
Set WshShell = CreateObject("WScript.Shell")
WSHShell.Run "%comspec% /c " & cmd & " >" & tmp, 0, True


--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--
Sarvesh replied on 31-Jan-09 11:38 PM
ail.com> ha
looking for
empi e programmi in VbScript)

Hi,

The above mentioned code is not moving any files. It is just creating
pathnames.txt file. I need to move the files that those filenames
contains string "mylog".

Please help me to get the appropriate code.

Thanks in Advance.

Regards,
Sarvesh
Pegasus \(MVP\) replied on 29-Jan-09 10:53 AM
You need to think about your requirements in more detail and post the
following information:
- Where are your files kept? In a single folder? In a directory tree?
- Where do you want to move them to? To the root of another partition? To a
folder on that partition? If so, what name? Do you wish to rebuild the
source directory tree on the target partition?
- What should happen if a file of the same name already exists on the target
partition?
- Can you show us the (partial) script solution you have developed so far?
Sarvesh replied on 04-Feb-09 05:17 AM
a
get
?

Hi,

I am having the below code to move the files from one drive(c:) to
another(d:).

Option Explicit

Dim oFS, oFile, sFile

Set oFS =3D WScript.CreateObject("Scripting.FileSystemObject")

Const sSourceFdr =3D "C:\test1\"
Const   sDestFdr =3D "D:\test2\"
Const      nDays =3D 10

For Each oFile In oFS.GetFolder(sSourceFdr).Files
On Error Resume Next
If DateDiff("d", oFile.DateLastModified, Now) > nDays Then
oFS.MoveFile oFile.Path, sDestFdr
End If
On Error Goto 0
Next
Set oFS =3D Nothing
WScript.Quit

I need cut and paste the files those filename's contains "mylog" .

Thanks,
Sarvesh
Pegasus \(MVP\) replied on 03-Feb-09 08:41 AM
Hi,

I am having the below code to move the files from one drive(c:) to
another(d:).

Option Explicit

Dim oFS, oFile, sFile

Set oFS = WScript.CreateObject("Scripting.FileSystemObject")

Const sSourceFdr = "C:\test1\"
Const   sDestFdr = "D:\test2\"
Const      nDays = 10

For Each oFile In oFS.GetFolder(sSourceFdr).Files
On Error Resume Next
If DateDiff("d", oFile.DateLastModified, Now) > nDays Then
oFS.MoveFile oFile.Path, sDestFdr
End If
On Error Goto 0
Next
Set oFS = Nothing
WScript.Quit

I need cut and paste the files those filename's contains "mylog" .

Thanks,
Sarvesh

==================

You can introduce an overall "if" condition like so:
if instr(1, oFile.Name, "mylog", 1) > 0 then
{do your file move}
end if
Rahu replied on 11-Feb-09 03:59 AM
Hi Sarvesh,

Try the below script...

Dim sOriginFolder, sDestinationFolder, sFile, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOriginFolder = "D:\Documents\P2P Dev\BoA_H2H\XML\"
sDestinationFolder = "D:\Documents\P2P Dev\BoA_H2H\XML\TrasnferXML\"
For Each sFile In oFSO.GetFolder(sOriginFolder).Files
If instr(1, sFile.Name, ".xml", 1) > 0 then
oFSO.MoveFile sFile, sDestinationFolder
End If

Next


I am pretty sure this will work for you.

Thanks
Rahul
Rahul_Bangi replied on 11-Feb-09 04:00 AM
Hi Sarvesh,

Try this....

Dim sOriginFolder, sDestinationFolder, sFile, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOriginFolder = "D:\Documents\P2P Dev\BoA_H2H\XML\"
sDestinationFolder = "D:\Documents\P2P Dev\BoA_H2H\XML\TrasnferXML\"
For Each sFile In oFSO.GetFolder(sOriginFolder).Files
If instr(1, sFile.Name, ".xml", 1) > 0 then
oFSO.MoveFile sFile, sDestinationFolder
End If

Next


I am pretty sure it will work for you.

Thanks
Rahul
sweta Rai replied to Rahul_Bangi on 28-Sep-10 06:33 AM
I have a code which would copy a file from a location to another, make 2 copies of it and rename both.But when i try to rename, it is renaming the file at source folder. Pls suggest



Sub mycode()

Dim filesys, oFile, sName, oCount, dFile

'Set oCount = 0

Const sSourceFdr = "E:\VBS\"

Const sDestFdr = "E:\VBS\temp\"

Set filesys = CreateObject("Scripting.FileSystemObject")



For Each oFile In filesys.GetFolder(sSourceFdr).Files

On Error Resume Next

If InStr(1, oFile.Name, "Actuals") <> 0 Then



filesys.CopyFile oFile.Path, sDestFdr, OverWriteExisting





sName = Replace(oFile.Name, "Inter", "InterCA")

oFile.Name = sName

End If

Next

On Error GoTo 0



End Sub
Eric Ahlman replied to Rahu on 13-Jul-11 06:07 PM
thanks that worked mostly for what I am trying to do.  Is there a way to add options to NOT overwrite if it already exists in the destination folder a file by the same name?  Also what about getting only the most recent version.  In this scenario i have database backups.  My software keeps like 8 full backups in one dir, and daily incrementals.



I only want to copy the latest version of the full backup (contains ~FULL in the file name) based on the date modified in the file structure.



Any help is greatly appreciated!