[RESOLVED] Looping thru a directory using VBScript???
I'm new to VBScript and I need to write a script that will basically open and read every file in a specific directory. All I know is that I can use the FileSystemObject. However, since this is VBScript and not VB.Net, I'm not sure what objects and methods I need to use to complete this task.
Also, within this looping structure...I need to call another VBScript that has 3 command line parameters, one of which would be the file in the directory being processed. I don't know how to do that. I was told to use the "Execute" command but I have found no examples of how to use it correctly.
See if this gets you started. Create 2 vbs files. Call one files.vbs and filenames.vbs.
Use this code in files.vbs. You can remove the comments to just loop through the files or just leave it in place and the code will loop through the files and then call the filenames.vbs passing it the filename along with a couple other arguments.
Code:
Option Explicit
Dim FSO, FLD, FIL
Dim strFolder
'Change as needed
strFolder = "C:\Documents and Settings\Mark\Desktop\VBS Sample"
'Create the filesystem object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
set FLD = FSO.GetFolder(strFolder)
' 'loop through the folder and get the file names
' For Each Fil In FLD.Files
' MsgBox Fil.Name
' Next
'Or to call an external vbs file with arguments
Dim oShell, strVBS, strRun
Dim strArgs, strArg1, strArg2, strArg3
Set oShell = CreateObject("Wscript.Shell")
strVBS = "filenames.vbs"
strArg2 = "Arg2"
strArg3 = "Arg3"
For Each Fil In FLD.Files
strArg1 = Fil.Name
strArgs = strArg1 & " " & strArg2 & " " & strArg3
strRun = strVBS & " " & strArgs
call oShell.run(strRun, 3, True)
Next
'Clean up
Set oShell = Nothing
Set FLD = Nothing
Set FSO = Nothing
Place this code in the filenames.vbs file.
Code:
Option Explicit
Dim objArgs, strArg, strArgs
Set objArgs = WScript.Arguments
For Each strArg in objArgs
strArgs = strArgs & strArg & vbcrlf
Next
MsgBox strArgs
Your code seemed to work ok, however, in my "called" script I got the error message: "VBScript Compilation Error: expected statement" on the highlighted line of code. Here is my code below
Code:
Sub CreateOutputFileName
Dim strFileName
DoAgain:
strError = ""
strFileName = "NBS" & Month(now) & Day(now) & Year(now) & "-" & Hour(now) & Minute(now) & Second(now) & ".csv"
strFileName = strOFolder & strFileName
If fso.FileExists(strFileName) Then
Goto DoAgain
Else
Set tsi2 = fso.CreateTextFile(strFileName, 1)
End If
End Sub
By the way....thanks for your help!!!!
Last edited by blakemckenna; Jan 29th, 2009 at 05:53 PM.
VBScript doesn't have a goto statement. Since your filename is being built based on time I would just loop while the file exists and then move on. In theory that shouldn't take more than a second.
Code:
Sub CreateOutputFileName
Dim strFileName
Do
strError = ""
strFileName = "NBS" & Month(now) & Day(now) & Year(now) & "-" & Hour(now) & Minute(now) & Second(now) & ".csv"
strFileName = strOFolder & strFileName
Loop While fso.FileExists(strFileName)
Set tsi2 = fso.CreateTextFile(strFileName, 1)
End Sub
I am new to scripts
I am trying to make a script that will open every file in a folder replace all tabs with a space, save the file and will close the same.
I am trying to takcle it step by step.
I went through this thread. It can be helpful for me.
It is looping and what i want to do is like
1. open the file
2. replace all tabs
3. save the file
4. Close it
5. open next file in that folder (follow same process till last file)
Kindly let me know where to put that code and what to put.
Option Explicit
Dim FSO, FLD, FIL, TS
Dim strFolder, strContent, strPath
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'Change as needed
strFolder = "C:\Documents and Settings\Mark\Desktop\VBS Sample"
'Create the filesystem object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
set FLD = FSO.GetFolder(strFolder)
'loop through the folder and get the files
For Each Fil In FLD.Files
'Open the file to read
Set TS = FSO.OpenTextFile(fil.Path, ForReading)
'Read the contents into a variable
strContent = TS.ReadAll
'Close the file
TS.Close
'Replace the tabs
strContent = Replace(strContent, vbTab, " ")
'Open the file to overwrite the contents
Set TS = FSO.OpenTextFile(fil.Path, ForWriting)
'Write the contents back
TS.Write strContent
'Close the current file
TS.Close
Next
'Clean up
Set TS = Nothing
Set FLD = Nothing
Set FSO = Nothing
Option Explicit
Dim FSO, FLD, FIL, TS
Dim strFolder, strContent, strPath
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'Change as needed
strFolder = "C:\Documents and Settings\Mark\Desktop\VBS Sample"
'Create the filesystem object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
set FLD = FSO.GetFolder(strFolder)
'loop through the folder and get the files
For Each Fil In FLD.Files
'Open the file to read
Set TS = FSO.OpenTextFile(fil.Path, ForReading)
'Read the contents into a variable
strContent = TS.ReadAll
'Close the file
TS.Close
'Replace the tabs
strContent = Replace(strContent, vbTab, " ")
'Open the file to overwrite the contents
Set TS = FSO.OpenTextFile(fil.Path, ForWriting)
'Write the contents back
TS.Write strContent
'Close the current file
TS.Close
Next
'Clean up
Set TS = Nothing
Set FLD = Nothing
Set FSO = Nothing
Many thanks Mark, but this is not doing it.... ....
Below is the sample of content of my file (text file) when i open it in notepad..
Test
Test
I am seeking output as :
Test
Test
Code seems fine to me..but it is not working..
Can you help on this or if you can test at your place it would be great.
I ran the code you provided blakemckenna and it is working gr8 bt this one is not working
If you want to post one of your text files I can take a look. The only reason it isn't working is the character before the word test is not a tab.
Hi Mark,
Please find attached the test file.
Actually file is downloaded from a mainframe application and it comes in this format . Since number of files are huge around 100 so i dont want to do that manually as it will take a lot of time.
It actually looks like you are trying to remove everything before the "T" so give this a try.
Code:
Option Explicit
Dim FSO, FLD, FIL, TS
Dim strFolder, strContent, strPath
Const ForReading = 1, ForWriting = 2, ForAppending = 8
dim strFind, strReplace
'Change as needed
strFolder = "C:\Documents and Settings\Mark\Desktop\VBS Sample"
'Create the filesystem object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
set FLD = FSO.GetFolder(strFolder)
'loop through the folder and get the file names
For Each Fil In FLD.Files
'Open the file to read
Set TS = FSO.OpenTextFile(fil.Path, ForReading)
'Read the contents into a variable
strContent = TS.ReadAll
'Close the file
TS.Close
strFind = chr(141) & chr(32)
strReplace = ""
'Do the replace
strContent = Replace(strContent, strFind, strReplace)
'Open the file to overwrite the contents
Set TS = FSO.OpenTextFile(fil.Path, ForWriting)
'Write the contents back
TS.Write strContent
'Close the current file
TS.Close
Next
'Clean up
Set TS = Nothing
Set FLD = Nothing
Set FSO = Nothing
You have to run a recursive function to get the sub directories.
Code:
Option Explicit
Dim fso, fold
Dim strFolder
Const ForReading = 1, ForWriting = 2
dim strFind, strReplace
'Change as needed
strFolder = "C:\Documents and Settings\Mark\Desktop\VBS Sample"
'Create the filesystem object
Set fso = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
Set fold = FSO.GetFolder(strFolder)
strFind = chr(141) & chr(32)
strReplace = ""
RecursiveSearch fold
'Clean up
Set fold = Nothing
Set fso = Nothing
Private Sub RecursiveSearch(flder)
Dim strContent
Dim fil, fld, ts
For Each fld In flder.SubFolders
RecursiveSearch fld
Next
'loop through the folder and get the file names
For Each fil In flder.Files
'Open the file to read
Set ts = fso.OpenTextFile(fil.Path, ForReading)
'Read the contents into a variable
strContent = ts.ReadAll
'Close the file
ts.Close
'Do the replace
strContent = Replace(strContent, strFind, strReplace)
'Open the file to overwrite the contents
Set ts = FSO.OpenTextFile(fil.Path, ForWriting)
'Write the contents back
ts.Write strContent
'Close the current file
ts.Close
Next
End Sub
Re: [RESOLVED] Looping thru a directory using VBScript???
Hi
I am new to scripting. I would require your help with the below requirement.
1. We have files in a folder with the naming convention transactionnumber_number of transactions_datetimestamp.csv
2. I would have to loop through the folder and merge contents of files with same transaction number when the number of files with that transaction number match the number of transactions in the file name.
3. Place contents of merged documents to another folder.
Re: [RESOLVED] Looping thru a directory using VBScript???
Originally Posted by gentran
Hi
I am new to scripting. I would require your help with the below requirement.
1. We have files in a folder with the naming convention transactionnumber_number of transactions_datetimestamp.csv
2. I would have to loop through the folder and merge contents of files with same transaction number when the number of files with that transaction number match the number of transactions in the file name.
3. Place contents of merged documents to another folder.
Any help is greatly appreciated. Thank you.
Please don't bump dead thread to ask your question start a new topic.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672