Solved

Batch (.bat) file to  copy files from one folder to another

Posted on 2015-02-09
6
480 Views
Last Modified: 2015-02-21
Hi ,

      I have two folders  - SourceFold  and  DestFold    

      There are several  sets of Files in the sourceFold  (each set has two files in it -   For ex.  1.dgn , 1.txt   )

       So a sample  folder will have  something like  1.dgn , 1.txt  , 2.dgn , 2.txt , 3.dgn, 3.txt  .........

      I need the following batch file

         cut/copy  a set (for example 1.dgn, 1.txt)  from  SourceFold  and copy to DestFold
            ( Important: Make sure  the  ,dgn file is copied first and then the .txt file )

          wait till the Destfolder is empty  ( with some internal process , the copied files will be absorbed .Wait till that)
         
          cut/copy  next set  

           ....   and it goes on  till last set

       can you please help with this Batch File ?

  Thanks in advance
0
Comment
Question by:Sam OZ
6 Comments
 
LVL 3

Expert Comment

by:Shane Kahkola
ID: 40600033
The only thing I know you can use in a batch file that can "monitor" a folder is Robocopy.  If you do it right, you can make Robocopy monitor the SourceFold and copy all sets to the DestFold whenver there is a change in the files.  It can also inspect the file to determine if it is different from an existing file in the destination.

I have attached the help file for Robocopy, but can you tell me a bit more about the context of this file?  What is your overall goal?
robohelp.txt
0
 

Author Comment

by:Sam OZ
ID: 40600058
Hi ,

    Thanks .
             The files are in sets ( 1.dgn, 1.txt , 2.dgn,2.txt ... )    kept in Source folder
             The overall goal is to  copy  files - one set at a time -  from Source folder to Dest folder .              
             But there are two rules to impose
      My key requirement is that  
       1)  the .dgn file  should be copied first and then .txt file  
        2) Copy the next set  only after the current set is sucked out by some internal  process (In other words,  only when the Dest Folder is empty)
0
 
LVL 3

Expert Comment

by:Shane Kahkola
ID: 40600077
In this case, I would recommend a scheduled task that will inspect the destination folder to see if it has files.  If not, you can copy the next set from the source folder.  

I'm not real good with batch file "for" loops, but that's essentially what you want.  Here's an explanation of how the for loop works in batch files:
http://ss64.com/nt/for.html

I'm willing to help you put one together, but I don't have time tonight.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40600109
why a batch file, anyhow, if you are posting in Microsoft development?
I would make this a Service to run in windows, to check in intervals and/or with foldernotification events, not even a scheduled service...
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40600201
@Guy: I think the OP may have been unsure of the categories to use.
0
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 40600216
- Moves numerically named .dgn & .txt file pairs.
- Files are named 1.dgn, 1.txt, 2.dgn, 2.txt, etc.
- Next pair cannot be moved until prior pair is removed.
- Sends results to a log file
- Revise SrcD, DstD, and FNLog folder names to fit your needs
- Copy the code below and save to a file such as MoveFilePairs.bat. I put my files in c:\utils. Adjust the folder name to your taste
- Confirm that it works via a test run of MoveFilePairs.bat in a CMD window.
- To run this on a recurring basis, use Task Scheduler.
-- Set a Trigger per your time frequency.
-- For Action:
--- Program: cmd
--- Add arguments: /c c:\utils\MoveFilePairs.bat
@echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
set SrcD=c:\utils\Source
set DstD=c:\utils\Dest
set FNLog=c:\utils\%~n0.txt
for %%A in (%SrcD%\*.dgn) do (
  set FN=%%~nA
  set Ext=%%~xA
  set /a FNLast=!FN!-1
  if exist %DstD%\!FNLast!!Ext! (
    echo Can't move %SrcD%\!FN!.* till %DstD%\!FNLast!!Ext! is removed
    echo %date% %time% Can't move %SrcD%\!FN!.* till %DstD%\!FNLast!.* is removed>>%FNLog%
    exit /b
  ) else (
    for %%B in (!FN!.dgn !FN!.txt) do call :MoveIt %%B
    exit /b
  )
)
echo Done %0
popd
goto :eof

:MoveIt
move %SrcD%\%1 %DstD%>nul
if %errorlevel% equ 0 (
  echo Moved %SrcD%\%1 to %DstD%
  echo %date% %time% Moved %SrcD%\%1 to %DstD%>>%FNLog%
) else (
  echo Error moving %SrcD%\%1 to %DstD%
  echo %date% %time% Error moving %SrcD%\%1 to %DstD%>>%FNLog%
)

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fizzArray2 challenge 1 102
Access 2013: API code to put hidden database icon in taskbar? 8 96
What are the big features of MVC5? 4 95
Authenticate using sesu from script 7 110
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

756 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