We'd like to remind Forumites to please avoid political debate on the Forum... Read More »
We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Automate a repetitive process in Windows
Options

KingL
Posts: 1,713 Forumite
in Techie Stuff
Hello
Would someone be so kind as to write the code (?batch file) to automate this process?
I have a folder with several hundred files in. For each file, I want to
1) create a new folder
2) rename the folder with the same name as the file (without the extension)
3) move the file into the folder
then step on to the next file; and repeat until there are no more remaining.
I don't know scripting. I've searched around a bit, but to no avail...
tia
(windows 10)
Would someone be so kind as to write the code (?batch file) to automate this process?
I have a folder with several hundred files in. For each file, I want to
1) create a new folder
2) rename the folder with the same name as the file (without the extension)
3) move the file into the folder
then step on to the next file; and repeat until there are no more remaining.
I don't know scripting. I've searched around a bit, but to no avail...
tia
(windows 10)
0
Comments
-
Very straightforward, provided you give a bit more information!
1) Please supply full path of the folder in which the hundreds of files reside
2) Please supply the full path of the folder in which all the new folders are to be created. This should NOT be the same as the folder in 1) to avoid looping! (You can always move the file structure anywhere you like after the BATch file has run...)
3) Is there a blank in ANY of the file names? (I presume yes!)
4) Are there any 'poison characters' in any of the file names, like ", ', %, ^, & ? These can be quite difficult to deal with, and may need to be changed/removed before the BATch file runs.
In fact, a DIR list to a TXT file would be best, so I could look through it...0 -
thanks, just keep the path statements generic, I can copy/paste the actual paths in later
right now it is all on a USB drive, so say the source folder is
f:\parent folder includes spaces\Source folder includes spaces
I was going to put the destination folders inside the source folder, but I don't have to. Say the destination folder is
f:\parent folder includes spaces\destination folder
Yes there are blanks in the file and foldernames
There are no special characters.
thanks0 -
Have a look at
@echo off setlocal set source=D:\Source Folder set target=D:\Target Folder for /f "tokens=*" %%a in (' dir /b "%source%" ') do call :process "%%a" endlocal PAUSE goto :eof ::---------- :process set filename=%~1 set foldername=%~n1 ECHO md "%target%\%foldername%" ECHO move "%source%\%filename%" "%target%\%foldername%\" PAUSE goto :eof
No comments included - ask if you aren't familiar with anything.
Name it MOVER.BAT (or anything you like!). Try it - once you are happy that the correct MD and MOVE statements are showing what you expect, remove ECHO (and perhaps PAUSEs).
Any problems, ask again.0 -
Looks good to me. Handles files with embedded spaces (always tricky in bats). Repeat running should be ok but may be worth adding detection of files in the for loop otherwise it'll pick up existing directories.
eg try
for /f "tokens=*" %%a in (' dir /b /A-D "%source%" ') do call :process "%%a"
rather than
for /f "tokens=*" %%a in (' dir /b "%source%" ') do call :process "%%a"
EDIT: and also make sure mover.bat is not in the same directory as Source Folder (obvious I know).If you put your general location in your Profile, somebody here may be able to come and help you.0 -
There are a number of checks I would introduce for a production file - but this is just to be run once!0
-
Thanks, I'll give it a try later0
-
Hi
Sorry, I couldn't get it to work. The echo statements ahead of each operation indicate that it is just about to do the right thing, but it doesn't actually make any directories or move any files....
I tried a (more focussed) test of it on thedrive (a partition on the main drive, rather than on an external drive) and with short path statements without spaces. Here's the dialogue when running it:
md "d:\test2\test" move "d:\test\test.txt" "d:\test2\test\" Press any key to continue . . . md "d:\test2\test2" move "d:\test\test2.txt" "d:\test2\test2\" Press any key to continue . . .
It doesn't report that it has moved the file (as it does when I just key the equivalent command (move) straight into a command line window) and it hasn't created the folder or moved the file.... It doesn't report any errors.
if I just key
md "d:\test2\test"
then
move "d:\test\test.txt" "d:\test2\test\"
into a commandline window, it works as expected, but not when running it from the batch file.
.
if I first manually make the subdirectory in the destination folder; then run the script, it also doesn't move the file into it.
.
if I use the code exactly as in post #4 (i.e. without me making edits to include my path statements), same result:md "D:\Target Folder\Testfile1" move "D:\Source Folder\Testfile1.txt" "D:\Target Folder\Testfile1\" Press any key to continue . . . md "D:\Target Folder\Testfile2" move "D:\Source Folder\Testfile2.txt" "D:\Target Folder\Testfile2\" Press any key to continue . . .
...no errors reported, but nothing in the target folder at the end.
.0 -
I vaguely recollect encountering problems in the past with BAT files when working with folders on a different drive to the default drive.
This is just a guess, but you could try adding a line at the start of the BAT file to change the default drive to thedrive (i.e. simply a line with
on it).
0 -
Hi
Sorry, I couldn't get it to work. The echo statements ahead of each operation indicate that it is just about to do the right thing, but it doesn't actually make any directories or move any files....
Did you pay heed to this comment?Try it - once you are happy that the correct MD and MOVE statements are showing what you expect, remove ECHO (and perhaps PAUSEs).
Did you edit the BAT file to remove the ECHO statements?0 -
re: #9
No, that doesn't fix it. But thanks for the suggestion.
.0
This discussion has been closed.
Confirm your email address to Create Threads and Reply

Categories
- All Categories
- 350.8K Banking & Borrowing
- 253.1K Reduce Debt & Boost Income
- 453.5K Spending & Discounts
- 243.8K Work, Benefits & Business
- 598.7K Mortgages, Homes & Bills
- 176.8K Life & Family
- 257.1K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.1K Discuss & Feedback
- 37.6K Read-Only Boards