Avatar of Jeremyw
Jeremyw
Flag for United States of America 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
VB Script

Avatar of undefined
Last Comment
Jeremyw

8/22/2022 - Mon
sirbounty

I believe this will do it for you...
@echo off
cd /d c:\folderA
for /f %%a in ('dir /b *.txt') do call :process %%a
goto :eof
 
:process
set filename=%1
for %%f in ("%filename%") do (
  set orgName=%%~nf
  set orgExt=%%~xf
)
copy %filename% c:\FolderB\%orgName:~0,-10%%orgExt%

Open in new window

sirbounty

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

Open in new window

Jeremyw

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
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

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
sirbounty

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

Open in new window

sirbounty

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(objFile)
Jeremyw

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(objFile)

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

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
sirbounty

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeremyw

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.
sirbounty

Oops - just tried to post to it apparently right as you deleted it...
Jeremyw

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes