简体   繁体   中英

Batch file to arrange files per name into relevant folders

I have the following folders:

Apple_folder
Pear_folder
Tomatoes_folder

Within Apple_folder , I have the following files:

Extracted-Apple_1.txt
Extracted-Apple_2.txt
Extracted-Pear_1.txt
Extracted-Pear_2.txt
Extracted-Apple_3.txt
Extracted-Tomatoes_1.txt

How do I move Extracted-Pear_1.txt , Extracted-Pear_2.txt , Extracted-Tomatoes_1.txt into their relevant folders, ie keep file name that contain apple under Apple_folder , etc.

Expected Apple_folder contents:

Extracted-Apple_1.txt
Extracted-Apple_2.txt
Extracted-Apple_3.txt

Expected Pear_folder contents:

Extracted-Pear_1.txt
Extracted-Pear_2.txt

Expected Tomatoes_folder contents:

Extracted-Tomatoes_1.txt

I am still a beginner in batch script, so not sure how to proceed?

The following script didn't work.

setlocal enabledelayedexpansion
set /A counter=0

@echo off
for %%a in (*.txt) do (
    for /f "tokens=2 delims=-_" %%f in ("%%a") do (
        set /A counter+=1 
        MOVE "%%a" "C:\Users\ADMIN\Documents\TESTING\Batch_script\%%f_folder\Extracted-%%~nf_moved__!counter!.txt"
    )
)
pause

I got to the bottom of this. This is the solution I was looking for. It will recognise the file names and check whether they're under the right folder, if not it will move them under the right folder:

setlocal enabledelayedexpansion

for /R "C:\Users\Admin\Documents\TESTING\Batch_script\" %%G IN (*.txt) do (
    for /F "tokens=7,12,13 delims=\-_." %%a in ("!%%G!") do (
            set /A counter+=1 
IF NOT %%a==%%b (MOVE "%%G" "C:\Users\ADMIN\Documents\TESTING\Batch_script\%%b_folder\MSG-Extracted-%%b_%%c_moved_!counter!.txt")
    )
)

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