Solved

VB Script, move files if a file contains specific value

Posted on 2015-02-21
17
71 Views
Last Modified: 2015-02-23
Hello Experts,

I have in a folder a thousand of csv files with the following structure:

ID;Name;Period Month;Period YEAR;Project ID;Project name;Task ID;Task
38980;Th, Paul;02;2015;179GA300;AGSR_TT_IMPORT;14810082;
358590;Th, Paul;02;2015;MVSPEP2;AGSR_TT_IMPORT;19510165

The highlight values represent the month and year of the current date.

The script that I need should be able to read the various files located at C:\test\, read the various lines related to Period Month and Period YEAR and if the value is not equal to the current date move the files to C:\wrong file\ otherwise keep the file.

Ex: file.csv with the following structure doesn't need to me moved

ID;Name;Period Month;Period YEAR;Project ID;Project name;Task ID;Task
38980;Th, Paul;02;2015;179GA300;AGSR_TT_IMPORT;14810082
358590;Th, Paul;02;2015;MVSPEP2;AGSR_TT_IMPORT;19510165

Ex2: file2.csv with the following structure needs to me moved

ID;Name;Period Month;Period YEAR;Project ID;Project name;Task ID;Task
3890;TF, Seb;01;2015;179GA300;AGSR_TT_IMPORT;14810082
3890;TF, Seb;01;2015;MVSPEP2;AGSR_TT_IMPORT;19510165

Ex3: file.csv with the following structure needs to be moved

ID;Name;Period Month;Period YEAR;Project ID;Project name;Task ID;Task
38980;Th, Paul;02;2014;179GA300;AGSR_TT_IMPORT;14810082
358590;Th, Paul;02;2014;MVSPEP2;AGSR_TT_IMPORT;19510165

Thank your for your help.
0
Comment
Question by:LD16
  • 8
  • 5
  • 4
17 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40623047
unfortunately i can't test the code because i'm on my mac, but i guess you need something like this:

dim FS, fsFolder, strMainFolder, strWrongFolder
dim monthyear, month, year
'Set Script Parameters
strMainFolder="C:\test\"
strWrongFolder="C:\wrong file\"
month=2
year=2015

Set FS = CreateObject("Scripting.FileSystemObject")
If Right(strMainFolder, 1) <> "\" Then strMainFolder = strMainFolder & "\"
If Right(strWrongFolder, 1) <> "\" Then strWrongFolder = strWrongFolder & "\"
Set fsFolder = FS.GetFolder(strMainFolder)
dim moved, kept
kept=0
moved=0
monthyear= "0" & month & ";" & year
monthyear=right(monthyear,7)
monthyear=";" & monthyear & ";"

For Each ofile In FSfolder.files
	if filecontains(ofile.name, monthyear) then
		fs.movefile strMainFolder & ofile.name, strWrongFolder & ofile.name
		moved=moved+1
	else
		kept=kept+1
	end if
Next
msgbox "Operation Complete: " & vbcrlf & "Total Files: " & moved+kept & vbcrlf & "Moved Files: " & moved & vbcrlf & "Retained Files: " & kept

function filecontains(f, t) 
	'Const ForReading = 1, ForWriting = 2, ForAppending = 8
	'Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
	Dim fso, MyFile, TextLine
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set MyFile = fso.OpenTextFile(f, 1)
	Do While MyFile.AtEndOfStream <> True
    		TextLine = MyFile.ReadLine
    		if instr(textline,t, 1) > 0 then
    			'we found the month and year
    			filecontains=true
    			MyFile.close
    			exit function
    		end if
	Loop
	MyFile.Close
	filecontains=false
end function

Open in new window

0
 

Author Comment

by:LD16
ID: 40623060
Hello Dany,

Thank you for your script, I will test it.

Is not a way to dynamically assign the the month and year variables?

Something like this

month =DatePart("yyyy", Now)
year=DatePart("m",Now)
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40623062
well i did the code in a way it's very easy to modify that:

just replace
m=2 with m=month(now)
and
y=2015 with y=year(now)
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40623064
Final Code:

dim FS, fsFolder, strMainFolder, strWrongFolder
dim monthyear, m, y
'Set Script Parameters
strMainFolder="C:\test\"
strWrongFolder="C:\wrong file\"
m=month(now) '2
y=year(now) '2015

Set FS = CreateObject("Scripting.FileSystemObject")
If Right(strMainFolder, 1) <> "\" Then strMainFolder = strMainFolder & "\"
If Right(strWrongFolder, 1) <> "\" Then strWrongFolder = strWrongFolder & "\"
Set fsFolder = FS.GetFolder(strMainFolder)
dim moved, kept
kept=0
moved=0
monthyear= "0" & m & ";" & y
monthyear=right(monthyear,7)
monthyear=";" & monthyear & ";"

For Each ofile In FSfolder.files
	if filecontains(ofile.name, monthyear) then
		fs.movefile strMainFolder & ofile.name, strWrongFolder & ofile.name
		moved=moved+1
	else
		kept=kept+1
	end if
Next
msgbox "Operation Complete: " & vbcrlf & "Total Files: " & moved+kept & vbcrlf & "Moved Files: " & moved & vbcrlf & "Retained Files: " & kept

function filecontains(f, t) 
	'Const ForReading = 1, ForWriting = 2, ForAppending = 8
	'Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
	Dim fso, MyFile, TextLine
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set MyFile = fso.OpenTextFile(f, 1)
	Do While MyFile.AtEndOfStream <> True
    		TextLine = MyFile.ReadLine
    		if instr(textline,t, 1) > 0 then
    			'we found the month and year
    			filecontains=true
    			MyFile.close
    			exit function
    		end if
	Loop
	MyFile.Close
	filecontains=false
end function
                                          

Open in new window


