Solved

A scripting batch file

Posted on 2014-11-09
19
156 Views
Last Modified: 2014-11-14
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
0
Comment
Question by:rakkad
  • 7
  • 6
  • 6
19 Comments
 

Expert Comment

by:DrHost
ID: 40431098
@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
 

Expert Comment

by:DrHost
ID: 40431099
Sorry forgot to mention this a CMD Batch file
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40431183
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
 

Author Comment

by:rakkad
ID: 40432593
~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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40433237
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
 

Author Comment

by:rakkad
ID: 40433491
Hi

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

Thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40433695
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
 

Author Comment

by:rakkad
ID: 40434800
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
 

Expert Comment

by:DrHost
ID: 40434844
Rak,

I wrote you a script and you are totally ignoring it.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:rakkad
ID: 40434973
This script works, but require some adjustments if you can do that.  That would be much appreciated.

Thanks
0
 

Expert Comment

by:DrHost
ID: 40434979
What adjustments let me know what you need and i'll be happy to accomodate
0
 

Author Comment

by:rakkad
ID: 40434994
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
 

Expert Comment

by:DrHost
ID: 40435007
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40435081
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
 

Author Comment

by:rakkad
ID: 40437751
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
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40437805
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
 

Expert Comment

by:DrHost
ID: 40438266
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
 

Author Closing Comment

by:rakkad
ID: 40443428
Excellent script

Thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40444022
Welcome, glad that was useful.

~bp
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

17 Experts available now in Live!

Get 1:1 Help Now