0

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.

1 Answers1

0
@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 ECHOed 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 ECHOed 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

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you. I set the same path for `sourcedir` and `destdir` where my folders and "file-*.dds" files are located, and removed `ECHO` before `MD` and `MOVE`. Seems I missed something because starting this .bat do nothing. – misha.physics Jul 26 '23 at 23:04
  • There is: The system cannot find the file specified. – misha.physics Jul 26 '23 at 23:11
  • It worked fine for me. With the `echo` commands in place, does the report-to-screen make sense? Oh - BTW, best to copy & paste using a text editor (not a word-processor) and ensure that the file is saved as ASCII, not unicode. Also check that the quote type has not been converted to "smart quotes" - straight quotes (" and ') are required. – Magoo Jul 27 '23 at 00:47
  • With `echo` doesn't work too. Quote type are right. I run the script, it ask these three values, and after entering the last I've got `The system cannot find the file specified`. – misha.physics Jul 27 '23 at 01:04
  • That error message would indicate that the filepath in the `dir` command is incorrect. It could be a typo, or perhaps the value for `sourcedir` contains a non-ANSI character. If you substitute the short name for the value assigned to `sourcedir`, the problem should be solved. To determine the shortname, use `dir /x` in each directory along the path, and use the value in the column before the actual name. This is a guaranteed-ANSI equivalent, BUT generation of the shortname is DISABLED by default in newer Windows versions. You could also try using a `symbolic link` to the directory. – Magoo Jul 27 '23 at 06:35
  • I'm sory that's my mistake. I used `SET "sourcedir=C:\Users\xolms\Desktop\mydir\t w o"` because thoght `t w o` as some needed parameters. I just changed it to `SET "sourcedir=C:\Users\xolms\Desktop\mydir"` and it works great. So, "t w o" was your folder name? – misha.physics Jul 27 '23 at 07:17
  • Yes - as I said in the program comments, the directories and filenames are names that I use for testing. Glad you got it going. Good luck and stay safe! – Magoo Jul 27 '23 at 07:21
  • Sorry, there's a little problem with sorting. If I have 16 files: (file-0.dds, file-1.dds ,..., file-15.dds), and 4 folders: (001180, 001181, 001182, 001183), then files (file-0.dds, file-1.dds, file-10.dds, file-11.dds) are moved into the first folder. Could it be changed to move (file-0.dds, file-1.dds, file-2.dds, file-3.dds) into the first folder? – misha.physics Jul 27 '23 at 08:47
  • I haven't actually tested the modification I've posted, but it *should* work – Magoo Jul 27 '23 at 09:12
  • I needed to add the extension, so the line `for /L %%e in (1,1,9) do ren "%sourcedir%\file-%%e.dds" "file-0%%e.dds"` works. But now it seems to be sorting problems for a large number of files, for example, if I have files from file-0.dds to file-1023.dds. – misha.physics Jul 27 '23 at 12:40
  • OK - fixed the extension and range of `file-*` is now `0..9999` – Magoo Jul 27 '23 at 13:24
  • Thank you so much! It seems to be working as I wished. – misha.physics Jul 27 '23 at 14:08