之前寫的一個整理桌面的批處理

文章第一次發(fā)在批處理之家,文中從互聯(lián)網(wǎng)借鑒了很多好的方法,在這里就不一一列舉了 +_+

使用方法:
復制下面的代碼,粘貼到文本文檔,然后重命名文本文檔后綴為bat就好了(xxx.txt--->xxxx.bat)
建議將文件放在非系統(tǒng)盤的文件夾下面,目錄無所謂,雙擊之后腳本會自動產(chǎn)生桌面的快捷方式

代碼部分

@echo off
set title_T=桌面便捷整理工具  --By:landwind@簡書 文件修改時間:%~t0
title %title_T%
rem 格式化時間
for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime /value') do (
  set tt=%%a
)
set Today=%tt:~0,4%-%tt:~4,2%-%tt:~6,2%
set Time_T=%tt:~8,2%:%tt:~10,2%:%tt:~12,2%
set y=%Today:~0,4%
set m=%Today:~5,2%
set d=%Today:~8,2%
set t=%Time_T:~0,8%
set t=%t::=.%
set config_sort_dir=%AllUsersProfile%\Auto_Sort_Desktop_files\
set config_sort_log=%AllUsersProfile%\Auto_Sort_Desktop_files\log
set config_sort_file=%AllUsersProfile%\Auto_Sort_Desktop_files\config.ini
rem 獲取下桌面路徑
for /f "tokens=2,*" %%i in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do (
set desktop_dir=%%j
)

rem 判斷配置文件夾以及配置文件是否存在,不存在創(chuàng)建
if not exist "%config_sort_dir%" mkdir "%config_sort_dir%"
if not exist "%config_sort_log%" mkdir "%config_sort_log%
if not exist "%config_sort_log%\delete" mkdir "%config_sort_log%\delete"
if not exist "%config_sort_file%" (goto first_Use) else (goto choice_config)
:choice_config
set ERRORLEVEL=
choice /C YN /M "要重新配置請在5s內(nèi)輸入Y" /T 5 /D N
if "%ERRORLEVEL%"=="1" goto re_config
goto start_main

:first_Use
cls
echo.
echo =====================歡迎試用桌面自動整理工具=========================
echo        1.檢測到配置文件不存在,請對本工具進行一個簡單的配置
echo        2.可以直接拖放文件夾到本窗口,CMD會自動獲取文件夾路徑
echo        3.只要選定一個文件夾,每次打開工具,工具會將桌面文件移動到
echo          設定的文件夾下面,按照 選定文件夾\年\年-月-日\具體文件 
echo          這種形式文件夾下面.
echo        4.配置文件在%config_sort_dir%目錄底下,日志也在此目錄
echo        5.關于日志,最多保存最近30天的文件,超過30天的,會移動到
echo          配置文件夾log\delete目錄下面,下次運行本工具的時候會
echo          刪除里面的所有文件,so不要放東西進去奧...
echo                             歡迎試用.
echo ======================================================================
echo.
echo ==========================配置模式選擇================================
echo        1.使用默認配置,自動歸檔桌面文件和文件夾到D:\原桌面
echo        2.手工設定歸檔文件架
echo ======================================================================
echo.
set /p config_style=請輸入要使用的配置模式前面的數(shù)字,回車確認:
set config_style=%config_style: =%
if "%config_style%"=="1" goto default_config
if "%config_style%"=="2" goto hands_config
goto config_error

:default_config
echo auto_sort_dir=d:\原桌面 >>%config_sort_file%
goto start_main
:hands_config
echo .
echo 小提示:直接拖放文件夾到本窗口可以直接獲取文件夾路徑
set /p hands_config_dir=請輸入歸檔文件夾:
rem 判斷先是不是在系統(tǒng)盤,如果是,那就提示,這里就不判斷是不是在桌面了,不好處理...關鍵字不能用,取前xx位判斷貌似不太準,鬼知道用戶的用戶名多長......
set input_disk=%hands_config_dir: =%
set input_disk=%input_disk:~0,1%
set system_disk=%systemroot:~0,1%
rem 悲劇啊,找了別人寫的轉換大小寫的批處理,活活看不懂T_T....用笨方法吧...
set "code=aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ"
setlocal EnableDelayedExpansion
for %%i in (%code%) do (
set code_T=%%i
if "!input_disk!"=="!code_T:~0,1!" set input_disk=!code_T:~1,1!
)
for %%i in (%code%) do (
set code_T=%%i
if "!system_disk!"=="!code_T:~0,1!" set system_disk=!code_T:~1,1!
)
setlocal DisableDelayedExpansion

