Computing.Net > Forums > Programming > Copy Files to Folder with Same Name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Copy Files to Folder with Same Name

Reply to Message Icon

Name: scampi01
Date: July 15, 2007 at 16:37:50 Pacific
OS: DOS
CPU/Ram: 1024
Comment:

Hi There,

I am trying to figure out how to write a batch program that will take the first 4 characters of a file name (from the cd rom) and move it to the appropriate file name folder. For example, the cd has pdf files called 0001a.pdf, 0002a.pdf, 0003a.pdf, etc. I want to parse the first 4 characters, and move the pdf file to the folders called 0001, 0002, 0003, etc.

Any ideas ?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: July 15, 2007 at 18:29:04 Pacific
Reply:

Untested batch:

GoTo start
Steps to use:
1. Save this as whatever.bat
2. Move whatever.bat to where you want to plant the new directory tree
3. Drag'n'drop the drive/folder icon that holds the all of the files to whatever.bat's icon.
4. I can't be held responsible if you don't follow step 3. May your God have mercy on your computer if you fail to follow step 3.
:start
FOR %%a IN (%1\*.*) DO call :doWork "%%~Na" "%%~Fa"
GoTo :eof

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


0

Response Number 2
Name: ghostdog
Date: July 16, 2007 at 07:34:13 Pacific
Reply:

a vbscript:
[code]
Option Explicit
Dim objFSO,srcRoot,objFolder,File,dstRoot,theName,dst,dstFile
Set objFSO=CreateObject("Scripting.FileSystemObject")
srcRoot="c:\temp"
dstRoot="c:\test1"
Set objFolder=objFSO.GetFolder(srcRoot)
For Each File In objFolder.Files
If objFSO.GetExtensionName(File) = "pdf" Then
theName= Left(File.Name,4)
dst=dstRoot & "\" & theName
dstFile=dst & "\" & File.Name
If Not objFSO.FolderExists(dst) Then
objFSO.CreateFolder(dst)
End If
objFSO.MoveFile File, dstFile
End If
Next
[/code]


0

Response Number 3
Name: anubis81
Date: August 23, 2007 at 10:56:02 Pacific
Reply:

Is it possible to modify this vbs script to just move files to a folder of the same name that already exists? I would also like to rename the once they are moved to the new folder. They would all have the same name.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


batch search f. and go to... Joining line using awk



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 Folder with Same Name

Copy files to folders with samename www.computing.net/answers/programming/copy-files-to-folders-with-samename/18511.html

vbscript find all subfolders with same name www.computing.net/answers/programming/vbscript-find-all-subfolders-with-same-name/18775.html

move files with same name www.computing.net/answers/programming/move-files-with-same-name/18856.html