1

I would like to create a .bat file in order to download and make following operations on 2 files (FILE1-Date.7z & FILE2-Date.7z) whose names are updated with "Date" information everyday. The batch file should do the below operations automatically ;

  1. Download FILE1-Date.7z & FILE2-Date.7z from internet page (i.e. https://collaboration.xxxgroup.com) into specified folder (D:/etc...), and this page requires username & password.

  2. Extract the files (file type is .mdb) inside .7z files to the same folder in which .7z files exist

  3. Delete old (existing) files (FILE1.mdb & FILE2.mdb) in the folder

  4. Rename FILE1-Date.mdb to FILE1.mdb & FILE2-Date.mdb to FILE2.mdb

Is it possible to do these operations by using a batch file or I need to use a totally different tool? Any suggestions?

NOTE : Changed "intranet" with "internet" in step 1, and added more details about web site that I will download the files. Also changed naming format of file WAS: FILE_Date BECOME: FILE-Date (example: LANGUAGES-26-01-2015)

2

You can use a batch file to do this. Without more details, it's hard to provide specifics, but in general, do the following:

  • Use the copy command to copy from the intranet to the specified folder.
  • Use %PROGRAMFILES%\7-Zip\7z.exe for extracting files and overwriting older ones. More Help Here.
  • Use the following for getting the current date:
    set DT=

    for /f "skip=1 delims=" %%A in ('wmic os get localdatetime') do (
        if not defined DT set DT=%%A)

    set YYYY=%DT:~0,4%
    set MM=%DT:~4,2%
    set DD=%DT:~6,2%
    set DATE=%YYYY%_%MM%_%DD%
  • Use the following to rename your file_date.mdb files to file.mdb:
    for /f "tokens=1,2 delims=_" %%A in ('dir /b *.mdb') do ren %%A_%%B %%A.mdb

This should help get you started. Please post a new question with specifics on what you have tried and what isn't working if you run into troubles crafting the batch file.

  • Thanks for sharing the first steps, I have realized that the link for files is on internet not on intranet. So I edited my question (Step1) So I think I should use another function in order to download files from internet (asking login and password if it's not saved on cache) – NT. Jan 27 '15 at 5:35
  • I've started by renaming function. Your rename code works fine with "" delimiter but when I try to use it with "-" delimiter it does not work, do I make sth. wrong? Just replaced "" by "-" in the code --> for /f "tokens=1,2 delims=-" %%A in ('dir /b *.mdb') do ren %%A_%%B %%A.mdb – NT. Jan 27 '15 at 5:48
  • The updates to your question changed the file names to use multiple hyphens rather than a single underscore to separate the name and date. You'll need to change the rename code to: for /f "tokens=1* delims=-" %%A in ('dir /b *.mdb') do ren "%%A-%%B" "%%A.mdb" – user387876 Jan 27 '15 at 16:00
  • I see you also changed "Intranet" to "Internet". That's a bit trickier to do from a batch file, but it is possible: How to download files from the command line. – user387876 Jan 27 '15 at 16:08
  • 1
    The original renaming code looks for an underscore in the file name and separates that file name into two tokens. FILE_DATE.mdb becomes FILE and DATE. If you replace the underscore with a hyphen and run it against FILE-26-01-2015, you will only get "FILE" and "26". We need it to be "FILE" and "26-01-2015". For this reason, we also need to update how the tokens are defined. That's why the new code uses "tokens=1*" in addition to "delims=-". for /f "tokens=1* delims=-" %%A in ('dir /b *.mdb') do ren "%%A-%%B" "%%A.mdb" – user387876 Jan 27 '15 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.