Script to Auto Rename Multiple Files in Windows Explorer


  1. Posts : 3
    Windows 7 32 bit
       #1

    Script to Auto Rename Multiple Files in Windows Explorer


    Is there a way to automate the renaming of multiple files in windows explorer which will also add the Julian date at the beginning of each file name.

    I have a sign-in roster for my employees at work to digitally sign each day when the report to work and again before the depart. I have been taking a single PDF file and copy/pasting it into a folder for each month for as many days there are in the month. I manually change the file name to add each Julian date to the beginning so the employees will know which file to open for that particular day at work. I have 5 different sections so this becomes an extremely tedious task.

    Please help.
      My Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    Hi IndyColtsMan,

    There sure is a way to rename files to your desire in an automated fashon via Windows scripting features.

    Firstly, more information will be needed.

    I need you to clarify or better explain what exactly your situation is. You must be precise in explaining what needs to happen to your input file A.pdf to get your output file B.pdf name.

    If you could provide a sample directory tree of your situation, and/or even better; actual sample file names with paths to work with, it would really help.

     
      My Computer

  3.   My Computer


  4. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #4

    IndyColtsMan said:
    I hope this helps explain what I’m trying to do.
    I understand what you're trying to achieve now and ... honestly, it doesn't sound easy to turn into a script, let alone a batch script -- the ideal way to automate thing in Windows. But I'll try my best.

    Taking a guess at the folder structure implied; suppose it looks something like:

    Code:
    C:\somepath\documents\2014\November
    With
    Code:
    C:\somepath\documents\2014\November\305 Liaison Office.pdf
    being the file for the first of November, 2014. Correct? Because it has been 305 days since... 0/1/2014?


    Is the location of the 'original' Liaison Office.pdf (the one that you copy and paste each month) in this directory tree somewhere? If not, where might its location be, relative from say C:\somepath\documents\2014? So that the script may access its location for copying.

    The title of this thread is asking to rename muliple files in a folder. But as your situation stands, I reckon it would be of your best interest to extend what you're asking for, and instead, ask for a script that automatically lays out the folder structure for you for that month (or possibly even for that entire year) and copies and renames the correct amount of files as days in that month appropriately. Is this prefered?
      My Computer


  5. Posts : 3
    Windows 7 32 bit
    Thread Starter
       #5

    Pyprohly,

    I have the original file in a separate directory on my hard drive. I have 5 different sections that I do this for (see below) and the actual folders that the employees access are on a shared network drive (V:\HTSI AFSB Kuwait (IZ) Staff Augmentation\ATTENDANCE TRACKER\Attendance Rosters) within another directory labeled as such for the month "305-334 (Nov) 2014". I could actually create another folder and place a copy of all five original files inside the Attendance Rosters folder.

    I do believe your suggestion would be better. I would want the automated folders placed inside the folder labeled "Pending" as I only have the folder for the current month visible to the employees.

    Does this help?
    Attached Thumbnails Attached Thumbnails Script to Auto Rename Multiple Files in Windows Explorer-folders.jpg   Script to Auto Rename Multiple Files in Windows Explorer-folders-2.jpg  
      My Computer


  6. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #6

    Here's a script that answers your initial question:

    Preview:
    Code:
    @echo off
    setlocal EnableDelayedExpansion
    REM Duplicate a file for as many days there are in the current month
    REM And rename each file with a Julian date infront of the file name
    pushd "%~dp0"
    call :GetCurrDate
    call :SetFebruary
    call :GetMonthName %MM%
    call :GetJulianDay %MM% %DD%
    call :GetMaxDaysInMonth %MM%
    call :GetMonthRange %MM%
    REM Check that only one other file is in the folder
    for /f "delims=" %%I in (' dir /b /a:-d "*.*" ^| find /v "%~nx0" ^| find /c /v "" ') do (
     if "%%I" NEQ "1" echo.& echo Error: Expected exactly 1 other file in directory.& timeout 2 >NUL& goto :eof
    )
    REM Copy files n number of times for days in current month
    set counter=0
    for /f "delims=" %%F in (' dir /b "*.*" ^| find /v "%~nx0" ') do (
     for /l %%I in (1,1,%DD_MaxDaysInMonth%) do (
      set /a counter+=1
      copy "%~dp0\%%~nxF" "%~dp0\tmp!COUNTER!%%~xF" >NUL
     )
    set FileName=%%~nF
    set FileExtn=%%~xF
    del "%%~fF"
    goto :break
    )
    :break
    REM Rename files in folder
    set counter=%MIN_JulianDayInMonth%
    for /f "delims=" %%F in (' dir /b "*.*" ^| find /v "%~nx0" ') do (
     ren "%%~fF" "!COUNTER! %FILENAME%%FILEEXTN%"
     if !COUNTER!==%MAX_JulianDayInMonth% goto :break
     set /a counter+=1
    )
    :break
     
    :End
    popd
    echo.&echo Success.
    goto :eof
    REM End
    REM Functions below
    :GetCurrDate
    setlocal
     for /f "delims=" %%I in (' date /t ') do (
      set currDate=%%I
      set currDate=!CURRDATE: =!
      set currDate=!CURRDATE:~4!
     )
     for /f "tokens=1-3 delims=/" %%A in ("%CURRDATE%") do (
      set DD=%%A
      set MM=%%B
      set YYYY=%%C
      set Index=!MM!
     )
    endlocal & set CurrDate=%CURRDATE%& set DD=%DD%& set MM=%MM%& set YYYY=%YYYY%&
    goto :eof
    :SetFebruary
    setlocal
     set /a Leapyear=YYYY %% 4
     if %LEAPYEAR%==0 ( set Feb=29) else ( set Feb=28)
    endlocal & set Feb=%FEB%
    goto :eof
    :GetMonthName MM
    setlocal
     set Index=%1
     for /f "tokens=%INDEX%" %%M in (
      " January February March April May June
       July August September October November December "
     ) do set MM_Name=%%M
    endlocal & set MM_MonthName=%MM_Name%
    goto :eof
    :GetJulianDay MM DD
    setlocal
     set Index=%1
     set /a Index-=1
     for /l %%I in (1,1,%INDEX%) do call :GetJulianDay_Sub %%I
     set /a JulianDay+=%2
    endlocal & set DD_JulianDay=%JULIANDAY%
    goto :eof
    :GetJulianDay_Sub
     for /f "tokens=%1" %%M in (
     " 31 %Feb% 31 30 31 30 31 31 30 31 30 31 "
     ) do ( set /a JulianDay+=%%M)
    goto :eof
    :GetMonthRange MM
    setlocal
     set Index=%1
     
     call :GetMaxDaysInMonth %INDEX%
     set DD=%DD_MaxDaysInMonth%
     call :GetJulianDay %INDEX% %DD%
     set MAX_JulianDayInMonth=%DD_JulianDay%
     
     set DD=1
     call :GetJulianDay %INDEX% %DD%
     set MIN_JulianDayInMonth=%DD_JulianDay%
     
     set MonthRange=%MIN_JulianDayInMonth%-%MAX_JulianDayInMonth%
    endlocal & set MIN_JulianDayInMonth=%MIN_JulianDayInMonth%& set MAX_JulianDayInMonth=%MAX_JulianDayInMonth%& set MonthRange=%MONTHRANGE%&
    goto :eof
    :GetMaxDaysInMonth MM
    setlocal
     set Index=%1
     for /f "tokens=%INDEX%" %%M in (
      " 31 %Feb% 31 30 31 30 31 31 30 31 30 31 "
     ) do set DD_MaxDays=%%M
    endlocal & set DD_MaxDaysInMonth=%DD_MaxDays%
    goto :eof

    Instructions:
    1) Place the batch file and a single pdf in a directory of your choice.
    2) Run the batch script.

    Note: for a date such as 4/3/yyyy, '62' will be the Julian date displayed on the pdf. Would you like this number to be represented by 3 digits instead? E.g. '062'

    Feel free to mark the thread as solved while a more complex script for your task is underway.

    Meanwhile, thank you for supplying a directory tree, it helps visualise the situation. I'm almost on the same page as you are now, but I'm a little confused on where Liaison Office.pdf files are situated from the directory tree you describe. From what I'm getting, you're saying you do the tedious task of copying and renaming 'Liaison Office.pdf' 5 times each month for the 5 folders inside 'ATTENDANCE TRACKER'?
    Script to Auto Rename Multiple Files in Windows Explorer Attached Files
      My Computer


  7. Posts : 1
    Windows 10 64 bit
       #7

    This is a great technique. You can also check a free software named: BatchRenameFiles Tool. Search on google- BatchRenameFiles Tool and kindly check the first result.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:29.
Find Us