if "%input_disk%"=="%system_disk%" goto dir_error
echo auto_sort_dir=%hands_config_dir% >>%config_sort_file%
goto start_main

:start_main
goto read_config
:start_move_file
if not exist "%auto_sort_dir%" mkdir "%auto_sort_dir%"
if not exist "%auto_sort_dir%\%y%\" mkdir "%auto_sort_dir%\%y%\"
if not exist "%auto_sort_dir%\%y%\%y%-%m%-%d%\" mkdir "%auto_sort_dir%\%y%\%y%-%m%-%d%\"
echo robocopy %desktop_dir%\ %auto_sort_dir%\%y%\%y%-%m%-%d%\ /E /MOVE  /XF *.lnk
robocopy %desktop_dir%\ %auto_sort_dir%\%y%\%y%-%m%-%d%\ /V /LOG+:%config_sort_log%\%y%-%m%-%d%.log /E /MOVE  /XF *.lnk
if exist "%desktop_dir%\歸檔文件夾.lnk" del /q /f "%desktop_dir%\歸檔文件夾.lnk"
if exist "%desktop_dir%\開始歸檔桌面文件.lnk" del /q /f "%desktop_dir%\開始歸檔桌面文件.lnk"

set SrcFile=%auto_sort_dir%\%y%\
set Icon=%systemroot%\System32\shell32.dll,301
set LnkFile=%desktop_dir%\歸檔文件夾
call :CreateShort "%SrcFile%" "%LnkFile%" "%Icon%"
set SrcFile=%~dpnx0
set Icon=%systemroot%\system32\shell32.dll,80
set LnkFile=%desktop_dir%\開始歸檔桌面文件
call :CreateShort "%SrcFile%" "%LnkFile%" "%Icon%"

goto handle_logfile



:handle_logfile
rem 啥?這不是只刪除.log文件啊,上面你說刪除所有的啊......呵呵噠,嚇唬你不行啊
del /s /q /f %config_sort_log%\delete\*.log >>%config_sort_log%\%y%-%m%-%d%.log
rem 將30天前的文件移動到delete文件夾
robocopy %config_sort_log% %config_sort_log%\delete /E /MOVE /MINAGE:30 /LEV:1
goto good_job

:config_error
cls
echo .
echo ========================配置模式選擇錯誤==============================
echo        很抱歉,配置模式選擇錯誤,請重新輸入
echo        你的輸入為: %config_style%
echo ======================================================================
ping 127.0.0.1 -n 3 >nul
goto first_Use

:dir_error
cls
echo .
echo ========================歸檔路徑配置錯誤==============================
echo        很抱歉,歸檔路徑選擇錯誤,不允許使用系統(tǒng)盤,請重新配置
echo        你的配置的文件夾為: %hands_config_dir%
echo ======================================================================
ping 127.0.0.1 -n 3 >nul
goto first_Use

:read_config
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%a in ('find "auto_sort_dir" "%config_sort_file%"') do (
set auto_sort_dir=%%a
)
setlocal DisableDelayedExpansion
goto del_space
:del_space
set space=%auto_sort_dir:~-1%
if "%space%" NEQ " " (goto start_move_file) else (
set auto_sort_dir=%auto_sort_dir:~0,-1%
goto del_space
)
:CreateShort
mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%~2.lnk""):b.TargetPath=""%~1"":b.IconLocation=""%~3"":b.WorkingDirectory=""%~dp1"":b.Save:close")
goto :eof
:re_config
cls
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%a in ('find "auto_sort_dir" "%config_sort_file%"') do (
set auto_sort_dir=%%a
)
setlocal DisableDelayedExpansion
echo .
echo ========================當前配置==============================
echo           當前配置目錄:%auto_sort_dir%
echo           1.重新配置
echo           2.呃呃呃,按錯了,放我回去
echo ==============================================================
echo .
echo.
set /p re_config_num=請輸入要使用的配置模式前面的數(shù)字,回車確認:
set re_config_num=%re_config_num: =%
if "%re_config_num%"=="1" goto first_Use
if "%re_config_num%"=="2" goto start_main

:good_job
cls
echo .
echo ========================歸檔完畢==============================
echo                        已歸檔完畢
echo              程序將在10s后退出,歡迎再次使用
echo ==============================================================
echo .
ping 127.0.0.1 -n 10 >nul
goto exit_me

:exit_me
exit
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容