Solved

Copy files based on file name script

Posted on 2011-02-24
3
671 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 56

Expert Comment

by:Bill Prew
ID: 34970972
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

627 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