Link to home
Start Free TrialLog in
Avatar of Jeremyw
JeremywFlag 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
Avatar of sirbounty
sirbounty
Flag of United States of America image

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

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

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

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

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)
Avatar of 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

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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.
Oops - just tried to post to it apparently right as you deleted it...
Avatar of 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