I already an existing vbs script to take one file (titled "Running_12345.xlsx") from one location and put it in the folder titled "Folder". This is an hourly file that has a long name based on what time it was run.
Now, I want to rename the file just "Running.xlsx" to remove the constantly changing file name. Initially, this code works, but for any subsequent occurence, it fails because the "Running.xlsx" file has already been renamed once and now already exists. How do I add overwrite logic to this code:
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
set oFldr = fso.getfolder("folder")
for each ofile in oFldr.Files
if lcase(fso.GetExtensionName(ofile.Name)) = "xlsx" then
ofile.name = "Running.xlsx"
Exit for
end if
Next
Duh, just delete the file first. The code below does what I want:
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
set oFldr = fso.getfolder("C:\Users\...\Desktop\MosaicTransforms\")
fso.DeleteFile("C:\Users\...\Desktop\MosaicTransforms\MosaicFile.xlsx")
for each ofile in oFldr.Files
if lcase(fso.GetExtensionName(ofile.Name)) = "xlsx" then
ofile.name = "MosaicFile.xlsx"
Exit for
end if
Next
Related
I have successfully created a VBScript that renames a file as required when it is the only file in the folder. I cannot figure out how to have the script search past the most recent file.
Option Explicit
Dim fso, folder, file, tmFile
Dim folderName
folderName = "\\pcc\Int\PC\Inbox\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
Set tmFile = Nothing
For each file In folder.Files
If (tmFile is Nothing) Then
Set tmFile = file
Exit For
End IF
Next
If InStr(tmfile.name, "TM") Then
TmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
End if
The above script correctly renames the file.
Here are a few modifications I have tried to go through all of the files in the folder to search for the file that has the prefix TM. This will always be the only file with the TM prefix.
For Each InStr(tmFile.name, "TM") Then
tmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
Exit for
and
If tmFile.fileexists(tmFile.name, "TM") Then
tmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
End if
You are close with your instr(), it's just that you need to put that test within your already existing For loop:
Option Explicit
Dim fso, folder, file, tmFile
Dim folderName
folderName = "\\pcc\Int\PC\Inbox\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
For each file In folder.Files
If instr(file, "TM") > 0 THEN
file.name = replace(file.name, ".txt", "A.txt")
End IF
Next
I've removed the tmfile variable since it's simply not needed here.
What I wish to do is:
Copy files from a variety of sub-folders under a single main folder to a destination folder.
Three options when copying:
If no file in destination folder exists then copy.
If file exists, copy over if filesize is larger than destination file.
If file exists and both are the same filesize compare date/time and copy over if most recent.
Here is my pseudocode so far:
Dim filesys, strSourceFile, strDestFolder, strDestFile
Set filesys = CreateObject("Scripting.FileSystemObject")
strSourceFile = S:\SoCal\Section_2\*\Autogen\texture\*.agn
strDestFolder = F:\ADDON_SCENERY\simwestSOCAL\texture
strDestFile = F:\ADDON_SCENERY\simwestSOCAL\texture\*.agn
COPY each file in strSourceFolder
If IsEmpty (SourceFile, DestFolder)
Else If (SourceFile FileSize > DestFile)
Else If (SourceFile DateTime > DestFile DateTime)
Then 'keep/copy most recent file
End if
Am I on the right track?
Do I need to add a Loop?
Can one compare file sizes? All my research has found nothing yet on this.
Can I compare Date and Time against files?
As an update to my original post... (hope I am following forum rules correctly),
I have spent the last several weeks non-stop just reading-reading-reading and testing-failure-testing. I am happy to say (and a little proud), that I have completed my very first script... and it appears to work as planned but for just one file. I now need to convert this to work on all files inside my 'sourcefolder'.
I am a bit "brain dead" from this so any direction on converting this would be most appreciated. I know I need loops but what type and where? Do I rename everything referring to a file to a folder or use '*.txt' for files? In the meantime I will keep studying.
Here is my script (yea, lot's of MsgBox's so I could follow along the script path):
dim dFolder
dFolder = "S:\Scripting Workfolder\destfolder\"
dim dFile
dFile= "S:\Scripting Workfolder\destfolder\File 1.txt"
dim sFile
sFile = "S:\Scripting Workfolder\sourcefolder\File 1.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If Not fso.FileExists(dFile) Then
MsgBox "File does not exist - will copy over to dFolder"
fso.CopyFile sFile, dFolder, true
Elseif fso.FileExists(dFile) Then
MsgBox "File already exist in destination folder determine largest"
ReplaceIfLarger sFile, dFile
End If
Sub ReplaceIfLarger(sFile, dFile)
const overwrite_existing = true
dim objFSO
set objFSO = createobject("Scripting.FileSystemObject")
dim objSourceFile
set objSourceFile = objFSO.GetFile(sFile)
'dim kbSourceSize
kbSourceSize = objSourceFile.size
dim objTargetFile
set objTargetFile = objFSO.GetFile (dFile)
'dim kbTargetSize
kbTargetSize = objTargetFile.size
If kbSourceSize > kbTargetSize Then
MsgBox "Source file is LARGER and will overwrite to dest folder"
objFSO.CopyFile objSourceFile.Path, objTargetFile.Path, overwrite_existing
ElseIf kbSourceSize < kbTargetSize Then
MsgBox "Source file is smaller - Will not overwrite to dest folder"
Else
ReplaceIfNewer sFile, dFile
End If
End Sub
Sub ReplaceIfNewer(sFile, dFile)
MsgBox "Both files exist and are the same size. Keep newest file"
const overwrite_existing = true
dim objFSO
set objFSO = createobject("Scripting.FileSystemObject")
dim dtmSourceFile
set dtmSourceFile = objFSO.GetFile(sFile)
dim dtmTargetFile
set dtmTargetFile = objFSO.GetFile(dFile)
If (dtmSourceFile.DateLastModified > dtmTargetFile.DateLastModified) then
MsgBox "Source File is Newer than Target File - Overwrite Target file"
objFSO.CopyFile dtmSourceFile.Path, dtmTargetFile.Path, overwrite_existing
Else
MsgBox "Source File is Older than Target File - Will not overwrite file"
End If
End Sub
I am basically trying to loop through my working folder to pick up any files that begin with 'Health' and unzip them in the same folder. The problem I have is that the code runs but I'm not getting any unzipped files nor am I getting any errors, can someone please help?
Sub Unzip()
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\Users\Jimbo\Documents\Process\_ThisWeek").Files
If LCase(fso.GetExtensionName(f)) = "zip" And Left(f.Name, 3) = "Health" Then
Unzip f.Path, "C:\Users\Jimbo\Documents\Process\_ThisWeek"
End If
'If the extraction location does not exist create it
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(f.path) Then
fso.CreateFolder(f.path)
End If
'Extract the contants of the zip file
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(f.name).items
objShell.NameSpace(f.path).CopyHere(f.name)
Set fso = Nothing
Set objShell = Nothing
Next
End Sub
Hi I succeed changing a file name using VBS by using this code:
Set fso = CreateObject("Scripting.FileSystemObject")
set oFldr = fso.getfolder("C:\v\t_TEST\")
for each ofile in oFldr.Files
if lcase(fso.GetExtensionName(ofile.Name)) = "txt" then
ofile.name = "index.txt"
Exit for
end if
Next
Now I would like to make every .txt file placed on folder "v" (subfolders) to change it name also.
Or to define a changing path ("C:\v\t_XXX), so each folder starts with t_ in this path will change inside it all files end with .txt.
How do I do that?
Thanks!
I believe this is what you are looking for....
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFldr = fso.getfolder("C:\v\")
For Each fld In ofldr.SubFolders
If Left(fld.name, 2) = "t_" Then
Set ofls = fso.GetFolder("C:\v\" & fld.name & "\")
For Each ofile In ofls.Files
If LCase(fso.GetExtensionName(ofile.Name)) = "txt" Then
ofile.name = "index.txt"
Exit For
End If
Next
End If
Next
In VBS how do you make a directory and then copy a file into it?
Id like to make a folder in the root of C e.g. C:\folder and then copy a file from \server\folder\file.ext into that new folder
Use the FileSystemObject object, namely, its CreateFolder and CopyFile methods. Basically, this is what your script will look like:
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Create a new folder
oFSO.CreateFolder "C:\MyFolder"
' Copy a file into the new folder
' Note that the destination folder path must end with a path separator (\)
oFSO.CopyFile "\\server\folder\file.ext", "C:\MyFolder\"
You may also want to add additional logic, like checking whether the folder you want to create already exists (because CreateFolder raises an error in this case) or specifying whether or not to overwrite the file being copied. So, you can end up with this:
Const strFolder = "C:\MyFolder\", strFile = "\\server\folder\file.ext"
Const Overwrite = True
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FolderExists(strFolder) Then
oFSO.CreateFolder strFolder
End If
oFSO.CopyFile strFile, strFolder, Overwrite
You can use the shell for this purpose.
Set shl = CreateObject("WScript.Shell")
shl.Run "cmd mkdir YourDir" & copy "