Copy files to folders with samename

April 3, 2009 at 14:22:22
Specs: Windows XP
Hey everyone, I need to move files into some folders with the same name. I have thousands of files named RFI 1000, RFI 1001, RFI 1002, sometimes RFI 1002.1, etc. I have folders of the exact same names but i need to move each file into the folder with the same name.

I've never actually made a batch file but i found the code below in an earlier post and it doesn't seem to work it just goes through all of the files and does nothing. Any help would be great, just be ready for my questions since I'm very new at this :)

FOR %%a IN (%1\*.*) DO call :doWork "%%~Na" "%%~Fa"
GoTo :eof

SET d=%~1
SET d=%d:~0,10%
MD %d%
COPY %2 %d%

See More: Copy files to folders with samename

Report •

April 5, 2009 at 14:45:35
I think the problem is that although the folders may appear to have the same name, winxp actually appends "(1)" when it is created within windows but at a command prompt it won't do it at all. ie the folders are probably dirname and dirname (1) or something similar. Actually, I just tried it within winxp and it won't allow it their either. The folders cannot be the same name.

John W. Borelli
IT Specialist

Report •

April 5, 2009 at 15:33:34
I think Klint answered your question with this:-

@echo off
setlocal enabledelayedexpansion
pushd "C:\Change to\Whateveryour\path"
for %%j in (*) do (
set name=%%~nj
if exist !name!\ (
move "%%j" "!name!"
) else (
echo Warning: %%j not moved to !name!: folder does not exist.

This will move any files into a folder (in the same directory) with the same name as the file. If the folder does not exist it will not move the files.
Should the folders be in a different directory change:
move "%%j" "!name!"
move "%%j" "Z:\parent\folder\etc\!name!"

I am going away for the Easter break so will not be around for any more response

OK just incase
This will make a folder from a file with the same name and move one into the other:

@echo off
pushd "C:\Docs and Sets\All Users\Docs\"
for %%j in (*) do (
md "%%~nj"
move "%%j" "%%~nj"

Report •

April 6, 2009 at 11:50:26
Thank you Shaka (and Klint), those formulas worked perfectly, just what I needed.

I'll be using this batch for along time :)

Report •

Related Solutions

Ask Question