Jeremyw
asked on
Remove Timestamp from Filename
I am copying a file from one folder to another.
In folder A, the filename is PO12345yyyymmddss.txt
When I copy the file to folder B, I only want to get the PO12345 and always strip yyyymmddss
I will always want to strip the last 10 characters of the filename.
Thanks,
Jeremy
In folder A, the filename is PO12345yyyymmddss.txt
When I copy the file to folder B, I only want to get the PO12345 and always strip yyyymmddss
I will always want to strip the last 10 characters of the filename.
Thanks,
Jeremy
Whoops - vbscript, sorry...here ya go...
SourceFolder = "C:\FolderA\"
TargetFolder = "C:\FolderB\"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder : Set objFolder = objFSO.GetFolder(SourceFolder)
For Each file in objFolder.Files
strExt = objFSO.GetExtensionName(file)
OrgName = objFSO.GetBaseName(file)
NewName = Left(OrgName, Len(OrgName) - 10)
objFSO.CopyFile file, TargetFolder & NewName & "." & strExt, True 'True = overwrite target file if present
Next
Set objFSO = Nothing
ASKER
Guess I should have included this in my original message.
This is where I need the help. 'Make copy of File removing the yyyymmddss
This is where I need the help. 'Make copy of File removing the yyyymmddss
Dim objFSO, strArgs, objFolder, objFile, objFile2
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set the FTP pickup Directory
strSPSFTPInbound = "c:\scripts\SPSFTPTest" ''SPS Test directory
' Set the drop directories
str850BackupFolder = "c:\scripts\backup\inbound\" '' specify folder where the backup file is copied to from SPS FTP
str850ProdFolder = "c:\scripts\prod\inbound\" '' specify folder where the file is moved for import to PRMS
' Loop the files in the SPS FTP folder
Set objFolder = objFSO.GetFolder("C:\Scripts\SPSFTPTest\")
'Make Backup of File
For each objFile in objFolder.Files
objFile.Move str850BackupFolder & objFile.Name
'Make copy of File removing the yyyymmddss
Set objFile2 = Left(objFile.Name,11)
objFile.Copy str850ProdFolder & objFile2.Name
Next
And if you want 'only' txt files in that folder...
SourceFolder = "C:\FolderA\"
TargetFolder = "C:\FolderB\"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder : Set objFolder = objFSO.GetFolder(SourceFolder)
For Each file in objFolder.Files
If objFSO.GetExtensionName(file)="txt" Then
strExt = objFSO.GetExtensionName(file)
OrgName = objFSO.GetBaseName(file)
NewName = Left(OrgName, Len(OrgName) - 10)
objFSO.CopyFile file, TargetFolder & NewName & "." & strExt, True
End If
Next
Set objFSO = Nothing
Perhaps we cross-posted? I think my example should show you how to make it work...?
If not, I think you can simply change:
Set objFile2 = Left(objFile.Name,11)
to
strFile2 = objFSO.GetBaseName(objFile )
NewFileName = Left(strFile2, Len(strFile2) - 10) & objFSO.GetExtensionName(ob jFile)
If not, I think you can simply change:
Set objFile2 = Left(objFile.Name,11)
to
strFile2 = objFSO.GetBaseName(objFile
NewFileName = Left(strFile2, Len(strFile2) - 10) & objFSO.GetExtensionName(ob
ASKER
Looks like we did post at the same time. I tried changing to your last suggestion
strFile2 = objFSO.GetBaseName(objFile )
NewFileName = Left(strFile2, Len(strFile2) - 10) & objFSO.GetExtensionName(ob jFile)
But looks like I may be missing something. It did copy the file, but did filenametxt instead of filename.txt
In case it matters, I'm retrieving a file from a FTP site, going to make a copy of the original file to a backup folder (str850BackupFolder), then copy that file to str850ProdFolder and remove the yyyymmddss. Then loop back through the FTP folder and pull the next file.
I'm going to start another question when this one is complete, because if the file already exists in the str850ProdFolder, then I will need to open the existing file and append to it instead of overwrite.
strFile2 = objFSO.GetBaseName(objFile
NewFileName = Left(strFile2, Len(strFile2) - 10) & objFSO.GetExtensionName(ob
But looks like I may be missing something. It did copy the file, but did filenametxt instead of filename.txt
In case it matters, I'm retrieving a file from a FTP site, going to make a copy of the original file to a backup folder (str850BackupFolder), then copy that file to str850ProdFolder and remove the yyyymmddss. Then loop back through the FTP folder and pull the next file.
I'm going to start another question when this one is complete, because if the file already exists in the str850ProdFolder, then I will need to open the existing file and append to it instead of overwrite.
Dim objFSO, strArgs, objFolder, objFile, objFile2
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set the FTP pickup Directory
strSPSFTPInbound = "c:\scripts\SPSFTPTest" ''SPS Test directory
' Set the drop directories
str850BackupFolder = "c:\scripts\backup\inbound\" '' specify folder where the backup file is copied to from SPS FTP
str850ProdFolder = "c:\scripts\prod\inbound\" '' specify folder where the file is moved for import to PRMS
' Loop the files in the SPS FTP folder
Set objFolder = objFSO.GetFolder("C:\Scripts\SPSFTPTest\")
'Make Backup of File
For each objFile in objFolder.Files
objFile.Move str850BackupFolder & objFile.Name
'Make copy of File removing the yyyymmddss
strFile2 = objFSO.GetBaseName(objFile)
NewFileName = Left(strFile2, Len(strFile2) - 10) & objFSO.GetExtensionName(objFile)
objFile.Copy str850ProdFolder & NewFileName
Next
Set objFSO = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect. I'll be posting another will all my code to try and come up with a solution to Append the file going to str850ProdFolder if it already exists.
Oops - just tried to post to it apparently right as you deleted it...
ASKER
I figured out the code not long after I posted. Sorry about that. Thanks for your help.
For each objFile in objFolder.Files
objFile.Copy str850BackupFolder & objFile.Name
'Make copy of File removing the yyyymmddss
''If File already exists in str850ProdFolder, then open File and paste current file.
strFile2 = objFSO.GetBaseName(objFile)
NewFileName = Left(strFile2, Len(strFile2) - 14) & "." & objFSO.GetExtensionName(objFile)
If objFSO.FileExists(str850ProdFolder & NewFileName) Then
Set aFile = objFSO.OpenTextFile(str850ProdFolder & NewFileName, 8) '8=for appending
Set cFile = objFSO.OpenTextFile(objFile, 1) '1=for reading
s = cFile.ReadAll
aFile.Write s
aFile.Close
cFile.Close
Else
objFile.Copy str850ProdFolder & NewFileName
End If
Open in new window