Solved

Command Prompt files move from one dir to other . bat

Posted on 2013-01-16
7
729 Views
Last Modified: 2013-01-16
Hello I hope someone can help me with one sample code in ms-dos command prompt .bat file to move files from one dir to other and repeat automatic again and again endless but this is what I need:

1.-I have 2 dirs first dir called c:\dir1 files will be copied to this directory automatic by users no needed to do nothing there, now I want the .bat file when execute each time the .bat (because remember the .bat must autorun forever and ever), but when execute at first time of each automatic run the .bat must make a pause of 2 minutes then move one file of c:\dir1 (just one file of the dir will be moved the other files not copied must be untouched in c:\dir1) to the other dir called c:\dir2, when runs again the .bat file the .bat will make the pause of 2 minutes then again move only other file from c:\dir1 to c:\dir2 and in that way in an endless loop.
2.-If c:\dir1 is empty then the autorun does nothing just make the pause of 2 minutes.

looking the procedure visually this is what it does:
a.-Initial run check if no files are on c:\dir1 if no files then make a pause of 2 minutes only and re run again the .bat if files found in the dir then:
b.-Make a pause of 2 minutes waiting time (no problem if other 2 minutes of wait time if no files found and after files found by the .bat).
c.-Then move one file of the dir c:\dir1 to c:\dir2 with no particular characteristics or order of the file to be moved just the .bat read the dir and choose the file, only one file of the list of files of that c:\dir1 and move to c:\dir2 like I said.
d.-After move the file the script re run again in othwr words starts again from point "a" from this list I made and on hat way forever and ever the script must run.

If you see the goal is move all files of c:\dir1 to c:\dir2 slowly in other words the c:\dir1 will be enpty after move all files, off course if users copy more files to c:\dir1 the script detect that and continue doing the work like the points above described.

I don't know so much of command prompt but I need that I hope someone can help in sample code to do.
Thank you
0
Comment
Question by:coerrace
  • 4
  • 3
7 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38785460
I think this does what you described, if I understood it correctly.

@echo off
setlocal

REM Set folder paths to work with
set BaseDir=c:\dir1
set DestDir=c:\dir2

:MainLoop
  REM Delay 2 minutes
  ping 1.1.1.1 -n 1 -w 120000>NUL

  REM If any files in source, copy one, and loop
  for %%F in ("%BaseDir%\*.*") do (
    move "%%~F" "%DestDir%"
    goto :MainLoop
  )

  REM No file found, loop
  goto :MainLoop

Open in new window

~bp
0
 

Author Comment

by:coerrace
ID: 38785510
Works excellent you understand perfect thank you so much just I want to ask if exist any other way to make the pause of 2 minutes because if I'm not connected to Internet the ping command not make the pause of 2 minutes if on other words I need to be connected in Internet to ping works but sometimes the .bat will run in a computer without Internet, then any other way to make the pause?
Thank you
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38785523
What version of Windows are you running?

Also, would it be acceptable to install a small EXE utility on the machine the script will run on?

~bp
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:coerrace
ID: 38785545
The machines can be windows 2008 server.
Thank you
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38785556
Try this instead:

timeout /T 120

~bp
0
 

Author Comment

by:coerrace
ID: 38785562
works perfect thank you for all!!!!!!!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38785577
Welcome.

~bp
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now