• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 885
  • Last Modified:

Help With batch file

Hi,

Im wanting to create a batch file that takes the first directory in the structure, uses it as a variable, check to see if there is a file in that directory, then copy it to another directory.

At the moment we use a batch like this:

copy 001\HTML\BINV-MON-001.HTM   001\DOWNLOAD\BINV-MON-001.HTM
copy 333\HTML\BINV-MON-333.HTM   333\DOWNLOAD\BINV-MON-333.HTM
copy 407\HTML\BINV-MON-407.HTM   407\DOWNLOAD\BINV-MON-407.HTM
copy AKKF\HTML\BINV-MON-AKKF.HTM   AKKF\DOWNLOAD\BINV-MON-AKKF.HTM
copy BEER\HTML\BINV-MON-BEER.HTM   BEER\DOWNLOAD\BINV-MON-BEER.HTM
copy BME\HTML\BINV-MON-BME.HTM   BME\DOWNLOAD\BINV-MON-BME.HTM
copy BST\HTML\BINV-MON-BST.HTM   BST\DOWNLOAD\BINV-MON-BST.HTM
copy CHAR\HTML\BINV-MON-CHAR.HTM   CHAR\DOWNLOAD\BINV-MON-CHAR.HTM
copy CJLL\HTML\BINV-MON-CJLL.HTM   CJLL\DOWNLOAD\BINV-MON-CJLL.HTM
copy CSGS\HTML\BINV-MON-CSGS.HTM   CSGS\DOWNLOAD\BINV-MON-CSGS.HTM
copy HOLL\HTML\BINV-MON-HOLL.HTM   HOLL\DOWNLOAD\BINV-MON-HOLL.HTM
copy JOR\HTML\BINV-MON-JOR.HTM   JOR\DOWNLOAD\BINV-MON-JOR.HTM
copy LAIT\HTML\BINV-MON-LAIT.HTM   LAIT\DOWNLOAD\BINV-MON-LAIT.HTM
copy LINK\HTML\BINV-MON-LINK.HTM   LINK\DOWNLOAD\BINV-MON-LINK.HTM
copy MOUK\HTML\BINV-MON-MOUK.HTM   MOUK\DOWNLOAD\BINV-MON-MOUK.HTM
copy NPFX\HTML\BINV-MON-NPFX.HTM   NPFX\DOWNLOAD\BINV-MON-NPFX.HTM
copy PMON\HTML\BINV-MON-PMON.HTM   PMON\DOWNLOAD\BINV-MON-PMON.HTM
copy PSTN\HTML\BINV-MON-PSTN.HTM   PSTN\DOWNLOAD\BINV-MON-PSTN.HTM
copy QEXP\HTML\BINV-THU-QEXP.HTM   QEXP\DOWNLOAD\BINV-THU-QEXP.HTM
copy RAYA\HTML\BINV-MON-RAYA.HTM   RAYA\DOWNLOAD\BINV-MON-RAYA.HTM
copy SMS\HTML\BINV-MON-SMS.HTM   SMS\DOWNLOAD\BINV-MON-SMS.HTM

now i want the batch file to go through and read the first directory put it in a variable eg. curdir="001". Then i want it to go and check to see if the html file is there, if it is there i then want to copy it to the %curdir\DOWNLOAD\  directory, then move on to the next directory in the structure and do the same thing.
can any one help me with this?
0
Anthony_E
Asked:
Anthony_E
  • 2
  • 2
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
This should do the trick if I understand what your asking for.  The following is the command you would use if you do NOT put this in a batch file.  If you put this in a batch file, make sure to change all %a references to %%a

for /f "tokens=1" %a in ('dir /ad /b') do @if exist %a\HTML\BINV-MON-%a.HTM copy %a\HTML\BINV-MON-%a.HTM %a\DOWNLOAD\BINV-MON-%a.HTM
0
 
Anthony_EAuthor Commented:
Thanks leew!!, worked like a charm

just 1 more thing, how can i get it to echo what file is being copied as its going through the process?

ive got:
for /f "tokens=1" %%a in ('dir /ad /b') do @if exist %%a\HTML\BINV-MON-%%a.HTM copy %%a\HTML\BINV-MON-%%a.HTM %%a\DOWNLOAD\BINV-MON-%%a.HTM

do i add it at the end by putting a & there or what? like &echo %aa\HTML\BINV-MON-%%a.htm COPIED?
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I'd probably do this:
for /f "tokens=1" %%a in ('dir /ad /b') do @if exist %%a\HTML\BINV-MON-%%a.HTM copy %%a\HTML\BINV-MON-%%a.HTM %%a\DOWNLOAD\BINV-MON-%%a.HTM & Echo %aa\HTML\BINV-MON-%%a.htm COPIED

Or to log the copies,

for /f "tokens=1" %%a in ('dir /ad /b') do @if exist %%a\HTML\BINV-MON-%%a.HTM copy %%a\HTML\BINV-MON-%%a.HTM %%a\DOWNLOAD\BINV-MON-%%a.HTM & Echo %aa\HTML\BINV-MON-%%a.htm COPIED > copy.log
0
 
Anthony_EAuthor Commented:
thanks again
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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