配套fMRI预处理使用。之前预处理忘记删前十秒的文件(为什么要删掉?因为前十秒的文件不稳定),于是59个文件夹不仅要保留删除前十秒还没预处理的文件,还要在每个文件夹里删掉大概一共700个左右的各个预处理步骤遗留下的文件。后面问了chatGPT,写了window脚本挺好用的,省了不少事,这里记录一下,分享一下。
文件地址:https://github.com/zhoushusheng/fMRI_preprocessing
操作步骤:
- 将脚本复制到记事本中,并保存为
.bat
文件(如delete_first_ten_files_by_pattern.bat
)。 - 双击
.bat
文件运行,脚本将将执行相应的功能。
delete_non_fMRI_nii.bat
@echo off setlocal enabledelayedexpansion for /l %%i in (1,1,35) do ( if %%i lss 10 ( set "folder=sbj0%%i" ) else ( set "folder=sbj%%i" ) set "target_dir=B:\fMRI\preprocessing_exclude_10s\CN\!folder!\fMRI" if exist "!target_dir!" ( cd /d "!target_dir!" if exist *.nii ( echo Processing folder: !target_dir! for %%f in (*.nii) do ( set "filename=%%~nf" echo Checking file: %%f REM Check if filename starts with "s" followed by exactly 3 digits and an underscore if "!filename:~0,1!"=="s" ( set "next_three=!filename:~1,3!" set "underscore=!filename:~4,1!" if "!underscore!"=="_" ( echo Keeping file: %%f ) else ( echo Deleting file: %%f del "%%f" ) ) else ( echo Deleting file: %%f del "%%f" ) ) ) else ( echo No .nii files found in !target_dir! ) ) else ( echo Directory "!target_dir!" does not exist! ) ) endlocal
delete_non_MRI_nii.bat 除了fMRI的,MRI里的也要,要不然fMRI预处理读取结构体的时候会报错,因为之前coregister的时候生成了多个..nii在MRI里头。
@echo off setlocal enabledelayedexpansion for /l %%i in (1,1,35) do ( if %%i lss 10 ( set "folder=sbj0%%i" ) else ( set "folder=sbj%%i" ) set "target_dir=B:\fMRI\preprocessing_exclude_10s\CN\\!folder!\MRI" if exist "!target_dir!" ( cd /d "!target_dir!" if exist *.nii ( echo Deleting files in !target_dir! for %%f in (*.nii *.mat) do ( set "filename=%%~nf" if /i "!filename:~0,1!" neq "s" ( echo Deleting file: %%f del "%%f" ) ) ) else ( echo No .nii files found in !target_dir! ) ) else ( echo Directory "!target_dir!" does not exist! ) ) endlocal
删除前10s的 delete_first_ten_nii_files_per_folder.bat
@echo off setlocal enabledelayedexpansion REM 遍历从 sbj01 到 sbj24 的文件夹 for /l %%i in (1,1,24) do ( if %%i lss 10 ( set "folder=sbj0%%i" ) else ( set "folder=sbj%%i" ) REM 设置目标目录 set "target_dir=B:\fMRI\preprocessing_exclude_10s\AD\!folder!\fMRI" REM 检查目录是否存在 if exist "!target_dir!" ( echo Processing folder: !target_dir! cd /d "!target_dir!" REM 初始化计数器 set /a count=0 REM 查找符合模式的文件,按序号排序后删除前十个 for /f "tokens=*" %%f in ('dir /b /on s???_S_*-01.nii') do ( if !count! lss 10 ( echo Deleting file: %%f del "%%f" set /a count+=1 ) ) echo Deleted !count! files in folder: !target_dir! ) else ( echo Directory "!target_dir!" does not exist! ) ) endlocal
标签:fMRI,nii,target,%%,echo,window,set,dir From: https://www.cnblogs.com/zhoushusheng/p/18381608