A scripting batch file

A script batch file that will do the following (DOS, vbscript or Powershell) which is the preference ?

1. There will be three files which will be coming on and daily basis to \\dcawin102\processedfiles but will be different.  These files are:-

     Albany.20141108
     TRN.20141108
     BRN.20141108

1. These files have a datestamp, so therefore the files will need wild cards so the complete files can be copied to \\dcawin101\overnightfiles, however we need to first make sure all three files exist in \\dcawin102\processedfiles before they can get copied to \\dcawin101\overnightfiles, if they do not exist report to a log file stating the files do not exist and showing which files don't exist

2.  Report to a log file stating that the required files are copied to \\dcawin101\overnightfiles if files are not copied report to a log file files not copied and show if files are not copied

Thanks
rakkadAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Okay, this will check the two folders to make sure they exist.

@echo off
setlocal EnableDelayedExpansion

REM Define folders and files to process
set BaseDir=c:\temp\test1
set DestDir=c:\temp\test2
set FileNames="GPL.CRCSV","GPL.MT940","Y01A.CARS"
set LogFile=c:\logfile.txt

REM Make sure input directory exists, exit if not
if not exist ("%BaseDir\%") (
  echo Source folder does not exist: "%BaseDir%">>"%LogFile%"
  exit /b
)

REM Make sure output directory exists, exit if not
if not exist ("%DestDir\%") (
  echo Source folder does not exist: "%DestDir%">>"%LogFile%"
  exit /b
)

REM Check if all files exist overnight before they are copied, if not exit
set FilesMissing=0
for %%A in (%FileNames%) do (
  if NOT exist "%BaseDir%\%%~A*.*" (
    echo Missing file "%BaseDir%\%%~A*.*">>"%LogFile%"
    set /a FilesMissing+=1
  )
)

REM Make sure all file groups found, error out if not
if %FilesMissing% GTR 0 (
  echo Found [%FilesMissing%] missing file groups, cannot copy.>>"%LogFile%"
  exit /b
)

REM Copy all needed files to destination folder
set ErrorFlag=N
for %%A in (%FileNames%) do (
  copy "%BaseDir%\%%~A*.*" "%DestDir%" >NUL 2>>"%LogFile%" || (
    echo Error copying files "%BaseDir%\%%~A*.*" to "%DestDir%".>>"%LogFile%"
    set ErrorFlag=Y
  )
)

REM Check if any errors occured
if "%ErrorFlag%" EQU "Y" (
  echo Errors occured during file copies, see error messages above.>>"%LogFile%"
) else (
  echo Files copied successfully.>>"%LogFile%"
)

Open in new window

~bp
0
 
DrHostCommented:
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
net use a: \\dcawin102\processedfiles
net use b: \\dcawin101\overnightfiles
set nightpath=a:\
set overnightpath=b:\
set logfile=c:\s\log_%datestamp%.txt
set errorlogfile=c:\s\errorlog_%datestamp%.txt

IF EXIST %errorlogfile% set errorlogfile=c:\s\errorlog_%datestamp%_1.txt
IF EXIST %logfile% set logfile=c:\s\log_%datestamp%_1.txt

IF EXIST %nightpath%\trn.* (
copy  %nightpath%\trn.* %overnightpath%\ >> %logfile%
del %nightpath%\trn.* 
) ELSE (
 echo "Cannot find TRN file" >> %errorlogfile%
)

IF EXIST %nightpath%\Albany.* (
copy  %nightpath%\Albany.* %overnightpath%\ >> %logfile%
del %nightpath%\Albany.* 
) ELSE (
 echo "Cannot find Albany file" >> %errorlogfile%
)

IF EXIST %nightpath%\brn.* (
copy  %nightpath%\brn.* %overnightpath%\ >> %logfile%
del %nightpath%\brn.* 
) ELSE (
 echo "Cannot find bRN file" >> %errorlogfile%
)

net use /del a: /y
net use /del b: /y

Open in new window

0
 
DrHostCommented:
Sorry forgot to mention this a CMD Batch file
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Bill PrewCommented:
I think this should do what you were looking for in a BAT script.  Adjust the SET commands near the top as needed.  Currently it copies the files, but not sure if you might want to change that to a MOVE?

@echo off
setlocal EnableDelayedExpansion

