To simplify routine work I made a batch script (.bat) that gives, for example, 4 subfolders and 16 *.dds files in the current folder.
These subfolders and files have ordered names like: Folder names: 002360, 002361, 002362, 002363. File names: file-0.dds, file-1.dds, file-2.dds, ... , file-15.dds.
I would like to extend this script to obtain the following:
a) It takes the first 4 files (from "file-0.dds" to "file-3.dds") moves them to the first folder ("002360") and renaming them to be from "003760.dds" to "003763.dds".
b) It takes the next 4 files (from "file-4.dds" to "file-7.dds") moves them to the next folder ("002361") and renaming them to the same names, namely from "003760.dds" to "003763.dds".
...
d) It takes the last 4 files (from "file-12.dds" to "file-15.dds") moves them to the last folder ("002363") and renaming them to the same names, namely from "003760.dds" to "003763.dds".
The number of moving files for each folder is same, I assign this number as variable:
%y%
The first file name (namely "003760.dds") can be entered at the beginning together with other variables.
Also I'm concerned with leading zeros in the file names. For the folders creating I use a loop like:
set /p n=Enter the number of the first zeroes in folder names:
set /p x_=Enter the name (without first zeroes) of the first folder:
set /a x=x_
set /p y_=Enter the number of folders:
set /a y=y_
set /a z=x+y-1
if %n%==2 (
for /l %%i in (%x%, 1, %z%) do md "00%%i"
) else if %n%==3 (for /l %%i in (%x%, 1, %z%) do md "000%%i"
)
I don't know how to make a loop to select the first few files and move them to the first folder. The renaming operation can be postponed, because it can be done after all files moving. Maybe there are some examples with the commands I need.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
SET /a directorydigits=6
SET /a filedigits=6
SET /p filecount="How many files AT a time? "
SET /p dirnum="Starting directory number (omit leading zeroes)? "
SET /p rennum="Starting destination file number (omit leading zeroes)? "
SET /a dirnum+=1000000000
SET /a rennum=1000000000 + rennum
SET /a rennummax=rennum + filecount
:: This should rename "file-0..9999" to "file-00000..099999"
for /L %%e in (0,1,9999) do IF EXIST "%sourcedir%\file-%%e.dds" (
SET /a filenumber=1000000000 + %%e
ren "%sourcedir%\file-%%e.dds" "file-!filenumber:~-5!.dds"
) ELSE GOTO continue
:continue
FOR /f "delims=" %%e IN ('dir /b /a-d "%sourcedir%\file-*.dds"') DO (
SET "destsubdir=%destdir%\!dirnum:~-%directorydigits%!"
ECHO MD "!destsubdir!" 2>NUL
ECHO MOVE "%sourcedir%\%%e" "!destsubdir!\!rennum:~-%filedigits%!.dds"
SET /a rennum+=1
IF !rennum! geq !rennummax! SET /a rennum-=filecount&SET /a dirnum+=1
)
GOTO :EOF
Always verify against a test directory before applying to real data.
Makes things a lot easier if you use meaningful variablenames.
The required MD commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO MD
to MD
to actually create the directories. I've appended 2>nul
to suppress error messages (eg. when the directory already exists)
The required MOVE commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE
to MOVE
to actually move the files. Append >nul
to suppress report messages (eg. 1 file moved
)
The dir
command generates a list of the filenames only (/b = basic, /a-d = no directorynames) which the for
processes to %%e
.
The maths manipulation should be obvious. To follow what's going on with delayedexpansion
, try Stephan's DELAYEDEXPANSION link: https://stackoverflow.com/a/30284028/2128947