Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: prefix folder name to the new created file  (Read 2839 times)

0 Members and 1 Guest are viewing this topic.

radema

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 10
    prefix folder name to the new created file
    « on: February 05, 2018, 07:57:08 PM »
    I wanted to have a batch file to help me manage my files without installing additional software.

    This is an example of my file naming convention or the final file after renaming ACO-DP-MA-2018-001_01_Proposed_Doc_v1
    where:
    ACO-DP-MA-2018-001 = the folder the file resides
    01 = auto number increment by 1
    Proposed_Doc_v1 = file name created by user

    I wan't to automatically add the folder name as prefix to the newly created file in that folder followed with an auto number with the exemption of the files which were already prefixed. It should also have auto monitoring for newly created file.

    For example when I create a new file inside ACO-DP-MA-2018-001 folder with a file name of Proposed_Doc_v1, my file will be auto renamed into ACO-DP-MA-2018-001_01_Proposed_Doc_v1. Or if I create another file with a file name Report_Doc_v1 in the same folder the final file name will now be ACO-DP-MA-2018-001_02_Report_Doc_v1 and so on and so forth. Else, renaming is skipped if the file name prefix match the folder name.

    I don't have any clue or even a starting point to create the script in scratch. What I did was I do research regarding my problem hoping for a solution but I can only find codes in fragments. I don't know how to combine these codes into one.

    Script with exemption:

    @echo off
    rem filter all files not starting with the prefix 'dat'
    setlocal enabledelayedexpansion
    FOR /R ReplaceWithDirectory %%F IN (*.*) DO (
    set fname=%%~nF
    set subfname=!fname:~0,3!
    IF NOT "!subfname!" == "dat" echo "%%F"
    )
    pause
    Prefixed file name with folder name:

    @echo off
    pushd "Replace with Directory"
    for /d %%P in (*) do for /f "delims=" %%F in ('dir /b /s /a-d "%%P"') do
    rename "%%F" "%%P_%%~nxF"
    popd
    To monitor the folder:

    @Echo OFF
    REM By Elektro H@cker
    PUSHD "Replace with Directory"
    :: Recycle past session logs
    Del /Q "%TEMP%\FileList.tmp","%TEMP%\FileListNew.tmp"
    :Monitor_Loop
    If Exist "%TEMP%\FileList.tmp" (
    Dir /B /A-D > "%TEMP%\FileListNew.tmp"
    Echo N | Comp "%TEMP%\FileList.tmp" "%TEMP%\FileListNew.tmp" 1>NUL 2>&1 || (
        Echo File changes found on directory.
        Call :FileOp
    )
    MOVE /Y "%TEMP%\FileListNew.tmp" "%TEMP%\FileList.tmp" 1>NUL
    ) ELSE (
    Dir /B /A-D > "%TEMP%\FileList.tmp"
    )
    REM Ping -n 5 LOCALHOST 1>NUL
    Timeout /T 5 1>NUL & REM Avoid Ping while you are in Windows 7/8.
    GOTO :Monitor_Loop
    :FileOp
    For %%# in ("*") Do (Echo "%%~#")
    )
    GOTO:EOF

    radema

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows 10
      Re: prefix folder name to the new created file
      « Reply #1 on: February 28, 2018, 12:50:43 AM »
      can someone help me with this code.
      using this ACO-DP-CB-2017_2_001_177-VN-BX_Sec_v5.png as base

      where:
      2 = file number
      001 = group number

      The script will only work with one set of group number in one folder.
      Note: the part of script which add folder name will only work if one subfolder with one file inside is present.

      @echo off
      rem filter all files not starting with the prefix 'dat'
      setlocal enabledelayedexpansion

      set max=0
      set oldfilemax=ACO-*.*
      for /f "delims=" %%F in ('dir /s /b /a-d %oldfilemax%') DO (
      set oldmax=%%~nF
      set /a FI=!oldmax:~15,1!
      if "!FI!" gtr "!max!" set max=!FI!
      rem echo !FI!
      )

      REM ----this will define the old file to match----
      set oldfile=ACO-*.*
      for /f "delims=" %%F in ('dir /s /b /a-d %oldfile%') DO (
      set oldfilename=%%~nF
      set /a oldname=!oldfilename:~17,3!

      )
      REM ----set for the maximum number of file number

      REM ----this will define the new file to rename----
      set newfile=_*.*
      for /f "delims=" %%F in ('dir /s /b /a-d %newfile%') DO (
      set newfilename=%%~nF
      set /a newname=!newfilename:~1,3!

      REM ----this will compare new file and old file----
      REM ----first if will prefix 1 if it can't find a match number in old file----
      IF NOT "!newname!" == "!oldname!" (
      set /a start=1
      rem set /a zero=0
      rename "%%F" "_!start!%%~nxF"
      call :addfoldername
      )
      REM ----second if will add 1 if it can find a match number in old file----
      IF "!newname!" == "!oldname!" (
      rem ECHO EQUAL
      set /a addnewfilenumber=!FI!+1
      rem set /a zero=0
      rename "%%F" "_!addnewfilenumber!%%~nxF"
      call :addfoldername
      )
      )

      :addfoldername
      pushd "C:\Users\Administrator.MMC-EXPLO\Music\New folder"
      for /f "delims=" %%a in ('dir /s /b /a-d *.* ^|findstr /iv "aco"') do for /d %%P in (*) do for /f "delims=" %%F in ('dir /s /b /a-d "%%P"') do rename "%%a" "%%P%%~nxa"