the old variable names were month and year, which are reserved words (i didn't notice) so i just changed them now to m and y respectively
0
 

Author Comment

by:LD16
ID: 40623217
Hello Dany,

I got an error message (file not found) in line 35 char.

I have check the folders variable declaration and all is ok.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40623539
Can there be lines with different dates in the same file?  For example could this happen?

ID;Name;Period Month;Period YEAR;Project ID;Project name;Task ID;Task
38980;Th, Paul;01;2015;179GA300;AGSR_TT_IMPORT;14810082
358590;Th, Paul;02;2015;MVSPEP2;AGSR_TT_IMPORT;19510165

Open in new window

Or, if the current month is found on one line in a file, can we assume the whole file is okay?

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40623609
Give this a try, adjust folder names near top.  You may want to comment out the display of each file after testing.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Specify from and to folders
strBaseDir = "B:\EE\EE28621689\From"
strDestDir = "B:\EE\EE28621689\Dest"

' Build search string from current month (;MM;YYYY;)
strCurrentMonth = ";" & Right("0" & Month(Now), 2) & ";" & Year(Now) & ";"

' Access from folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Initialize counters
intKeep = 0
intMove = 0

' Process each file in from folder
For Each objFile In objFolder.Files

   ' Read file into a  variable
   Set objReadFile = objFSO.OpenTextFile(objFile.Path, ForReading, False, TriStateUseDefault)
   strData = objReadFile.ReadAll
   objReadFile.Close

   ' See if it contains current month string, if so keep it, if not move it
   If InStr(strData, strCurrentMonth) > 0 Then
      Wscript.Echo "Keep:" & objFile.Name
      intKeep = intKeep + 1
   Else
      Wscript.Echo "Move:" & objFile.Name
      objFSO.MoveFile objFile.Path, strDestDir & "\"
      intMove = intMove + 1
	End If

Next

' Display counts
Wscript.Echo "Files Kept : " & intKeep
Wscript.Echo "Files Moved : " & intMove

Open in new window

~bp
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40624695
Lol bill that's my exact code!
Sorry LD i'm on mobile, i will check the code again when i'm on my laptop!

Can u just post me the full code that i tried? Thanks
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Expert Comment

by:Dany Balian
ID: 40625207
ok there were 2 problems in the code:
1. i was not sending the strmainfolder to the function that checks the files (only filename was being sent, so it was not finding the file path)
2. the instr function in vbscript takes the first argument as where to start the search from.. (an integer number)
the full working, tested code is below

dim FS, fsFolder, strMainFolder, strWrongFolder
dim monthyear, month, year
'Set Script Parameters
strMainFolder="C:\ee\src\"
strWrongFolder="C:\ee\dest\"
month=2
year=2015

Set FS = CreateObject("Scripting.FileSystemObject")
If Right(strMainFolder, 1) <> "\" Then strMainFolder = strMainFolder & "\"
If Right(strWrongFolder, 1) <> "\" Then strWrongFolder = strWrongFolder & "\"
Set fsFolder = FS.GetFolder(strMainFolder)
dim moved, kept
kept=0
moved=0
monthyear= "0" & month & ";" & year
monthyear=right(monthyear,7)
monthyear=";" & monthyear & ";"

For Each ofile In FSfolder.files
	if filecontains(strMainFolder & ofile.name, monthyear) then
		fs.movefile strMainFolder & ofile.name, strWrongFolder & ofile.name
		moved=moved+1
	else
		kept=kept+1
	end if
Next
msgbox "Operation Complete: " & vbcrlf & "Total Files: " & moved+kept & vbcrlf & "Moved Files: " & moved & vbcrlf & "Retained Files: " & kept

function filecontains(f, t)
	'Const ForReading = 1, ForWriting = 2, ForAppending = 8
	'Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
	Dim fso, MyFile, TextLine
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set MyFile = fso.OpenTextFile(f, 1)
	Do While MyFile.AtEndOfStream <> True
    		TextLine = MyFile.ReadLine
    		if instr(1,textline,t) > 0 then
    			'we found the month and year
    			filecontains=true
    			MyFile.close
    			exit function
    		end if
	Loop
	MyFile.Close
	filecontains=false
end function

Open in new window

0
 

Author Comment

by:LD16
ID: 40625226
@Dany: I tested your last script with a csv file containing the following date 4/2014. The file should be moved and the script doesn't move the file. Additionnally the date are not dynamically set up.

@Bill: I tested your script and it works however is it possible to move with an overwrite set up If the file already exists in the strDestDir?
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40625240
we just need to replace these 2 lines:
m=month(now)
y=year(now)

could it be that the file: contains both of 02;2015 and 04;2014 ? in that case it will not move the file..

i updated the code below to reflect the dynamic date building and also to replace the file if it exists in the destination folder. (there's no such thing as move with overwrite.. but what we need to do is to copy with overwrite, then delete the file)

dim FS, fsFolder, strMainFolder, strWrongFolder
dim monthyear, m, y
'Set Script Parameters
strMainFolder="C:\test\"
strWrongFolder="C:\wrong file\"
m=month(now) '2
y=year(now) '2015

Set FS = CreateObject("Scripting.FileSystemObject")
If Right(strMainFolder, 1) <> "\" Then strMainFolder = strMainFolder & "\"
If Right(strWrongFolder, 1) <> "\" Then strWrongFolder = strWrongFolder & "\"
Set fsFolder = FS.GetFolder(strMainFolder)
dim moved, kept
kept=0
moved=0
monthyear= "0" & m & ";" & y
monthyear=right(monthyear,7)
monthyear=";" & monthyear & ";"

For Each ofile In FSfolder.files
	if filecontains(strMainFolder & ofile.name, monthyear) then
		'fs.movefile strMainFolder & ofile.name, strWrongFolder & ofile.name
                fs.CopyFile strMainFolder & ofile.name, strWrongFolder & ofile.name, True
                fs.DeleteFile strMainFolder & ofile.name, True
		moved=moved+1
	else
		kept=kept+1
	end if
Next
msgbox "Operation Complete: " & vbcrlf & "Total Files: " & moved+kept & vbcrlf & "Moved Files: " & moved & vbcrlf & "Retained Files: " & kept

function filecontains(f, t) 
	'Const ForReading = 1, ForWriting = 2, ForAppending = 8
	'Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
	Dim fso, MyFile, TextLine
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set MyFile = fso.OpenTextFile(f, 1)
	Do While MyFile.AtEndOfStream <> True
    		TextLine = MyFile.ReadLine
    		if instr(1,textline,t) > 0 then
    			'we found the month and year
    			filecontains=true
    			MyFile.close
    			exit function
    		end if
	Loop
	MyFile.Close
	filecontains=false
end function

Open in new window

0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40625252
it is important to know what you want to do if two dates are found in the same file, keep or move?
0
 

Author Comment

by:LD16
ID: 40625273
@Dany each file contains a unique date.
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 250 total points
ID: 40625278
Final code:
dim FS, fsFolder, strMainFolder, strWrongFolder
dim monthyear, m, y
'Set Script Parameters
strMainFolder="C:\ee\src\"
strWrongFolder="C:\ee\dest\"
m=month(now)
y=year(now)

Set FS = CreateObject("Scripting.FileSystemObject")
If Right(strMainFolder, 1) <> "\" Then strMainFolder = strMainFolder & "\"
If Right(strWrongFolder, 1) <> "\" Then strWrongFolder = strWrongFolder & "\"
Set fsFolder = FS.GetFolder(strMainFolder)
dim moved, kept
kept=0
moved=0
monthyear= "0" & m & ";" & y
monthyear=right(monthyear,7)
monthyear=";" & monthyear & ";"

For Each ofile In FSfolder.files
	if not filecontains(strMainFolder & ofile.name, monthyear) then
		'fs.movefile strMainFolder & ofile.name, strWrongFolder & ofile.name
                fs.CopyFile strMainFolder & ofile.name, strWrongFolder & ofile.name, True
                fs.DeleteFile strMainFolder & ofile.name, True
		moved=moved+1
	else
		kept=kept+1
	end if
Next
msgbox "Operation Complete: " & vbcrlf & "Total Files: " & moved+kept & vbcrlf & "Moved Files: " & moved & vbcrlf & "Retained Files: " & kept

function filecontains(f, t) 
	'Const ForReading = 1, ForWriting = 2, ForAppending = 8
	'Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
	Dim fso, MyFile, TextLine
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set MyFile = fso.OpenTextFile(f, 1)
	Do While MyFile.AtEndOfStream <> True
    		TextLine = MyFile.ReadLine
    		if instr(1,textline,t) > 0 then
    			'we found the month and year
    			filecontains=true
    			MyFile.close
    			exit function
    		end if
	Loop
	MyFile.Close
	filecontains=false
end function

Open in new window

0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 40625480
Give this a try, it will delete the file if it already exists before moving.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Specify from and to folders
strBaseDir = "B:\EE\EE28621689\From"
strDestDir = "B:\EE\EE28621689\Dest"

' Build search string from current month (;MM;YYYY;)
strCurrentMonth = ";" & Right("0" & Month(Now), 2) & ";" & Year(Now) & ";"

' Access from folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Initialize counters
intKeep = 0
intMove = 0

' Process each file in from folder
For Each objFile In objFolder.Files

   ' Read file into a  variable
   Set objReadFile = objFSO.OpenTextFile(objFile.Path, ForReading, False, TriStateUseDefault)
   strData = objReadFile.ReadAll
   objReadFile.Close

   ' See if it contains current month string, if so keep it, if not move it
   If InStr(strData, strCurrentMonth) > 0 Then
      Wscript.Echo "Keep:" & objFile.Name
      intKeep = intKeep + 1
   Else
      Wscript.Echo "Move:" & objFile.Name
      If objFSO.FileExists(strDestDir & "\" & objFile.Name) Then
         objFSO.DeleteFile strDestDir & "\" & objFile.Name
      End If
      objFSO.MoveFile objFile.Path, strDestDir & "\"
      intMove = intMove + 1
	End If

Next

' Display counts
Wscript.Echo "Files Kept : " & intKeep
Wscript.Echo "Files Moved : " & intMove

Open in new window

~bp
0
 

Author Comment

by:LD16
ID: 40625494
Tested. Both scripts works!
Thank you!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40625501
Thanks, glad that was useful, check back if you have any questions.

~bp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now