Results 1 to 4 of 4

Thread: Need a Batch script to check files

  1. #1
    Join Date
    Dec 2008
    Posts
    50

    idea Need a Batch script to check files

    Hey Guys,

    I would like to know that which is the best Batch script for checking files. I would like to have a batch script for the following.

    1) If say there is a folder A, in which there are 10 files which are updated daily. I would like to check this files and if the files are updated with latest timestamp.
    2) If any of the file is been not updated, then it should be show a display error, saying this file is not been updated.
    3) And on destination, before copying i need to have archive existing files.

    Is it possible for me to do so? Can any body provide me the correct logical steps for doing it? Any kind of information on the above issue would be appreciated.

    Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Need a Batch script to check files

    OK file exists bit is easy:

    @echo off
    if exists "c:\dir\yourfile.txt" echo It exists
    if not exists "c:\dir\yourfile.txt" echo It doesn't exist

    To get number of files is slightly more difficult:

    @echo off
    for /f "tokens=1 delims= " %%a in ('dir ^| find "File(s)"') do echo %%a

    You can check that with

    @echo off
    set numfiles=0
    for /f "tokens=1 delims= " %%a in ('dir ^| find "File(s)"') do set numfiles=%%a
    if %numfiles% LSS 30 echo Less than 30 files

    hth

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Need a Batch script to check files

    You did not mention which timestamp - last modification time, last acces time or last creation time. I will assume last modification time.

    Here is the script. It is written in biterscripting. It will copy files modified after a certain time stamp from folder A to B.

    # Get a list of all files whose time stamp is after 20090418.
    var str filelist ; find -n "*" "folder A" ($fmtime > "20090418") > $filelist
    while ($filelist <> "")
    do
    # Get the next file.
    var str file ; lex "1" $filelist > $file
    # Copy file to folder B.
    system copy $file "folder B"
    done
    The above script will copy all files modified after Apr 18, 2009.

    Copy the above script into file C:/X.txt. Start biterscripting and call the script as follows.
    script "C:/X.txt"
    That's it.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Need a Batch script to check files

    OK, this is slightly more difficult. Variables are passed into a batch file as command line parameters. An example of this would be the following:
    Code for making a 'new' copy batch file - Filename: c:\copier.bat

    @ECHO OFF
    COPY %1 %2
    This would be used as follows:

    copier testfile.txt c:\folder

    This would, in the same way as copy does, copy the file from c:\ to c:\folder\ by calling copy to do it. Not very helpful yet, but you can use this a bit better with a nested batch file.

    File 1: test1.bat
    @ECHO OFF
    CALL test2.bat file.txt
    ECHO.
    ECHO The code here has now run
    File2: test2.bat
    @ECHO OFF
    IF EXIST %1 (
    DEL %1
    ) ELSE (
    ECHO.
    ECHO File does not exist
    ECHO.
    )
    This would check to see if the file stored in batch file 1 exists, then if it does, deletes it, otherwise lets you know. After that, it returns to the first batch file where it left off. If you left out the CALL command it would go and run the second file then immediately terminate without returning.

    If you know BASIC, it equates to the CALL command is being a GOSUB with the end of the next file being a RETURN, whereas just the filename is a GOTO.

    Hope so it may help you out.

Similar Threads

  1. Batch Script Issue
    By spmaguire in forum Software Development
    Replies: 3
    Last Post: 04-09-2013, 10:41 AM
  2. Batch Script to Convert PNG to XPM
    By BRIGHID in forum Operating Systems
    Replies: 3
    Last Post: 22-08-2010, 03:25 AM
  3. SharePoint Batch Check
    By RICO12 in forum Windows Software
    Replies: 4
    Last Post: 06-01-2010, 02:59 AM
  4. Need a batch script to map drive
    By Common in forum Networking & Security
    Replies: 3
    Last Post: 06-07-2009, 07:42 PM
  5. Several. Msc window from a batch / script call?
    By Stephanatic in forum Technology & Internet
    Replies: 2
    Last Post: 26-11-2008, 05:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,260,199.56297 seconds with 17 queries