Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Move files from original folder to existing folders - w batch file?

24 Mar 2015   #1

Windows 7 64
Move files from original folder to existing folders - w batch file?

My files have a format of "ID_date_time.d7d
Ex: 11111-5_2013_10_07_142149.d7d
The ID is 1111-5 and was tested 10/7/2013 at 2:21:49 PM and the file extension is always .d7d

The local files are automatically saved here by the program generating the data:

I want them copied to the following directory:
E:\common\UserGroup\UserLocation\Test Data\(ID)
For the above example, it would be copied to: E:\common\UserGroup\UserLocation\Test Data\11111-5

Then, upon successful completion of copying the file, I want the file moved to:
C:\IDArchive\Data\Old Data

Then the file should be removed from the original Local folder.

I can do most of this with a batch file, up to the point where I match the file in the Local\Data folder to its proper destination using its ID number. I need to pull the ID number out of the file name and use it to identify its destination - all of the destination folders already exist. I can't figure out the correct code in the batch file to find the ID number and then extract and match it to its folder.


My System SpecsSystem Spec
25 Mar 2015   #2

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan

Hi Jordansl,

Here's a full solution to the task you mention. A double For /F loop is used to extract this ID from each file name.

@echo off

REM For each file in %source_location%, copy it to
REM %copy_to%\*ItsID*, then move the file to %move_to%.

set from_source="C:\Local\Data"
set copy_to="C:\common\UserGroup\UserLocation\Test Data"
set move_to="C:\IDArchive\Data\Old Data"

pushd "%from_source:"=%"
for /f "delims=" %%A in (' dir /a:-d /b %from_source:"=% ') do (
	for /f "tokens=1 delims=_" %%B in ( "%%~nxA" ) do (
		for %%C in (%COPY_TO%) do (
			if exist "%%~fC\%%~B"\* (
				copy "%%~fA" "%%~fC\%%~B"
			) else (
				echo.& echo A destination directory for the file '%%~fA' does not exist.
				echo Would you like one to be created for it with the file '%%~nxA' copied to it?
				echo If N, the file will not be copied or moved.
				choice & if not errorlevel 2 md "%%~fC\%%~B" && copy "%%~fA" "%%~fC\%%~B"
	if not errorlevel 2 move "%%~fA" "%MOVE_TO:"=%" || echo.

Attached Files
File Type: bat th-365156.bat (889 Bytes, 2 views)
My System SpecsSystem Spec

 Move files from original folder to existing folders - w batch file?

Thread Tools

Similar help and support threads
Thread Forum
batch&task to create new folder and move all desktop file to it
hi so work in an recruitment office and im trying to create a batch file that the computers can run on a schedule to create a folder with that days date on it and move all files from the desktop to that folder( also created on the desktop), i want the code to only move files and leave folders and...
General Discussion
[Help] Batch File move every 15.000 files to another folders
Hi guys I'm new here, please help I have about 2 million email files that i want to convert into mbox files. but the tool i used only can convert every 15.000 files. so i need a batch file that can move this 2 million files into another folder (15.000 files each folder) and the folder name...
General Discussion
Batch code to read, then move files from one folder to another
I'm new to batch coding, so I was hoping someone could help me out. I need to read several .txt files and search them for a specific string, lets say "key1" and "key2." If the file has key1, then it needs to be moved into directory1. If the file has key2, then it needs to be moved to directory2....
General Discussion
Batch file to move (many) duplicted folder contents - Help!!
Hope I can explain my difficulty. I have duplicated folders album name for all my music files. As there is quite a considerable number I wondered if it was possible to batch file (automate) the process. I can not delete "d" as this would delete all the tracks. What I need to do is move the...
General Discussion
How to create a batch file that will move files based on the file type
How to create a batch file that will move files based on the file type? My Requirement : I need a batch file to copy all .doc files in all my HardDisk to a removable disk say (Z: drive) . Can anyone make it possible using a batch file?Please...Its urgent...
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 20:43.
Twitter Facebook