Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1573
  • Last Modified:

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

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
Sam OZ
Asked:
Sam OZ
1 Solution
 
Shane KahkolaDirector of I.T.Commented:
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
 
Sam OZAuthor Commented:
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
 
Shane KahkolaDirector of I.T.Commented:
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
Industry Leaders: 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!

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
NVITCommented:
@Guy: I think the OP may have been unsure of the categories to use.
0
 
NVITCommented:
- 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now