Neeleshwar Pandey
Neeleshwar Pandey

Reputation: 28

Want to automate the process of moving multiple files

I am new to computers and the world of programming.

@echo off
for /l %%i in (1,1,8) do (
for /f "delims=" %%f in ('findstr  /mc:" segment %%i " 
C:\Users\King_Arthur\Desktop\file\Myfile_split_files\Myfile_.txt') do (
 move "%%f" "segment %%i\"
 )
)

So this program moves files based on the content of the text file, in this case it checks whether the txt file has segment (1-8) string or not. But it checks only one file at a time and I have 10,000 files named as-

Myfile_1.txt,Myfile_2.txt and so on until Myfile_10000.txt.

Okay so each txt file contains

 >KM368312.1 Influenza A virus 
 (A/swine/Shandong/01/2009(H1N1)) segment 3 
 polymerase PA (PA) and PA-X protein (PA-X) genes, 
 complete cds 

you can assume that the first line of every file is like this except the segment part ranges from segment 1 - segment 8.

So how can I modify this to read and move all files to their destination, in this case folders named as segment 1 - segment 8 ?

I know linux is a better alternative to perform operations like these, and I am trying to make the switch as soon as possible.

Upvotes: 0

Views: 54

Answers (1)

Magoo
Magoo

Reputation: 79982

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"

for /L %%e in (8,-1,1) do md "%sourcedir%\segment %%e\"
FOR /f "tokens=1,2,*delims=:" %%a IN (
 'findstr /L /c:" segment " "%sourcedir%\file*" '
 ) DO (
 call :whatseg %%c
 for /L %%e in (8,-1,1) do if errorlevel %%e echo move "%%a:%%b" "segment %%e\"&move "%%a:%%b" "segment %%e\"
)

GOTO :EOF

:whatseg
if "%~1"=="segment" goto foundseg
shift
goto whatseg

:foundseg 
exit /b%2

You would need to change the setting of sourcedir to suit your circumstances. The listing uses a setting that suits my system.

I deliberately include spaces in names to ensure that they are processed correctly.

The findstr command looks for the literal segment within each file and produces a listing of the format fullfilename:linefound. Since the full-filename contains a :, the for/f assigns the drive-letter to %%a, the path and filename to %%b and the line text to %%c.

The routine :whatseg is then called, which simply shifts each token until segment is found, and then sets errorlevel to the following argument and returns to the caller.

the for/L then checks errorlevel returned by the :whatseg routine - in reverse-order because if errorlevel is interpreted as if errorlevel is n or greater. When %%e matches errorlevel, the file is moved, which will return errorlevel 0 and hence only 1 match is possible.

Upvotes: 1

Related Questions