Create multiple files from a multiple file upload - vb.net

I can upload multiple files using FileUpload but when I run my code only ONE new file is created even though multiple are uploaded. So my output is only one file despite multiple being successfully uploaded.
Any help with how to fix this would be great. thanks in advance.
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<asp:Button ID="Button1" Text="Upload File" runat="server" OnClick="UploadFile" />
'--Upload Multiple Files
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim filePath As String = Server.MapPath("~/Files/") & fileName
FileUpload1.SaveAs(filePath)
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
'-- Create new file to output
Dim NewFile As String = filePath & "_NewCreatedFile.txt"
Dim FilWtr As New StreamWriter(NewFile)
'-- close file
FilWtr.Close()
Next

If you allow multiple files, then the upload control does return a "collection of those files.
This should work:
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
Dim fileName As String = Path.GetFileName(postedFile.FileName)
Dim filePath As String = Server.MapPath("~/Files/") & fileName
FileUpload1.SaveAs(filePath)
Next

Related

replace text multiple file vb.net

I want multiple files with a name and in a different format
I will replace some of the text
To do this, use the following command:
Dim sb As New StringBuilder(File.ReadAllText("tmp\boot_root\initrd\fstab.*"))
sb.Replace("ro,barrier", "ro,noatime,barrier")
sb.Replace("ro,errors", "ro,noatime,errors")
But this does not work. I need a better command to do this
help please
To get a list of all files matching a pattern in a given directory use Directory.Getfiles(path, pattern). Example:
Dim list as String()
Dim path as String = "C:\tmp\"
Dim filename as String = "fstab"
list = Directory.GetFiles(path, filename & ".*")
For Each file As String In list
'Logic goes here.
Next

Convert multiple .xls files to .xlsx in ssis

I have a folder that receives multiple excel files in .xls format. I need to change the format type to .xlsx in order to load the excel data into SQLvia SSIS. I know how to rename the file using "File System Task" but that works for a specific file. but my file contains a file # and date as well that needs to stay same as source file, I only want the file type to change and the file move to a processed folder. Can anyone help me?
Source Path: C:\Documents\TestFolder
Source File: TestSegRpt_0001_2017_02_22.xls
Destination Path: C:\Documents\TestFolderProcessed
Destination File: TestSegRpt_0001_2017_02_22.xlsx
Hoping i understood your problem correctly.
I think below link will help.
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_other/batch-convert-xls-to-xlsx/1d9b3d78-daf0-4014-8fb2-930aca6493b0
You have to add a Script Task, loop over files, and use a function like the following to create precessed directory and convert files (code in Vb.net):
Public Sub ConvertXlsToXlsx(ByVal strpath as string)
Dim strDirectory as string = System.IO.Path.GetDirectoryName(strpath) & "Processed"
If Not System.IO.Directory.Exists(strDirectory) Then System.IO.Directory.CreateDirectory(strDirectory)
Dim xl As New Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
xlWorkBook = xl.Workbooks.Open(strpath)
xlBook.SaveAs(strDirectory & "\" & System.IO.Path.GetFilename(strpath) & "x")
xl.Application.Workbooks.Close()
xl.Application.Quit()
End Sub
Your code will look like:
Public Sub Main
Dim strFolder as string = Dts.Variables.Item("FolderPath").Value
Dim strXlsFiles() as string = IO.Directory.GetFiles(strFolder,"*.xlsx",SearchOption.TopDirectoryOnly)
For each strFile as String in strXlsFiles
If strFile.EndsWith("xlsx") The Continue For
ConvertXlsToXlsx(strFile)
Next
End Sub
Reference:
https://social.msdn.microsoft.com/Forums/office/en-US/a73f846c-91ee-4dad-bd7b-c04d418d0561/convert-xls-into-xlsx?forum=exceldev

Screenshot window and save with environment variables

I have code to take a screenshot of a window, seen here:
SC.CaptureWindowToFile(Me.Handle, "c:\Program Files\image_" & Now.ToString("yyyyMMddHHmmss") & ".png", Imaging.ImageFormat.Png)
I want to be able to set an environment variable so that the images will be saved in a folder created by my installer, let's call it "Screenshots" and it's in the Documents folder. I assume I'll have to use something like:
Dim fullPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
But I don't know how to combine these two together so it will save the screenshots into the folder in my documents. Any ideas?
If I understand your question, this should do it:
Dim myDocumentsPath as String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim formattedDateString as String = Date.Now.ToString("yyyyMMddHHmmss")
Dim imagePath as String = String.Format("{0}\Screenshots\image_{1}.png", myDocumentsPath, formattedDateString)
SC.CaptureWindowToFile(Me.Handle, imagePath, Imaging.ImageFormat.Png)

Open Windows Explorer from VB.NET: doesn't open in the right folder

I am trying to locate a file from a program, in VB.NET
Dim exeName As String = "explorer.exe"
Dim params As String = "/e,""c:\test"",/select,""C:\test\a.txt"""
Dim processInfo As New ProcessStartInfo(exeName, params)
Process.Start(processInfo)
It opens the containing directory "c:\" but doesn't go inside to "c:\test", I would like to have the file selected...
Dim filePath As String = "C:\test\a.txt" 'Example file
Process.Start("explorer.exe", "/select," & filePath) 'Starts explorer.exe and selects desired file
You don't need the folder path in there after the /e, try this for your params:
Dim params As String = "/e, /select,""C:\temp\file.txt"""

How to paste a file from Clipboard to specific path

How I can Paste from Clipboard a file to my path? I work in VB .NET. I got filename from clipboard but don't know how to extract file from cliboard and save it to my folder.
Dim data As IDataObject = Clipboard.GetDataObject()
If data.GetDataPresent(DataFormats.FileDrop) Then
Dim files As String() = data.GetData(DataFormats.FileDrop)
End If
Can anybody help me?
Thanks in advance!
You can use the Path class to both isolate the file name and create the path of the new file to use in a file copy operation:
Dim data As IDataObject = Clipboard.GetDataObject
If data.GetDataPresent(DataFormats.FileDrop) Then
For Each s As String In data.GetData(DataFormats.FileDrop)
Dim newFile As String = Path.Combine("c:\mynewpath", Path.GetFileName(s))
File.Copy(s, newFile)
Next
End If
Example needs error checking.
You can also get the full path to the file as follows:
Dim objeto As IDataObject = Clipboard.GetDataObject
For Each data As String In objeto.GetData(DataFormats.FileDrop)
...
Dim newFile As String = Path.GetFullPath(data.ToString)
...
Next