简体   繁体   中英

Make subfolder names from part of file name and copy files with Robocopy

Is it posible to copy and make directories automatically from file name substrings using Robocopy?

I mean i have files like these. LAJ00306130201004626.rc the first 8 chararacters are control number ( LAJ00306 =control number) this would be the name of the folder and the rest are the date and time (Date=130201) (time=004626).

LAJ00306130201004626.rc
LAJ00306130202004626.rc
LAJ00306130203004626.rc
LAJ00307130201004626.rc
LAJ00307130202004626.rc

and i would like to copy and create folders from the file name like under and copy the files mentioned before in the new folders.

LAJ00306
LAJ00307

I hope to be clear if necessary ask me for more information

try this, look at the output and remove the echo s before MD and ROBOCOPY , if it looks good:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcefolder=."
SET "targetfolder=X:\data"

CD /d "%sourcefolder%"
FOR %%a IN (*.rc) DO (
    SET "fname=%%a"
    SET "folder=!fname:~0,8!"
    SET "$!folder!=1"
)

FOR /f "delims=$=" %%a IN ('set "$"') DO (
    ECHO MD "%targetfolder%\%%a" 2>nul
    ECHO ROBOCOPY "%sourcefolder%" "%targetfolder%\%%a" "%%a*.rc"
)

Set sourcefolder and targetfolder for your folder tree.

Try this:

@echo off
pushd "c:\source folder"
setlocal enabledelayedexpansion
for %%a in (*.rc) do (
    set "name=%%a"
    robocopy "%cd%" "%%a" "D:\target directory\!name:~0,8!" 
)
popd

Answers to your questions are:

pushd "drive:\\path" makes the location the current working directory.
popd restores the last working directory
setlocal enabledelayedexpansion allows you to change and use variables within a loop, using the !variable! syntax.

If your 2000 files are in a single folder then it should work - but test it on some sample files first so that you can see how it will work.

@ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=c:\destdir"
FOR /f "tokens=1*delims=_" %%i IN (
 'dir /b /a-d "%sourcedir%\*_*."'
) DO XCOPY /b "%sourcedir%\%%i_%%j" "%destdir%\%%i\"
GOTO :EOF

This should accomplish the task described. You'd need to set up the source and destination directories to suit, of course. Add >nul to the end of the XCOPY line to suppress 'copied' messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM