一些用批處理命令批量處理資料夾或檔案的功能實現
阿新 • • 發佈:2019-02-05
筆者之前做實驗,需要批量處理大量的資料夾或者檔案,寫了一些批處理的指令碼,在這裡記錄分享一下:
1.將資料夾名中某個字元替換為另一字元
@echo off
::設定待替換的字元
set r1=0
::設定替換後的字元
set r2=1
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir/ad/b') do (
set y=%%a
ren "%%~a" "!y:%r1%=%r2%!")
echo.&echo 替換成功
pause
2.批量新建資料夾
for /l %%i in (11,1,100) do md H:\folder pause
3.刪除當前資料夾下所有子目錄中指定名字的資料夾
@echo off
for /f "delims=" %%i in ('dir /ad /b /s "8"') do (
rd /s /q "%%i"
)
4.刪除檔名中包含指定字元的檔案
@echo off
set "fn=so_*.txt"
for /r %%i in ("%fn%?")do echo %%i&del "%%i"
pause
5.刪除檔名中指定的字元6.刪除指定行號的行@echo off&setlocal enabledelayedexpansion for /f "delims=" %%a in ('dir /b/a-d e*.txt') do ( set "str=%%a" set "str=!str:e=!" ren "%%a" "!str!" )
@echo off
SetLocal EnableDelayedExpansion
set/p v=請輸入要刪除的行號並回車,若指定多行請用空格隔開(如:2 5 8):
for %%i in (%v%) do (
set x=!x! %%id;
echo !x!
)
sed -i "%x%" 123.txt
pause
7.刪除子資料夾內檔名中指定字元8.在檔名指定位置新增指定字元@echo off&setlocal enabledelayedexpansion pushd H:\ExperimentResults\leve2\source_output for /f "delims=" %%a in ('dir /a-d /b /s so_*.txt') do ( set "str=%%~nxa" set "str=!str:so_=!" ren "%%a" "!str!" ) popd pause
@echo off
for /f %%i in ('dir /a-d /b *.txt') do ren %%i e_0.%%~ni.txt
@pause