Solved

Copy files based on file name script

Posted on 2011-02-24
3
666 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 53

Expert Comment

by:Bill Prew
ID: 34970972
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extract field headers and values between quotes 12 51
How can I save all open docs into a given folder 12 113
Problem to With line 4 57
ROBOFTP UNZIP 1 39
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

786 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