Computing.Net > Forums > Programming > Copy files to folders with samename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Copy files to folders with samename

Reply to Message Icon

Name: Wiggyspawn
Date: April 3, 2009 at 14:22:22 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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 :)

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

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



Sponsored Link
Ads by Google

Response Number 1
Name: borelli35
Date: April 5, 2009 at 14:45:35 Pacific
Reply:

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
OmniIT
borelli35


0

Response Number 2
Name: Shaka
Date: April 5, 2009 at 15:33:34 Pacific
Reply:

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.
)
)
popd
pause
exit

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!"
into
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"
)
popd
pause
exit


0

Response Number 3
Name: Wiggyspawn
Date: April 6, 2009 at 11:50:26 Pacific
Reply:

Thank you Shaka (and Klint), those formulas worked perfectly, just what I needed.

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


awk script problem Batch Capitalizer



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Copy files to folders with samename

Copy Files to Folder with Same Name www.computing.net/answers/programming/copy-files-to-folder-with-same-name/15568.html

copy files from folder A to folder B .... www.computing.net/answers/programming/copy-files-from-folder-a-to-folder-b-/20177.html

Batch File To temporarily backup www.computing.net/answers/programming/batch-file-to-temporarily-backup-/9826.html