Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy files based on file name script

Posted on 2011-02-24
3
Medium Priority
?
672 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 2000 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 57

Expert Comment

by:Bill Prew
ID: 34970972
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

722 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