Renaming files using a batch script
Results 1 to 10 of 10

Thread: Renaming files using a batch script

  1. #1
    Join Date
    Oct 2004
    Posts
    3

    Question Renaming files using a batch script

    I receive a file from an external source on my PC each day with the name format:

    FILE-01_CSV.yyyymmdd.output.csv

    I have a batch script which moves this file to a shared directory. However, my users can't automatically import the file into their database because of the dots in the filename. Is there a simple way to rename the file, keeping the .csv extension but replacing all the other dots in the filename with an underscore? My script runs on NT. Thanks in advance.


  2. #2
    Join Date
    Jan 2000
    Location
    EnZed
    Posts
    2,532
    Hi Dave, Sorry for the delay in replying.
    Your batch file uses simple DOS commands.
    Use the COPY command to copy it from source directory to destinantion directory with new filename.
    e.g.
    Code:
    COPY FILE-01_CSV.yyyymmdd.output.csv d:\sharedir\FILE-01_CSV_yyyymmdd_output.csv
    smurfy
    Linux Counter #318033 Free yourself - Get Linux now
    Running:
    Ubuntu, OpenSuse, Mandriva, FedoraCore, Debian, Slackware and various Windows versions

  3. #3
    Join Date
    Oct 2004
    Posts
    3
    Hi Smurfy,

    That would be fine if I was working with a fixed filename but the filename changes each day (the yyyymmdd being the date). I think that I really need some sort of filename manipulation routine which parses the name of the file coming in to build up the name of the output file.

  4. #4
    Nix's Avatar
    Nix is offline Aka: Nix*, NNiixx, Nix23
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    8,255
    Is it possible to get the people who generate the file to name it FILE-01_CSV_yyyymmdd_output.csv ?

    Then you don't need to rename it.

  5. #5
    Nix's Avatar
    Nix is offline Aka: Nix*, NNiixx, Nix23
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    8,255
    Or try this
    Code:
    Set CURRDATE=%TEMP%\CURRDATE.TMP
    DATE /T > %CURRDATE%
    
    Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
    For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%j
    
    copy FILE-01_CSV.yyyymmdd.output.csv x:\share\FILE-01_CSV_%yyyymmdd%_output.csv
    Which basically works out what the date is in YYYYMMDD format and then copies the file renaming and inserting the date.

    With thanks to http://discussions.virtualdr.com/sho...hreadid=157827

    and thus http://www.winnetmag.com/Article/Art...575/13575.html
    Last edited by Nix; October 18th, 2004 at 10:24 PM.

  6. #6
    Join Date
    Oct 2004
    Posts
    3
    Hi Nix,

    That's almost what I need. The only problem that remains to be solved is that the file arrives just after midnight and has the previous day's date. How do I subtract 1 from the current machine date?

  7. #7
    Nix's Avatar
    Nix is offline Aka: Nix*, NNiixx, Nix23
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    8,255
    I have sent a PM to Vernon Frazee who is our resident expert of DOS batch files.

    Hopefully he can assist.

  8. #8
    Join Date
    Feb 2000
    Location
    26.03°N 80.14°W
    Posts
    9,410
    Don't have a NT machine handy to try the following BATch file on but it looks like it'll do what you're after on XP:
    Code:
    @echo off
    if exist FILE-01_CSV.*.csv goto Begin
    echo.
    echo Error: No "FILE-01_CSV.*.csv" files exist in the current directory
    goto End
    :Begin
    echo The following file(s):
    dir/b FILE-01_CSV.*.csv
    echo.
    echo has/(have) been renamed to:
    dir/b FILE-01_CSV.*.csv>$temp$.tmp
    for /f "eol=; tokens=1,2,3,4 delims=." %%i in ($temp$.tmp) do ren %%i.%%j.%%k.%%l %%i_%%j_%%k.%%l
    del $temp$.tmp
    dir/b FILE-01_CSV_*.csv
    :End
    Note that you'll need to be in the directory (folder) along with your "FILE-01_CSV.yyyymmdd.output.csv" files before running the above.
    Vernon Frazee, Microsoft MVP (Windows - Shell/User)

    Defenses Up!
    Tip: When prompted for a password, give an incorrect one first. A phishing site will accept it; a legitimate one won't.


    Inside Spyware: A Guide to Finding, Removing and Preventing Online Pests


    If you don't keep up with security fixes, your computer|network won't be yours for long.

  9. #9
    Join Date
    Aug 2017
    Posts
    1
    Quote Originally Posted by DaveVod View Post
    I receive a file from an external source on my PC each day with the name format:

    FILE-01_CSV.yyyymmdd.output.csv

    I have a batch script which moves this file to a shared directory. However, my users can't automatically import the file into their database because of the dots in the filename. Is there a simple way to rename the file, keeping the .csv extension but replacing all the other dots in the filename with an underscore? My script runs on NT. Thanks in advance.



    Hello,
    There is a simple solution for your trouble. I suggest you to try BatchRenameFiles Tool program and you can change pretty much everything you want.

  10. #10
    photolady's Avatar
    photolady is offline Lifetime Friend of Site Staff
    Join Date
    Mar 2002
    Location
    At my computer, cruising VDR and watching your back
    Posts
    23,412
    Please be aware this thread is 13 years old. And as such will now be closed.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •