Add dry run mode and auto root path via %~dp0

This commit is contained in:
blance
2026-04-23 21:18:28 -05:00
parent 7caf23a715
commit ea5aa9fc24
2 changed files with 52 additions and 23 deletions
+26 -6
View File
@@ -42,12 +42,32 @@ ROOT\
### Setup ### Setup
1. Open `Rename and Move PT STMT.bat` in Notepad 1. Place `Rename and Move PT STMT.bat` in the root patient folder
2. Set the `ROOT` variable to your actual folder path: 2. Double-click to run — it automatically uses the folder it lives in as the root
```bat
set "ROOT=C:\Users\Melissa\Documents\Patients" ### Dry Run
```
3. Save and double-click to run The script defaults to **dry run mode** — it will print every action it would take without touching any files. To run for real, open the file in Notepad and change:
```bat
set "DRYRUN=1"
```
to:
```bat
set "DRYRUN=0"
```
Dry run output looks like:
```
[DRY RUN] No files will be changed.
Found: C:\Patients\abc\Patient\Export\.
RENAME export.txt > PT STMT_20260423.txt
MKDIR PT STMT_20260423
MOVE PT STMT_20260423.txt > PT STMT_20260423\
MOVE 1000001.tif > PT STMT_20260423\
MOVE 1000002.tif > PT STMT_20260423\
```
### Notes ### Notes
+26 -17
View File
@@ -1,8 +1,11 @@
@echo off @echo off
setlocal enabledelayedexpansion setlocal enabledelayedexpansion
REM === SET YOUR ROOT FOLDER HERE === REM === SET TO 1 TO PREVIEW WITHOUT MAKING CHANGES, 0 TO RUN FOR REAL ===
set "ROOT=C:\path\to\patient" set "DRYRUN=1"
REM === ROOT FOLDER — leave as %~dp0 to use the folder this bat file lives in ===
set "ROOT=%~dp0"
REM === GET TODAY'S DATE IN YYYYMMDD FORMAT === REM === GET TODAY'S DATE IN YYYYMMDD FORMAT ===
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd"') do set TODAY=%%i for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd"') do set TODAY=%%i
@@ -10,6 +13,11 @@ for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd"') do
set "NEWNAME=PT STMT_%TODAY%.txt" set "NEWNAME=PT STMT_%TODAY%.txt"
set "FOLDERNAME=PT STMT_%TODAY%" set "FOLDERNAME=PT STMT_%TODAY%"
if "%DRYRUN%"=="1" (
echo [DRY RUN] No files will be changed.
) else (
echo [LIVE] Changes will be made.
)
echo Processing folders under %ROOT%... echo Processing folders under %ROOT%...
echo. echo.
@@ -17,29 +25,30 @@ REM === LOOP THROUGH ALL SUBFOLDERS ===
for /r "%ROOT%" %%d in (.) do ( for /r "%ROOT%" %%d in (.) do (
if exist "%%d\export.txt" ( if exist "%%d\export.txt" (
echo Processing: %%d echo Found: %%d
REM pushd into the folder so all commands use short relative paths (fixes path-too-long errors)
pushd "%%d" pushd "%%d"
REM Rename export.txt if "%DRYRUN%"=="1" (
ren "export.txt" "%NEWNAME%" echo RENAME export.txt ^> %NEWNAME%
echo MKDIR %FOLDERNAME%
REM Create dated folder echo MOVE %NEWNAME% ^> %FOLDERNAME%\
mkdir "%FOLDERNAME%" 2>nul for %%f in ("*.tif") do (
echo MOVE %%~nxf ^> %FOLDERNAME%\
REM Move renamed txt file )
move "%NEWNAME%" "%FOLDERNAME%\" ) else (
ren "export.txt" "%NEWNAME%"
REM Move all .tif files mkdir "%FOLDERNAME%" 2>nul
for %%f in ("*.tif") do ( move "%NEWNAME%" "%FOLDERNAME%\"
move "%%f" "%FOLDERNAME%\" for %%f in ("*.tif") do (
move "%%f" "%FOLDERNAME%\"
)
) )
echo.
popd popd
) )
) )
echo.
echo Done. echo Done.
pause pause