Solved

Copy files based on file name script

Posted on 2011-02-24
3
663 Views
Last Modified: 2012-05-11
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
Comment
Question by:failed
3 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 34968880
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
 
LVL 19

Expert Comment

by:Thommy
ID: 34968950
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 34970972
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now