[英]batch file to create new subfolder in folder, move file to newly created subfolder, for all folders in directory
[英]How to create folder name without extension of file type using batch script and move in created sub directory?
幫幫我先生,我嘗試了 100 多次,但它並不完全適合我。
目前以錯誤的方式獲得結果:-
下面給出了批處理腳本從這個文件創建文件夾(7101-1 -kutana.pdf )名稱與.pdf擴展名但不完全是我需要的。
結果我需要以正確的方式a / q給出以下步驟
例如
第 1 步:-我想從文件名中刪除第一個五個字符(7101- 1- kutana )
而且我想從文件夾名稱中刪除擴展名。
源目錄=e:\automovesystem\temp
A. 來源 pdf 文件名:- 7101-1- kutana.pdf
第2步:-
在1-(space)kutana的destdir名稱下創建目錄並授予
B. destdir=e:\automovesystem\prpl
創建目錄e:\automovesystem\prpl\1- kutana\award
Step3.根據步驟1文件名檢查文件夾名稱是否可用。
第 4 步: - 將相同的文件移動到在第 2 步中創建的子目錄下的同一文件夾中。
@echo off setlocal enableextensions disabledelayedexpansion
Set "sourcedir=e:\automovesystem\temp"
Set "destdir=e:\automovesystem\prpl"
For /f "eol=| tokens=1* delims=-" %%a in ('dir /b /a-d-h "%sourcedir%\*-*" 2^>nul') do (
Md "%destdir%\%%b\award" 2>nul
Move /y "%sourcedir%\%%a-%%b" "%destdir%\%%b\award" )
Endlocal
除了您自己的答案之外,我可能會更像這樣:
@Echo Off
SetLocal EnableExtensions
Set "SRC=E:\automovesystem\TEMP"
Set "DST=E:\automovesystem\PRPL"
If Not Exist "%SRC%\" (Exit /B) Else If Not Exist "%DST%\" Exit /B
For /F "Delims=" %%G In ('%SystemRoot%\System32\where.exe "%SRC%":"????-?*-?*.*"
2^>NUL') Do For /F "Tokens=1,* Delims=-" %%H In ("%%~nG"
) Do %SystemRoot%\System32\Robocopy.exe "%%~dpG." "%DST%\%%I\NOTICE" "%%~nxG"^
/Mov 1>NUL 2>&1
我自己做了它,我很高興😅😍我對批處理腳本的了解較少,這就是為什么不從昨天開始睡覺繼續努力制作 AutoMoveFilesystem 但我也非常感謝提供堆棧溢出平台來創建此代碼。 我不能沒有這個平台。
@Echo Off
Set "SRC=E:\automovesystem\TEMP"
Set "DST=E:\automovesystem\PRPL"
SetLocal EnableDelayedExpansion
If Not Exist "%SRC%\" (Exit/B) Else If Not Exist "%DST%\" Exit/B
For /F "Delims=" %%A In ('Where "%SRC%":?????????*.*') Do (Set "FNX=%%~nA"
If Not Exist "%DST%\!FNX:~5,50!\NOTICE" (MD "%DST%\!FNX:~5,50!\NOTICE" 2>Nul
If ErrorLevel 1 Echo Unable to create directory %DST%\!FNX:~5,50!\NOTICE)
If Exist "%DST%\!FNX:~5,50!\NOTICE" (Move /Y "%%A" "%DST%\!FNX:~5,50!\NOTICE">Nul 2>&1
If ErrorLevel 1 Echo Unable to move %%A to %DST%\!FNX:~5,50!\NOTICE))
Timeout -1
Exit/B
這方面的所有幫助都歸功於@XionicFire,他重新編寫了整個代碼,我只是將它發布給其他人使用/受益:
這段代碼來自幾個不同的人@PrahladDixit 和@Compo,代碼不起作用我們無法解釋為什么經過大量搜索后它似乎遭受了通常的影響......“一位專業人士寫了這個,所以沒有人明白什么他做到了”綜合症,我們的蠕蟲無法弄清楚,我們重新制作並組織了它,以便正常的人類“蠕蟲”可以理解它在做什么並使它更友好一點,因此更容易根據需要進行修改和更改,我們希望這會有所幫助其他,在 windows 11 中效果很好
@ECHO OFF
ECHO This file will create Individual folders inside the current folder using the following Parameters and sort all *.JPG files in current directory accordingly
REM To change target or source directory simply type it in the variable field
SET "SRC=%~dp0"
SET "SRC=%SRC:~0,-1%"
SET "DST=%~dp0"
SET "DST=%DST:~0,-1%"
ECHO.
REM For Diagnostics & Troubleshooting Purposes
ECHO Source: %SRC%
ECHO Destination: %DST%
ECHO Characters: 19
ECHO Where: %SystemRoot%\System32\where.exe "%SRC%":*.jpg
ECHO.
ECHO To Cancel this operation press CTRL-C
PAUSE
SetLocal EnableDelayedExpansion
If Not Exist "%SRC%\" (Exit/B) Else If Not Exist "%DST%\" Exit/B
For /F "Delims=" %%A In ('%SystemRoot%\System32\where.exe "%SRC%":*.jpg') Do (
SET "FNX=%%~nA"
REM Replace 19 for the number of characters from the start of the filename you wish to use as Base for folder creation
SET "FNX=!FNX:~,19!
REM For Diagnostics & Troubleshooting Purposes
REM ECHO !DST!\!FNX!\
If Not Exist "!DST!\!FNX!\" (MD "!DST!\!FNX!" 2>NUL
If ErrorLevel 1 ECHO Unable to create directory !DST!\!FNX!)
If Exist "!DST!\!FNX!\" (MOVE /Y "%%A" "!DST!\!FNX!">NUL 2>&1
If ErrorLevel 1 ECHO Unable to move %%A to !DST!\!FNX!)
)
ECHO ************************ DONE ************************
PAUSE
REM Use in the case of running multiple scripts in a row
REM EXIT/B
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.