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