简体   繁体   中英

How to create folder name without extension of file type using batch script and move in created sub directory?

Help me sir i try more than 100 times but it not exactly works for me.

Currently getting results in wrong way:-
Given below batch script create folder from this file (7101-1- kutana.pdf ) name with.pdf extension but not exactly what i need.

Results i need in right way a/q to given below steps

For example
Step 1:-I want to remove 1st five character from file name (7101- 1- kutana )
and also i want to remove extension from folder name.
Sourcedir=e:\automovesystem\temp
A. source pdf file name:- 7101-1- kutana.pdf

Step 2:-
Create directory under destdir name of 1-(space)kutana and award
B. destdir=e:\automovesystem\prpl
Created directory e:\automovesystem\prpl\1- kutana\award

Step3.check if already folder name available according to step 1 file name.

Step 4: - move same file in same folder which is created in step 2 under sub directory.

@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

Additional to your own answer, I probably would have done it a litte more like this:

@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

I made it own sir i am very happy😅😍 I had less knowledge of batch scripting that's why not sleep from yesterday continue working on to make AutoMoveFilesystem but also i am great thankful to provide stack overflow platform to create this code. i can't able to do without this plateform.

@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

All help with this goes to @XionicFire he reworked the whole of this code I'm just posting it for others to use/benefit:

This code comes from a mix of several different people @PrahladDixit & @Compo, the code was not working we couldn't tell why after a LOT of search seems it suffered the usual... "a pro wrote this so no one understands what he did" syndrome, where us worms cannot figure it out, we remade it and organized it so normal human "worms" can understand what its doing and made it a little friendlier so its easier to mod and change as needed, we hope this helps others, works great in windows 11

Code

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM