Question : Need a bat file to copy specific files from an autonumbered directory

Hello.

I have a process that creates a numbered directory in the path "C:\CSIRP\Storage\".  The number is generated automatically and is different each day.  For example today's path is "C:\CSIRP\Storage\68".  Inside that directory there are approximately 100 files.  I need a batch file that will look at the root directory, "C:\CSIRP\Storage\", find the highest numbered subdirectory, today will be 66 tomorrow will be 67.  Then once I find the correct directory, I need to copy 4 files to path "\\servername\sharename".  The 4 files I need are ALWAYS named:

**CF0364, **DMCIF, **LMCIF, and **NMCIF

Where ** is the same as the number of the directory.  The batch will be scheduled to run multiple times daily, so before it copies the files, it should check the "\\servername\sharename" path for the existance of the files, and abort if the files already exist (no need to copy them twice.)

Please help if you can.

Answer : Need a bat file to copy specific files from an autonumbered directory

Here's a slightly more compact version, that also checks the actual names of the subfolders looking for the one with the highest value.  You may want to stick with the current approach but if not this is an option.  It also uses a couple of PUSHD (CD) commands to reduce the wordiness of the code.

~bp
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
@echo off
setlocal EnableDelayedExpansion
set BaseDir=C:\CSIRP\Storage
set DestDir=\\servername\sharename
set NewDir=0
pushd "%BaseDir%"
REM for /F "tokens=*" %%A in ('dir /ad /od /tc /b') do set NewDir=%%A
for /D %%A in (*) do (
  if %%A GTR !NewDir! set NewDir=%%A
)
pushd "%BaseDir%\%NewDir%"
for %%A in ("??CF0364" "??DMCIF" "??LMCIF" "??NMCIF") do (
  if not exist "%DestDir%\%%~nA" copy "%%~nA" "%DestDir%"
)
popd
popd
Random Solutions  
 
programming4us programming4us