REM Define folders and files to process
set BaseDir=\\dcawin102\processedfiles
set DestDir=\\dcawin101\overnightfiles
set FileNames="Albany","TRN","BRN"
set LogFile=c:\temp\logfile.txt

REM Get current date in YYYYMMDD format
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined LocalDateTime set LocalDateTime=%%A
)
set CurrentDate=%LocalDateTime:~0,8%

REM Insert header into log file
echo ======================================== %CurrentDate% ========================================>>"%LogFile%"

REM Check if all files needed exist for today, if not exit
for %%A in (%FileNames%) do (
  if not exist "%BaseDir%\%%~A.%CurrentDate%" (
    echo %DATE% %TIME% - Missing file "%BaseDir%\%%~A.%CurrentDate%", cannot copy.>>"%LogFile%"
    exit /b
  )
)

REM Copy all needed files to destination folder
for %%A in (%FileNames%) do (
  echo %DATE% %TIME% - Copy file "%BaseDir%\%%~A.%CurrentDate%" to "%DestDir%".>>"%LogFile%"
  copy "%BaseDir%\%%~A.%CurrentDate%" "%DestDir%" >>"%LogFile%" 2>&1
)

Open in new window

~bp
0
 
rakkadAuthor Commented:
~bp

I have changed the code to the following (I don't need the timestamp entries...)

@echo on
setlocal EnableDelayedExpansion

REM Define folders and files to process
set BaseDir=c:\temp\test1
set DestDir=c:\temp\test2
set FileNames="GPL.CRCSV*.*","GPL.MT940*.*","Y01A.CARS*.*"
set LogFile=c:\logfile.txt

REM Check if all files exist overnight before they are copied, if not exit
for %%A in (%FileNames%) do (
  if NOT exist "%BaseDir%\%%~A" (
    echo Missing file "%BaseDir%\%%~A" cannot copy.>>"%LogFile%"
    exit /b
  )
)

REM Copy all needed files to destination folder
for %%A in (%FileNames%) do (
  echo Copy file "%BaseDir%\%%~A" to "%DestDir%".>>"%LogFile%"
  copy "%BaseDir%\%%~A" "%DestDir%" >>"%LogFile%" 2>&1
)

I removed the files to check if the first part works, but it doesn't produce a log to state that the files are missing.  Can you help thanks
0
 
Bill PrewCommented:
In more recent versions of Windows the root of C: is protected unless in administrative mode, try storing the log file in a folder rather than the root of C: as a test.

~bp
0
 
rakkadAuthor Commented:
Hi

I changed it to look at c:\temp\test1 but still the same issue, can you try the script yourself

Thanks
0
 
Bill PrewCommented:
Okay, if I am guessing correctly that you want to copy all the files you specified, but only if at least one of each exists, then this might get the job done:\
@echo off
setlocal EnableDelayedExpansion

REM Define folders and files to process
set BaseDir=c:\temp\test1
set DestDir=c:\temp\test2
set FileNames="GPL.CRCSV","GPL.MT940","Y01A.CARS"
set LogFile=c:\logfile.txt

REM Check if all files exist overnight before they are copied, if not exit
for %%A in (%FileNames%) do (
  echo Checking:[%%~A]
  if NOT exist "%BaseDir%\%%~A*.*" (
    echo Missing file "%BaseDir%\%%~A*.*" cannot copy.>>"%LogFile%"
    exit /b
  )
)

REM Copy all needed files to destination folder
for %%A in (%FileNames%) do (
  echo Copy file "%BaseDir%\%%~A*.*" to "%DestDir%".>>"%LogFile%"
  copy "%BaseDir%\%%~A*.*" "%DestDir%" >>"%LogFile%" 2>&1
)

Open in new window

~bp
0
 
rakkadAuthor Commented:
I would like it to report if multiple file(s) are missing.  I did a test to remove all the files, but it reported on the first file missing instead of reporting file(s) which are missing, can you adjust the batch file to include this.

Also once the files are copied, confirm this to a logfile to confirm files are copied or failed.

Thanks
0
 
DrHostCommented:
Rak,

I wrote you a script and you are totally ignoring it.
0
 
rakkadAuthor Commented:
This script works, but require some adjustments if you can do that.  That would be much appreciated.

Thanks
0
 
DrHostCommented:
What adjustments let me know what you need and i'll be happy to accomodate
0
 
rakkadAuthor Commented:
Basically the ones highlighted previously..

I would like it to report if multiple file(s) are missing.  I did a test to remove all the files, but it reported on the first file missing instead of reporting file(s) which are missing, can you adjust the batch file to include this.

 Also once the files are copied, confirm this to a logfile to confirm files are copied or failed.

Thanks
0
 
DrHostCommented:
All this already exists.

the script will create two files log_date.txt and errorlog_date.txt

in case of error per file it will log CANNOT FIND "file" (TRN/BRN/Albany)

if the files were copied it will log which files were copied

current the logs reside in c:\s\ but you can change this

set logfile=c:\s\log_%datestamp%.txt
set errorlogfile=c:\s\errorlog_%datestamp%.txt

change the location to where you want the logs to be at.
0
 
Bill PrewCommented:
Okay, here are the latest changes requested.  Feel free to adjust the messages to the LOG file as needed.

@echo off
setlocal EnableDelayedExpansion

REM Define folders and files to process
set BaseDir=c:\temp\test1
set DestDir=c:\temp\test2
set FileNames="GPL.CRCSV","GPL.MT940","Y01A.CARS"
set LogFile=c:\logfile.txt

REM Check if all files exist overnight before they are copied, if not exit
set FilesMissing=0
for %%A in (%FileNames%) do (
  if NOT exist "%BaseDir%\%%~A*.*" (
    echo Missing file "%BaseDir%\%%~A*.*">>"%LogFile%"
    set /a FilesMissing+=1
  )
)

REM Make sure all file groups found, error out if not
if %FilesMissing% GTR 0 (
  echo Found [%FilesMissing%] missing file groups, cannot copy.>>"%LogFile%"
  exit /b
)

REM Copy all needed files to destination folder
set ErrorFlag=N
for %%A in (%FileNames%) do (
  copy "%BaseDir%\%%~A*.*" "%DestDir%" >NUL 2>>"%LogFile%" || (
    echo Error copying files "%BaseDir%\%%~A*.*" to "%DestDir%".>>"%LogFile%"
    set ErrorFlag=Y
  )
)

REM Check if any errors occured
if "%ErrorFlag%" EQU "Y" (
  echo Errors occured during file copies, see error messages above.>>"%LogFile%"
) else (
  echo Files copied successfully.>>"%LogFile%"
)

Open in new window

~bp
0
 
rakkadAuthor Commented:
The script works if files are present or not.  However, if the destdir is not there, the copy process doesn't produce an error.. Is this something you can add ?

Thanks
0
 
DrHostCommented:
There you go

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
set logfile=c:\s\log_%datestamp%.txt
set errorlogfile=c:\s\errorlog_%datestamp%.txt
IF NOT EXIST  \\dcawin102\processedfiles goto end1
IF NOT EXIST  \\dcawin101\overnightfiles goto end2

net use a: \\dcawin102\processedfiles
net use b: \\dcawin101\overnightfiles
set nightpath=a:\
set overnightpath=b:\


IF EXIST %errorlogfile% set errorlogfile=c:\s\errorlog_%datestamp%_1.txt
IF EXIST %logfile% set logfile=c:\s\log_%datestamp%_1.txt

IF EXIST %nightpath%\trn.* (
copy  %nightpath%\trn.* %overnightpath%\ >> %logfile%
del %nightpath%\trn.* 
) ELSE (
 echo "Cannot find TRN file" >> %errorlogfile%
)

IF EXIST %nightpath%\Albany.* (
copy  %nightpath%\Albany.* %overnightpath%\ >> %logfile%
del %nightpath%\Albany.* 
) ELSE (
 echo "Cannot find Albany file" >> %errorlogfile%
)

IF EXIST %nightpath%\brn.* (
copy  %nightpath%\brn.* %overnightpath%\ >> %logfile%
del %nightpath%\brn.* 
) ELSE (
 echo "Cannot find bRN file" >> %errorlogfile%
)

net use /del a: /y
net use /del b: /y
exit

:end
 echo "Source folder does not exist" >> %errorlogfile

:end2
echo "Destination folder does not exist" >> %errorlogfile

Open in new window

0
 
rakkadAuthor Commented:
Excellent script

Thanks
0
 
Bill PrewCommented:
Welcome, glad that was useful.

~bp
0
All Courses

From novice to tech pro — start learning today.