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


  1. Posts : 1
    Windows 7 64
       #1

    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:
    C:\Local\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.

    Help?
      My Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    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.


    Preview:
    Code:
    @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.
    )
    popd
    Move files from original folder to existing folders - w batch file? Attached Files
      My Computer


 

  Related Discussions
Our Sites
Site Links
About 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 03:40.
Find Us