Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

Copy files based on file name script

Hi Experts

I am looking for a script to run under Windows XP/Server 2003 which will copy and move files to a location determined by their name and alter their file extension.

Basically It has to be triggered by a .bat file but I can call a VBScript etc from within the batch

example:
in c:\testing\
12345678_ordrsp.128475
12345678_ordrsp.178548
56789101_ordrsp.787955

Two variables are:

%customer%
%messagetype%

These are as follows in example:
12345678 and 56789101 are the %customer%, these are always the first characters before the first underscore

ordrsp is the %messagetype% - these are always the 6 characters after the first _


Step 1
Files should end up with the . changed to _ and having .xml at the end
12345678_ordrsp_128475.xml
12345678_ordrsp_178548.xml
56789101_ordrsp_787955.xml

Step 2
Copy the files to c:\testing\backup\%customer%\%messagetype%

Step 3
Move the files to c:\testing\outbound\%customer%\%messagetype%

Not essential but if the folders with the values behind %customer% and %messagetype% did not exist then it would be useful the script would create them.

HELP :)

ps. To get the examples uploaded I had to rename them .bmp as EE wont allow the extension types .128475 etc



testing.zip
0
failed
Asked:
failed
1 Solution
 
prashanthdCommented:
Test the following code, it will do the following

1)Rename the files as specfied
2)Copy the files as specified and create %customer% and %messagetype% folders if not exist
2)Move the files as specified and create %customer% and %messagetype% folders if not exist
On Error Resume Next

sFolder = "c:\testing\"

Set objfso = CreateObject("Scripting.FileSystemObject")

Set folder = objfso.GetFolder(sFolder)
Set files = folder.Files

For Each file In files
    If InStr(file.name,".") > 0 Then
        'WScript.Echo file.Path
        newfilename=Replace(file.Name,".","_")&".xml"
        'WScript.Echo newfilename
        newfilepath=Replace(file.Path,file.Name,newfilename)
        'WScript.Echo Newfilepath
        customer=Mid(file.Name,1,InStr(file.Name,"_")-1)
        messagetype=Mid(file.Name,InStr(file.Name,"_")+1,6)
        'WScript.Echo customer & " - " & messagetype
        objfso.MoveFile file.path , newfilepath
        'WScript.Echo "c:\testing\backup\"&customer&"\"&messagetype
        If objfso.FolderExists("c:\testing\backup\"&customer) Then
            'WScript.Echo "exists1"
        Else
            objfso.CreateFolder("c:\testing\backup\"&customer)
        End If
        
        If objfso.FolderExists("c:\testing\backup\"&customer&"\"&messagetype) Then
            'WScript.Echo "exists1"
        Else
            objfso.CreateFolder("c:\testing\backup\"&customer&"\"&messagetype)
        End If
        If objfso.FolderExists("c:\testing\outbound\"&customer) Then
            'WScript.Echo "exists2"
        Else
            objfso.CreateFolder("c:\testing\outbound\"&customer)
        End If
        
        If objfso.FolderExists("c:\testing\outbound\"&customer&"\"&messagetype) Then
            'WScript.Echo "exists2"
        Else
            objfso.CreateFolder("c:\testing\outbound\"&customer&"\"&messagetype)
        End If
        
        copy_path="c:\testing\backup\"&customer&"\"&messagetype &"\"
        move_path="c:\testing\outbound\"&customer&"\"&messagetype&"\"
        
        objfso.CopyFile newfilepath,copy_path
        objfso.MoveFile newfilepath,move_path
        
    End If
Next

Open in new window

0
 
ThommyCommented:
Try this batch script...

CopyFiles.cmd has to be stored in a different path (not in c:\testing)!!!
@echo off
set BasePath=C:\TESTING
FOR /F "tokens=1,2,3 delims=_." %%A IN ('DIR %BasePath% /B /A-D') DO (
  if not exist %BasePath%\backup\%%A md %BasePath%\backup\%%A
  if not exist %BasePath%\backup\%%A\%%B md %BasePath%\backup\%%A\%%B 
  copy c:\testing\%%A_%%B.%%C c:\testing\backup\%%A\%%B\%%A_%%B_%%C.xml
)

Open in new window

CopyFiles.cmd
0
 
Bill PrewCommented:
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now