VBA script to create dir and file - vba

I would like to create directory folders and files using excel VBA script. I have the following String
/path/project/command.exe
And I create folders and file under drive D:\ likes this D:\path\project\command.exe.My file may be vary .exe or .txt or .doc or etc.. I already used MkDir but I only create folder not create file. So help me to create directory folders and files using excel VBA script. If exe file or some file types that are not create from vba, I only need to create some temporary files for replacement of exe file.

If you want to create blank files you could use cmd's fsutil:
Sub createBlankFile()
Shell "fsutil file createnew ""D:\path\project\command.exe"" 0", vbNormalFocus
End Sub
The 0 refers to the size of the file.

Related

Word VBA Documents.Open deleting files in folder when falsely passing that folder's path instead of file path

I encountered the following issue:
When accidentally passing a folder path to the Documents.Open function in VBA Word I get the runtime error 5174 as one would expect.
However all files with names that begin with an underscore get deleted in that moment from that folder.
To replicate:
Assume folder C:/Test/
In said folder have two files:
test.txt
_test.txt
In Word VBA execute the command:
Documents.Open("C:/Test/")
(As part of a subroutine or in the immediate window.)
Result: Runtime Error 5174 and _test.txt is now missing.
Note: Passing a nonexisting file like "C:/Test/abc.txt" or a wrong folder path like "C:/Test" (without the last slash) will not have this effect and won't cause the runtime error.
I have only tested this on one system on a network drive under windows 10 with Microsoft Professional Plus 2019. It's entirely possible that it's an issue with the file system. Does anyone have any suggestions as to why is this happening? I now included the workaround to check if I'm passing a folder, but it's still unnerving.
The Documents.Open method opens the specified document and adds it to the Documents collection. It is designed to take the file path, not a folder. If you want to allow users to pick file(s) you may consider using the file open dialog. The FileOpenDialog triggered by your code which opens a folder for picking files allows specifying file formats that should be supported and visible via the dialog window.
Set dlgSaveAs = Application.FileDialog(msoFileDialogFilePicker)
dlgSaveAs.Filter = "Text Files (.txt)|*.txt|Word Documents (.docx)|*.docx|Word Template (.dotx)|*.dotx|All Files (*.*)|*.*"
dlgSaveAs.ValidateNames = true
Res = dlgSaveAs.Show

How would i repeat a filecopy in visual basic?

I'm trying to create a program that repeatedly copies a file to test a flash drive's true size. Since overwriting a file isnt allowed, how would i name the files that go to the destination differently so that i could copy it multiple times?
I would append your loop variable to the end of your destination file name

VBA code (if needed) to save new Word document in same directory as other Word doc

Many users, myself included, begin editing a Word document, create a new Word doc, and wish to save the second doc in the same directory as the first doc. This is the default behavior in an editor such as emacs. Word 2013, alas, makes me pick the directory ("Folder") where I wish to save the new file, forcing me to select among various network folders and then select multiple subdirectories before I reach the one containing my first document. I used to have a simple VBA script that would find the directory of the first file and then save the new file in that directory, or at least it would set the directory of the first file as the default directory for saving files. Does anyone have code to do what I'm looking for? Thanks!
This will the working directory to the directory of the first file you were working on:
ChDir(Application.Documents(Application.Documents.Count).Path)
If you want to save the most recent opened document to the same location as the first file you were working on, then try the following
Sub SaveWithFirstFile()
NewFileName = "filename1.docx
Application.Documents(1).SaveAs2 FileName:=Application.Documents(Application.Documents.Count).Path & "\\" & NewFileName
End Sub
Change "filename1.docx" to what you want it to

VB.NET Iteration and folder operations

I am new to VB. Can anyone help on this, thanks. BTW, it's bloody pain to mess the formatting here :) How to do the indent?
parameters:
source file
target
backup folder
Pseudocode:
get all sub folders under target
for each folder under target
if exists source file
if exists backup folder
replace/copy source file to backup folder
else
create backup folder
replace/copy source file to backup folder
replace source file
Check this methods:
Directory.GetDirectories()
File.Exists()
Directory.Exists()
Directory.CreateDirectory()
File.Copy()
As I told you before on the other posts, just check the methods on System.IO. Its very well documented on MSDN, specially the classes File, Directory and Path.
For getting subfolders refer to this question
You then will want to put the sub folder locations into an array. From this array, loop through each object and so on.
Try using methods under 'File.' as this will include many functions for file processing.
This will require the 'Imports System.IO'.

How to use Process.Start

I'm using process.Start to run Convert.exe. This program's purpose is to convert all files which are in the exe's folder. So when I normally use it, I copy paste a file into the same folder as Convert.exe and then run Convert.exe. Convert.exe will create a new "converted" file in the same folder.
I'm trying to automate this tedious process. A User selects a file which needs to be converted from FolderA, I copy it to the same folder where Convert.exe is and I'm using process.start(Convert.exe) to run it.
Just to be clear, this "Convert.exe" accepts NO arguments.
The problem: "Convert.exe" is not converting the files in its folder. Instead it's converting all the files in FolderA for some weird reason. I don't know why it picked that folder, I never even try to send it as an argument or nothing.
Here's the code I have:
Dim techInfo As New System.IO.FileInfo(itm.strFilePath)
techInfo.CopyTo(ConverterPath & techInfo.Name)
Dim procInfoConvert As New ProcessStartInfo
procInfoConvert.CreateNoWindow = False
procInfoConvert.Arguments = ""
procInfoConvert.FileName = ConverterPath & "Convert.exe"
Dim procConvert As Process = Process.Start(procInfoConvert)
I did a test where I copy pasted a file into the "Convert.exe" folder and then just run this code:
process.start(ConverterPath & "Convert.exe")
The exe returns nothing, same as if there was no files in the folder.
The only thing I can think of is that when process.Start is run, it copies the file to another location and runs it from there.
Any ideas anyone?
Try this:
procInfoConvert.WorkingDirectory = ConverterPath
That'll set the process up to start in the directory it's contained in, instead of the current directory.